Skip to content

Commit

Permalink
support git log format time
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Jan 6, 2022
1 parent 0ba7150 commit 8f069a0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
25 changes: 19 additions & 6 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TimeE(val interface{}) (t time.Time, err error) {
func parseDate(s string) (t time.Time, err error) {
fs := []string{
time.RFC3339,
"2006-01-02T15:04:05", // iso8601 without timezone
time.RFC1123Z,
time.RFC1123,
time.RFC822Z,
Expand All @@ -53,6 +52,13 @@ func parseDate(s string) (t time.Time, err error) {
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
"2006-01-02T15:04:05", // ISO8601 without timezone
"Mon Jan 2 15:04:05 2006 -0700", // Git log date
"2006-01-02 15:04:05.999999999 -0700 MST", // Time.String()
"2006-01-02",
"02 Jan 2006",
Expand All @@ -62,18 +68,25 @@ func parseDate(s string) (t time.Time, err error) {
"2006-01-02 15:04:05Z07:00", // RFC3339 without T
"2006-01-02 15:04:05Z0700", // RFC3339 without T or timezone hh:mm colon
"2006-01-02 15:04:05",
"2006-01-02 15:04:05.000",
"2006-01-02 15:04:05.000000",
"2006-01-02 15:04:05.000000000",
"2006.01.02",
"2006.01.02 15:04:05",
"2006.01.02 15:04:05.000",
"2006.01.02 15:04:05.000000",
"2006.01.02 15:04:05.000000000",
"2006/01/02",
"2006/01/02 15:04:05",
"2006/01/02 15:04:05.000",
"2006/01/02 15:04:05.000000",
"2006/01/02 15:04:05.000000000",
"2006年01月02日",
"2006年01月02日 15:04:05",
"2006年01月02日 15:04:05.000",
"2006年01月02日 15:04:05.000000",
"2006年01月02日 15:04:05.000000000",
"2006年01月02日 15时04分05秒",
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
}

for _, dateType := range fs {
Expand Down
13 changes: 13 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,30 @@ func TestTimeE(t *testing.T) {
{"Nov 10 23:00:00.000000000", time.Date(0, 11, 10, 23, 0, 0, 0, loc), false}, // StampNano
{"2016-03-06 15:28:01-00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false}, // RFC3339 without T
{"2016-03-06 15:28:01-0000", time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false}, // RFC3339 without T or timezone hh:mm colon
{"Fri Sep 25 13:58:21 2016 -0400", time.Date(2016, 9, 25, 17, 58, 21, 0, time.UTC), false},
{"2016-03-06 15:28:01", time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false},
{"2016-03-06 15:28:01.618", time.Date(2016, 3, 6, 15, 28, 1, 618000000, loc), false},
{"2016-03-06 15:28:01.618003", time.Date(2016, 3, 6, 15, 28, 1, 618003000, loc), false},
{"2016-03-06 15:28:01.618003001", time.Date(2016, 3, 6, 15, 28, 1, 618003001, loc), false},
{"2016-03-06 15:28:01 -0000", time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false},
{"2016-03-06 15:28:01 -00:00", time.Date(2016, 3, 6, 15, 28, 1, 0, loc), false},
{"2006-01-02", time.Date(2006, 1, 2, 0, 0, 0, 0, loc), false},
{"02 Jan 2006", time.Date(2006, 1, 2, 0, 0, 0, 0, loc), false},
{"2010.03.07", time.Date(2010, 3, 7, 0, 0, 0, 0, loc), false},
{"2010.03.07 18:08:18", time.Date(2010, 3, 7, 18, 8, 18, 0, loc), false},
{"2010.03.07 18:08:18.618", time.Date(2010, 3, 7, 18, 8, 18, 618000000, loc), false},
{"2010.03.07 18:08:18.618003", time.Date(2010, 3, 7, 18, 8, 18, 618003000, loc), false},
{"2010.03.07 18:08:18.618003001", time.Date(2010, 3, 7, 18, 8, 18, 618003001, loc), false},
{"2010/03/07", time.Date(2010, 3, 7, 0, 0, 0, 0, loc), false},
{"2010/03/07 18:08:18", time.Date(2010, 3, 7, 18, 8, 18, 0, loc), false},
{"2010/03/07 18:08:18.618", time.Date(2010, 3, 7, 18, 8, 18, 618000000, loc), false},
{"2010/03/07 18:08:18.618003", time.Date(2010, 3, 7, 18, 8, 18, 618003000, loc), false},
{"2010/03/07 18:08:18.618003001", time.Date(2010, 3, 7, 18, 8, 18, 618003001, loc), false},
{"2010年03月07日", time.Date(2010, 3, 7, 0, 0, 0, 0, loc), false},
{"2010年03月07日 18:08:18", time.Date(2010, 3, 7, 18, 8, 18, 0, loc), false},
{"2010年03月07日 18:08:18.618", time.Date(2010, 3, 7, 18, 8, 18, 618000000, loc), false},
{"2010年03月07日 18:08:18.618003", time.Date(2010, 3, 7, 18, 8, 18, 618003000, loc), false},
{"2010年03月07日 18:08:18.618003001", time.Date(2010, 3, 7, 18, 8, 18, 618003001, loc), false},
{"2010年03月07日 18时08分18秒", time.Date(2010, 3, 7, 18, 8, 18, 0, loc), false},
{1472574600, time.Date(2016, 8, 30, 16, 30, 0, 0, loc), false},
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, loc), false},
Expand Down

0 comments on commit 8f069a0

Please sign in to comment.