From 16f401949914b3496f8df1cf60c6e51274c225de Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Thu, 30 Nov 2023 14:13:43 +0800 Subject: [PATCH 1/5] Upgrade from v2.2.13 to 2.2.14 --- carbon.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/carbon.go b/carbon.go index 318dd62b..543d7a13 100644 --- a/carbon.go +++ b/carbon.go @@ -14,7 +14,7 @@ import ( // Version current version // 当前版本号 -const Version = "2.2.13" +const Version = "2.2.14" // timezone constants // 时区常量 From 2cadc97dd52cb276259a9c84e24a9064521ed2f2 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Thu, 30 Nov 2023 14:14:38 +0800 Subject: [PATCH 2/5] Add `Parse` method support for time format --- helper.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/helper.go b/helper.go index 59c84164..3156486f 100755 --- a/helper.go +++ b/helper.go @@ -52,10 +52,11 @@ var layouts = []string{ "2006-01-02 15:04:05PM MST", "2006-01-02 15:04:05.999999999PM MST", "2006-1-2 15:4:5PM MST", "2006-1-2 15:4:5.999999999PM MST", "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-2T15:4:5Z07", "2006-1-2T15:4:5.999999999Z07", - "2006-1-2T15:4:5Z07:00", "2006-1-2T15:4:5.999999999Z07:00", - "2006-1-2T15:4:5-07:00", "2006-1-2T15:4:5.999999999-07:00", + "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", "20060102150405-07:00", "20060102150405.999999999-07:00", "20060102150405Z07", "20060102150405.999999999Z07", "20060102150405Z07:00", "20060102150405.999999999Z07:00", From ff3038e6d95cf148a6714538fe4a256de5476791 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Thu, 30 Nov 2023 14:15:41 +0800 Subject: [PATCH 3/5] Increase unit testing coverage for `Fastest` method --- traveler_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/traveler_test.go b/traveler_test.go index 98d2b5af..95983d2a 100755 --- a/traveler_test.go +++ b/traveler_test.go @@ -97,6 +97,7 @@ func TestCarbon_Farthest(t *testing.T) { {"2023-04-01", "2023-03-28", "2023-03-28", "2023-03-28"}, {"2023-04-01", "2023-03-28", "2023-04-16", "2023-04-16"}, + {"2023-04-01", "2023-04-05", "2023-04-02", "2023-04-05"}, } for index, test := range tests { From 39b89f21f16769a9959afcb12a47fcdd53f0bf2f Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Thu, 30 Nov 2023 14:16:29 +0800 Subject: [PATCH 4/5] timezone[len(timezone)-1] -> timezone[0] --- creator.go | 10 ++--- difference.go | 20 +++++----- outputer.go | 108 +++++++++++++++++++++++++------------------------- parser.go | 4 +- traveler.go | 6 +-- 5 files changed, 74 insertions(+), 74 deletions(-) diff --git a/creator.go b/creator.go index b492d5be..46458136 100755 --- a/creator.go +++ b/creator.go @@ -17,7 +17,7 @@ func CreateFromStdTime(tt time.Time) Carbon { // 从给定的秒级时间戳创建 Carbon 实例 func (c Carbon) CreateFromTimestamp(timestamp int64, timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c @@ -36,7 +36,7 @@ func CreateFromTimestamp(timestamp int64, timezone ...string) Carbon { // 从给定的毫秒级时间戳创建 Carbon 实例 func (c Carbon) CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c @@ -55,7 +55,7 @@ func CreateFromTimestampMilli(timestamp int64, timezone ...string) Carbon { // 从给定的微秒级时间戳创建 Carbon 实例 func (c Carbon) CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c @@ -74,7 +74,7 @@ func CreateFromTimestampMicro(timestamp int64, timezone ...string) Carbon { // 从给定的纳秒级时间戳创建 Carbon 实例 func (c Carbon) CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c @@ -254,7 +254,7 @@ func CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string // 从给定的年、月、日、时、分、秒、纳秒创建 Carbon 实例 func (c Carbon) create(year, month, day, hour, minute, second, nanosecond int, timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c diff --git a/difference.go b/difference.go index c5e5e9a4..cda253b6 100755 --- a/difference.go +++ b/difference.go @@ -13,7 +13,7 @@ func (c Carbon) DiffInYears(carbon ...Carbon) int64 { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } dy, dm, dd := end.Year()-start.Year(), end.Month()-start.Month(), end.Day()-start.Day() @@ -40,7 +40,7 @@ func (c Carbon) DiffInMonths(carbon ...Carbon) int64 { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } startYear, startMonth, startDay := c.Date() endYear, endMonth, endDay := end.Date() @@ -75,7 +75,7 @@ func (c Carbon) DiffInWeeks(carbon ...Carbon) int64 { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (7 * 24 * 3600)))) } @@ -94,7 +94,7 @@ func (c Carbon) DiffInDays(carbon ...Carbon) int64 { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } return int64(math.Floor(float64((end.Timestamp() - start.Timestamp()) / (24 * 3600)))) } @@ -113,7 +113,7 @@ func (c Carbon) DiffInHours(carbon ...Carbon) int64 { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } return c.DiffInSeconds(end) / SecondsPerHour } @@ -132,7 +132,7 @@ func (c Carbon) DiffInMinutes(carbon ...Carbon) int64 { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } return c.DiffInSeconds(end) / SecondsPerMinute } @@ -151,7 +151,7 @@ func (c Carbon) DiffInSeconds(carbon ...Carbon) int64 { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } return end.Timestamp() - c.Timestamp() } @@ -170,7 +170,7 @@ func (c Carbon) DiffInString(carbon ...Carbon) string { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } if c.Error != nil || end.Error != nil { return "" @@ -187,7 +187,7 @@ func (c Carbon) DiffAbsInString(carbon ...Carbon) string { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } if c.Error != nil || end.Error != nil { return "" @@ -204,7 +204,7 @@ func (c Carbon) DiffForHumans(carbon ...Carbon) string { end = CreateFromTimestampNano(c.testNow, c.Location()) } if len(carbon) > 0 { - end = carbon[len(carbon)-1] + end = carbon[0] } if c.Error != nil || end.Error != nil { return "" diff --git a/outputer.go b/outputer.go index 74b121ee..f8bcbf2b 100644 --- a/outputer.go +++ b/outputer.go @@ -18,7 +18,7 @@ func (c Carbon) String() string { // 输出 "2006-01-02 15:04:05.999999999 -0700 MST" 格式字符串 func (c Carbon) ToString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -30,7 +30,7 @@ func (c Carbon) ToString(timezone ...string) string { // 输出完整月份字符串,支持i18n func (c Carbon) ToMonthString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -51,7 +51,7 @@ func (c Carbon) ToMonthString(timezone ...string) string { // 输出缩写月份字符串,支持i18n func (c Carbon) ToShortMonthString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -72,7 +72,7 @@ func (c Carbon) ToShortMonthString(timezone ...string) string { // 输出完整星期字符串,支持i18n func (c Carbon) ToWeekString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -93,7 +93,7 @@ func (c Carbon) ToWeekString(timezone ...string) string { // 输出缩写星期字符串,支持i18n func (c Carbon) ToShortWeekString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -114,7 +114,7 @@ func (c Carbon) ToShortWeekString(timezone ...string) string { // 输出 "Mon, Jan 2, 2006 3:04 PM" 格式字符串 func (c Carbon) ToDayDateTimeString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -126,7 +126,7 @@ func (c Carbon) ToDayDateTimeString(timezone ...string) string { // 输出 "2006-01-02 15:04:05" 格式字符串 func (c Carbon) ToDateTimeString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -138,7 +138,7 @@ func (c Carbon) ToDateTimeString(timezone ...string) string { // 输出 "2006-01-02 15:04:05.999" 格式字符串 func (c Carbon) ToDateTimeMilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -150,7 +150,7 @@ func (c Carbon) ToDateTimeMilliString(timezone ...string) string { // 输出 "2006-01-02 15:04:05.999999" 格式字符串 func (c Carbon) ToDateTimeMicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -162,7 +162,7 @@ func (c Carbon) ToDateTimeMicroString(timezone ...string) string { // 输出 "2006-01-02 15:04:05.999999999" 格式字符串 func (c Carbon) ToDateTimeNanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -174,7 +174,7 @@ func (c Carbon) ToDateTimeNanoString(timezone ...string) string { // 输出 "20060102150405" 格式字符串 func (c Carbon) ToShortDateTimeString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -186,7 +186,7 @@ func (c Carbon) ToShortDateTimeString(timezone ...string) string { // 输出 "20060102150405.999" 格式字符串 func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -198,7 +198,7 @@ func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string { // 输出 "20060102150405.999999" 格式字符串 func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -210,7 +210,7 @@ func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string { // 输出 "20060102150405.999999999" 格式字符串 func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -222,7 +222,7 @@ func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string { // 输出 "2006-01-02" 格式字符串 func (c Carbon) ToDateString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -234,7 +234,7 @@ func (c Carbon) ToDateString(timezone ...string) string { // 输出 "2006-01-02.999" 格式字符串 func (c Carbon) ToDateMilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -246,7 +246,7 @@ func (c Carbon) ToDateMilliString(timezone ...string) string { // 输出 "2006-01-02.999999" 格式字符串 func (c Carbon) ToDateMicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -258,7 +258,7 @@ func (c Carbon) ToDateMicroString(timezone ...string) string { // 输出 "2006-01-02.999999999" 格式字符串 func (c Carbon) ToDateNanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -270,7 +270,7 @@ func (c Carbon) ToDateNanoString(timezone ...string) string { // 输出 "20060102" 格式字符串 func (c Carbon) ToShortDateString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -282,7 +282,7 @@ func (c Carbon) ToShortDateString(timezone ...string) string { // 输出 "20060102.999" 格式字符串 func (c Carbon) ToShortDateMilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -294,7 +294,7 @@ func (c Carbon) ToShortDateMilliString(timezone ...string) string { // 输出 "20060102.999999" 格式字符串 func (c Carbon) ToShortDateMicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -306,7 +306,7 @@ func (c Carbon) ToShortDateMicroString(timezone ...string) string { // 输出 "20060102.999999999" 格式字符串 func (c Carbon) ToShortDateNanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -318,7 +318,7 @@ func (c Carbon) ToShortDateNanoString(timezone ...string) string { // 输出 "15:04:05" 格式字符串 func (c Carbon) ToTimeString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -330,7 +330,7 @@ func (c Carbon) ToTimeString(timezone ...string) string { // 输出 "15:04:05.999" 格式字符串 func (c Carbon) ToTimeMilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -342,7 +342,7 @@ func (c Carbon) ToTimeMilliString(timezone ...string) string { // 输出 "15:04:05.999999" 格式字符串 func (c Carbon) ToTimeMicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -354,7 +354,7 @@ func (c Carbon) ToTimeMicroString(timezone ...string) string { // 输出 "15:04:05.999999999" 格式字符串 func (c Carbon) ToTimeNanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -366,7 +366,7 @@ func (c Carbon) ToTimeNanoString(timezone ...string) string { // 输出 "150405" 格式字符串 func (c Carbon) ToShortTimeString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -378,7 +378,7 @@ func (c Carbon) ToShortTimeString(timezone ...string) string { // 输出 "150405.999" 格式字符串 func (c Carbon) ToShortTimeMilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -390,7 +390,7 @@ func (c Carbon) ToShortTimeMilliString(timezone ...string) string { // 输出 "150405.999999" 格式字符串 func (c Carbon) ToShortTimeMicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -402,7 +402,7 @@ func (c Carbon) ToShortTimeMicroString(timezone ...string) string { // 输出 "150405.999999999" 格式字符串 func (c Carbon) ToShortTimeNanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -420,7 +420,7 @@ func (c Carbon) ToAtomString(timezone ...string) string { // 输出 "Mon Jan _2 15:04:05 2006" 格式字符串 func (c Carbon) ToANSICString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -432,7 +432,7 @@ func (c Carbon) ToANSICString(timezone ...string) string { // 输出 "Monday, 02-Jan-2006 15:04:05 MST" 格式字符串 func (c Carbon) ToCookieString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -444,7 +444,7 @@ func (c Carbon) ToCookieString(timezone ...string) string { // 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串 func (c Carbon) ToRssString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -462,7 +462,7 @@ func (c Carbon) ToW3cString(timezone ...string) string { // 输出 "Mon Jan _2 15:04:05 MST 2006" 格式字符串 func (c Carbon) ToUnixDateString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -474,7 +474,7 @@ func (c Carbon) ToUnixDateString(timezone ...string) string { // 输出 "Mon Jan 02 15:04:05 -0700 2006" 格式字符串 func (c Carbon) ToRubyDateString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -486,7 +486,7 @@ func (c Carbon) ToRubyDateString(timezone ...string) string { // 输出 "3:04PM" 格式字符串 func (c Carbon) ToKitchenString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -498,7 +498,7 @@ func (c Carbon) ToKitchenString(timezone ...string) string { // 输出 "2006-01-02T15:04:05-07:00" 格式字符串 func (c Carbon) ToIso8601String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -510,7 +510,7 @@ func (c Carbon) ToIso8601String(timezone ...string) string { // 输出 "2006-01-02T15:04:05.999-07:00" 格式字符串 func (c Carbon) ToIso8601MilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -522,7 +522,7 @@ func (c Carbon) ToIso8601MilliString(timezone ...string) string { // 输出 "2006-01-02T15:04:05.999999-07:00" 格式字符串 func (c Carbon) ToIso8601MicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -534,7 +534,7 @@ func (c Carbon) ToIso8601MicroString(timezone ...string) string { // 输出 "2006-01-02T15:04:05.999999999-07:00" 格式字符串 func (c Carbon) ToIso8601NanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -546,7 +546,7 @@ func (c Carbon) ToIso8601NanoString(timezone ...string) string { // 输出 "02 Jan 06 15:04 MST" 格式字符串 func (c Carbon) ToRfc822String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -558,7 +558,7 @@ func (c Carbon) ToRfc822String(timezone ...string) string { // 输出 "02 Jan 06 15:04 -0700" 格式字符串 func (c Carbon) ToRfc822zString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -570,7 +570,7 @@ func (c Carbon) ToRfc822zString(timezone ...string) string { // 输出 "Monday, 02-Jan-06 15:04:05 MST" 格式字符串 func (c Carbon) ToRfc850String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -582,7 +582,7 @@ func (c Carbon) ToRfc850String(timezone ...string) string { // 输出 "Mon, 02 Jan 06 15:04:05 -0700" 格式字符串 func (c Carbon) ToRfc1036String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -594,7 +594,7 @@ func (c Carbon) ToRfc1036String(timezone ...string) string { // 输出 "Mon, 02 Jan 2006 15:04:05 MST" 格式字符串 func (c Carbon) ToRfc1123String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -606,7 +606,7 @@ func (c Carbon) ToRfc1123String(timezone ...string) string { // 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串 func (c Carbon) ToRfc1123zString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -618,7 +618,7 @@ func (c Carbon) ToRfc1123zString(timezone ...string) string { // 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串 func (c Carbon) ToRfc2822String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -630,7 +630,7 @@ func (c Carbon) ToRfc2822String(timezone ...string) string { // 输出 "2006-01-02T15:04:05Z07:00" 格式字符串 func (c Carbon) ToRfc3339String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -642,7 +642,7 @@ func (c Carbon) ToRfc3339String(timezone ...string) string { // 输出 "2006-01-02T15:04:05.999Z07:00" 格式字符串 func (c Carbon) ToRfc3339MilliString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -654,7 +654,7 @@ func (c Carbon) ToRfc3339MilliString(timezone ...string) string { // 输出 "2006-01-02T15:04:05.999999Z07:00" 格式字符串 func (c Carbon) ToRfc3339MicroString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -666,7 +666,7 @@ func (c Carbon) ToRfc3339MicroString(timezone ...string) string { // 输出 "2006-01-02T15:04:05.999999999Z07:00" 格式字符串 func (c Carbon) ToRfc3339NanoString(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -678,7 +678,7 @@ func (c Carbon) ToRfc3339NanoString(timezone ...string) string { // 输出 "Mon, 02 Jan 2006 15:04:05 GMT" 格式字符串 func (c Carbon) ToRfc7231String(timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -690,7 +690,7 @@ func (c Carbon) ToRfc7231String(timezone ...string) string { // 输出指定布局模板的时间字符串 func (c Carbon) ToLayoutString(layout string, timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" @@ -708,7 +708,7 @@ func (c Carbon) Layout(layout string, timezone ...string) string { // 输出指定格式模板的时间字符串 func (c Carbon) ToFormatString(format string, timezone ...string) string { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.IsInvalid() { return "" diff --git a/parser.go b/parser.go index d6715f08..25d98259 100755 --- a/parser.go +++ b/parser.go @@ -11,7 +11,7 @@ func (c Carbon) Parse(value string, timezone ...string) Carbon { return c } if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } switch value { case "now": @@ -58,7 +58,7 @@ func ParseByFormat(value, format string, timezone ...string) Carbon { // 通过布局模板将时间字符串解析成 Carbon 实例 func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c diff --git a/traveler.go b/traveler.go index e03627db..58e50911 100755 --- a/traveler.go +++ b/traveler.go @@ -8,7 +8,7 @@ import ( // 当前 func (c Carbon) Now(timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c @@ -30,7 +30,7 @@ func Now(timezone ...string) Carbon { // 明天 func (c Carbon) Tomorrow(timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c @@ -54,7 +54,7 @@ func Tomorrow(timezone ...string) Carbon { // 昨天 func (c Carbon) Yesterday(timezone ...string) Carbon { if len(timezone) > 0 { - c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1]) + c.loc, c.Error = getLocationByTimezone(timezone[0]) } if c.Error != nil { return c From 9dd8c9de40fb151725dcf802a0c5ba80002fb915 Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Thu, 30 Nov 2023 14:56:34 +0800 Subject: [PATCH 5/5] Fix issue #202 --- helper.go | 9 +++++---- parser_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/helper.go b/helper.go index 3156486f..adb91ee0 100755 --- a/helper.go +++ b/helper.go @@ -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", diff --git a/parser_test.go b/parser_test.go index 1e8f0b3b..f731202c 100755 --- a/parser_test.go +++ b/parser_test.go @@ -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()")