-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
## Change log | ||
|
||
- Optimize error codes and logs. | ||
- Change protobuf demo. | ||
1. Change page field size to limit. | ||
2. Supporting custom bingdingXXX and response in the web(protobuf). | ||
3. Match message name xxxID in the protobuf file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package utils | ||
|
||
import "time" | ||
|
||
const ( | ||
// DateTimeLayout is the layout string for datetime format. | ||
DateTimeLayout = "2006-01-02 15:04:05" | ||
|
||
// DateTimeLayoutWithMS is the layout string for datetime format with milliseconds. | ||
DateTimeLayoutWithMS = "2006-01-02 15:04:05.000" | ||
|
||
// DateTimeLayoutWithMSAndTZ is the layout string for datetime format with milliseconds and timezone. | ||
DateTimeLayoutWithMSAndTZ = "2006-01-02T15:04:05.000Z" | ||
|
||
// TimeLayout is the layout string for time format. | ||
TimeLayout = "15:04:05" | ||
|
||
// DateLayout is the layout string for date format. | ||
DateLayout = "2006-01-02" | ||
) | ||
|
||
// FormatDateTime formats the given time to string | ||
func FormatDateTime(t time.Time, format string) string { | ||
switch format { | ||
case DateTimeLayoutWithMS: | ||
return t.Format(DateTimeLayoutWithMS) | ||
case DateTimeLayoutWithMSAndTZ: | ||
return t.UTC().Format(DateTimeLayoutWithMSAndTZ) | ||
case TimeLayout: | ||
return t.Format(TimeLayout) | ||
case DateLayout: | ||
return t.Format(DateLayout) | ||
default: | ||
return t.Format(DateTimeLayout) | ||
} | ||
} | ||
|
||
// ParseDateTime parses the given string to time | ||
func ParseDateTime(s string, format string) (time.Time, error) { | ||
switch format { | ||
case DateTimeLayoutWithMS: | ||
return time.Parse(DateTimeLayoutWithMS, s) | ||
case DateTimeLayoutWithMSAndTZ: | ||
return time.Parse(DateTimeLayoutWithMSAndTZ, s) | ||
case TimeLayout: | ||
return time.Parse(TimeLayout, s) | ||
case DateLayout: | ||
return time.Parse(DateLayout, s) | ||
default: | ||
return time.Parse(DateTimeLayout, s) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFormatAndParseDateTime(t *testing.T) { | ||
layouts := []string{ | ||
DateTimeLayout, | ||
DateTimeLayoutWithMS, | ||
DateTimeLayoutWithMSAndTZ, | ||
TimeLayout, | ||
DateLayout, | ||
} | ||
|
||
now := time.Now() | ||
|
||
for _, layout := range layouts { | ||
str := FormatDateTime(now, layout) | ||
ti, err := ParseDateTime(str, layout) | ||
t.Log(str, ti.Second() == now.Second(), err) | ||
} | ||
} |