增加单元测试用例

This commit is contained in:
gouguoyin 2022-10-27 14:46:59 +08:00
parent f68e3fc15f
commit 8fc500a156
2 changed files with 30 additions and 29 deletions

View File

@ -10,28 +10,16 @@ func (c Carbon) Parse(value string, timezone ...string) Carbon {
timeLayouts := []string{
DayDateTimeLayout,
DateTimeLayout,
DateTimeMilliLayout,
DateTimeMicroLayout,
DateTimeNanoLayout,
ShortDateTimeLayout,
ShortDateTimeMilliLayout,
ShortDateTimeMicroLayout,
ShortDateTimeNanoLayout,
DateLayout,
DateMilliLayout,
DateMicroLayout,
DateNanoLayout,
ShortDateLayout,
ShortDateMilliLayout,
ShortDateMicroLayout,
ShortDateNanoLayout,
TimeLayout,
TimeMilliLayout,
TimeMicroLayout,
TimeNanoLayout,
ShortTimeLayout,
ShortTimeMilliLayout,
ShortTimeMicroLayout,
ShortTimeNanoLayout,
ANSICLayout,
UnixDateLayout,
@ -44,35 +32,29 @@ func (c Carbon) Parse(value string, timezone ...string) Carbon {
KitchenLayout,
CookieLayout,
RFC3339Layout,
RFC3339MilliLayout,
RFC3339MicroLayout,
RFC3339NanoLayout,
ISO8601Layout,
ISO8601MilliLayout,
ISO8601MicroLayout,
ISO8601NanoLayout,
RFC1036Layout,
RFC7231Layout,
"2006", "2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5", "2006-1-2 15:4:5.999999999",
"2006.1", "2006.1.2", "2006.1.2 15", "2006.1.2 15:4", "2006.1.2 15:4:5", "2006.1.2 15:4:5.999999999",
"2006", "2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5",
"2006.1.2", "2006.1.2 15", "2006.1.2 15:4", "2006.1.2 15:4:5", "2006.1.2", "2006.1.2 15", "2006.1.2 15:4", "2006.1.2 15:4:5", "2006.1.2 15:4:5.999999999",
"2006/1", "2006/1/2", "2006/1/2 15", "2006/1/2 15:4", "2006/1/2 15:4:5", "2006/1/2 15:4:5.999999999",
"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",
"15:4:5 Jan 2, 2006 MST", "2006-1-2 15:4:5.999999999 -0700 MST", "Monday, 2-Jan-2006 15:4:5 MST",
"2006-1-2T15:4:5Z07", "2006-1-2T15:4:5Z0700", "2006-1-2T15:4:5Z07:00", "2006-1-2T15:4:5-07:00", "2006-1-2T15:4:5.999999999Z07", "2006-1-2T15:4:5.999999999Z0700", "2006-1-2T15:4:5.999999999-07:00", "2006-1-2T15:4:5.999999999Z07:00",
}
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.Error != nil {
return c
"15:4:5 Jan 2, 2006 MST", "2006-1-2 15:4:5.999999999 -0700 MST",
"2006-1-2T15:4:5-07:00", "2006-1-2T15:4:5.999999999-07:00",
"2006-1-2T15:4:5Z07", "2006-01-02T15:04:05Z0700", "2006-1-2T15:4:5Z07:00", "2006-1-2T15:4:5.999999999Z07:00",
}
if value == "" || value == "0" || value == "0000-00-00 00:00:00" || value == "0000-00-00" || value == "00:00:00" {
return c
}
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
for _, layout := range timeLayouts {
tt, err := time.ParseInLocation(layout, value, c.loc)
t, err := time.ParseInLocation(layout, value, c.loc)
if err == nil {
c.time = tt
c.time = t
return c
}
}
@ -120,6 +102,7 @@ func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon {
return c
}
c.time = tt
c.loc = tt.Location()
return c
}

View File

@ -20,8 +20,26 @@ func TestCarbon_Parse(t *testing.T) {
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020-8-5", "2020-08-05 00:00:00 +0800 CST"},
{"2020-8-05", "2020-08-05 00:00:00 +0800 CST"},
{"2020-08-05", "2020-08-05 00:00:00 +0800 CST"},
{"2020-8-5 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020-8-05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020-08-05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020.8.5", "2020-08-05 00:00:00 +0800 CST"},
{"2020.8.05", "2020-08-05 00:00:00 +0800 CST"},
{"2020.08.05", "2020-08-05 00:00:00 +0800 CST"},
{"2020.8.5 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020.8.05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020.08.05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/8/5", "2020-08-05 00:00:00 +0800 CST"},
{"2020/8/05", "2020-08-05 00:00:00 +0800 CST"},
{"2020/08/05", "2020-08-05 00:00:00 +0800 CST"},
{"2020/8/5 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/8/05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020/08/05 13:14:15", "2020-08-05 13:14:15 +0800 CST"},
{"2020-8-5T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-8-05T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-08-05T13:14:15+08:00", "2020-08-05 13:14:15 +0800 CST"},
{"2020-08-05T13:14:15.999+08:00", "2020-08-05 13:14:15.999 +0800 CST"},
{"2020-08-05T13:14:15.999999+08:00", "2020-08-05 13:14:15.999999 +0800 CST"},