From 8fc500a15674fe97ee5297e81b9ab66d2f42602a Mon Sep 17 00:00:00 2001 From: gouguoyin <245629560@qq.com> Date: Thu, 27 Oct 2022 14:46:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parser.go | 39 +++++++++++---------------------------- parser_test.go | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/parser.go b/parser.go index 1d824e2..b854000 100755 --- a/parser.go +++ b/parser.go @@ -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 } diff --git a/parser_test.go b/parser_test.go index 0e83d2d..16a4976 100755 --- a/parser_test.go +++ b/parser_test.go @@ -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"},