Skip to content

Commit

Permalink
Fix issue #202
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Nov 30, 2023
1 parent 39b89f2 commit 9dd8c9d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
9 changes: 5 additions & 4 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ var layouts = []string{
"2006-01-02 15:04:05 PM MST", "2006-01-02 15:04:05.999999999 PM MST", "2006-1-2 15:4:5 PM MST", "2006-1-2 15:4:5.999999999 PM MST",
"1/2/2006", "1/2/2006 15", "1/2/2006 15:4", "1/2/2006 15:4:5", "1/2/2006 15:4:5.999999999",
"2006-1-2 15:4:5 -0700 MST", "2006-1-2 15:4:5.999999999 -0700 MST", "2006-1-2 15:04:05 -0700 MST", "2006-1-2 15:04:05.999999999 -0700 MST",
"2006-01-02T15:04:05", "2006-01-02T15:04:05.999999999", "2006-01-02T15:4:5", "2006-01-02T15:4:5.999999999",
"2006-1-2T15:04:05Z07", "2006-1-2T15:04:05.999999999Z07", "2006-1-2T15:4:5Z07", "2006-1-2T15:4:5.999999999Z07",
"2006-1-2T15:04:05Z07:00", "2006-1-2T15:04:05.999999999Z07:00", "2006-1-2T15:4:5Z07:00", "2006-1-2T15:4:5.999999999Z07:00",
"2006-1-2T15:04:05-07:00", "2006-1-2T15:04:05.999999999-07:00", "2006-1-2T15:4:5-07:00", "2006-1-2T15:4:5.999999999-07:00",
"2006-01-02T15:04:05", "2006-01-02T15:04:05.999999999", "2006-1-2T3:4:5", "2006-1-2T3:4:5.999999999",
"2006-01-02T15:04:05Z07", "2006-01-02T15:04:05.999999999Z07", "2006-1-2T15:4:5Z07", "2006-1-2T15:4:5.999999999Z07",
"2006-01-02T15:04:05Z07:00", "2006-01-02T15:04:05.999999999Z07:00", "2006-1-2T15:4:5Z07:00", "2006-1-2T15:4:5.999999999Z07:00",
"2006-01-02T15:04:05-07:00", "2006-01-02T15:04:05.999999999-07:00", "2006-1-2T15:4:5-07:00", "2006-1-2T15:4:5.999999999-07:00",
"2006-01-02T15:04:05-0700", "2006-01-02T15:04:05.999999999-0700", "2006-1-2T3:4:5-0700", "2006-1-2T3:4:5.999999999-0700",
"20060102150405-07:00", "20060102150405.999999999-07:00",
"20060102150405Z07", "20060102150405.999999999Z07",
"20060102150405Z07:00", "20060102150405.999999999Z07:00",
Expand Down
25 changes: 25 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,31 @@ func TestCarbon_ParseByLayout(t *testing.T) {
}
}

// https://github.com/golang-module/carbon/issues/202
func TestCarbon_Issue202(t *testing.T) {
assert := assert.New(t)

tests := []struct {
input string // 输入值
output string // 期望值
}{
{"2023-01-08T09:02:48", "2023-01-08 09:02:48 +0800 CST"},
{"2023-1-8T09:02:48", "2023-01-08 09:02:48 +0800 CST"},
{"2023-01-08T9:2:48", "2023-01-08 09:02:48 +0800 CST"},
{"2023-01-8T9:2:48", "2023-01-08 09:02:48 +0800 CST"},
{"2023-1-8T9:2:48", "2023-01-08 09:02:48 +0800 CST"},
{"2023-01-08T09:02:48.000+0000", "2023-01-08 17:02:48 +0800 CST"},
{"2023-1-8T09:02:48.000+0000", "2023-01-08 17:02:48 +0800 CST"},
{"2023-1-8T9:2:48.000+0000", "2023-01-08 17:02:48 +0800 CST"},
}

for index, test := range tests {
c := SetTimezone(PRC).Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.output, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}

func TestError_Parse(t *testing.T) {
date, layout, format, timezone := "2020-08-50", "2006-01-02", DateLayout, "xxx"
assert.NotNil(t, Parse(date).Error, "It should catch an exception in Parse()")
Expand Down

0 comments on commit 9dd8c9d

Please sign in to comment.