diff --git a/os/gtime/gtime_format.go b/os/gtime/gtime_format.go index 6e0898e31..a62879131 100644 --- a/os/gtime/gtime_format.go +++ b/os/gtime/gtime_format.go @@ -17,35 +17,35 @@ import ( var ( // Refer: http://php.net/manual/en/function.date.php formats = map[byte]string{ - 'd': "02", // Day: Day of the month, 2 digits with leading zeros. Eg: 01 to 31. - 'D': "Mon", // Day: A textual representation of a day, three letters. Eg: Mon through Sun. - 'w': "Monday", // Day: Numeric representation of the day of the week. Eg: 0 (for Sunday) through 6 (for Saturday). - 'N': "Monday", // Day: ISO-8601 numeric representation of the day of the week. Eg: 1 (for Monday) through 7 (for Sunday). - 'j': "=j=02", // Day: Day of the month without leading zeros. Eg: 1 to 31. - 'S': "02", // Day: English ordinal suffix for the day of the month, 2 characters. Eg: st, nd, rd or th. Works well with j. - 'l': "Monday", // Day: A full textual representation of the day of the week. Eg: Sunday through Saturday. - 'z': "", // Day: The day of the year (starting from 0). Eg: 0 through 365. - 'W': "", // Week: ISO-8601 week number of year, weeks starting on Monday. Eg: 42 (the 42nd week in the year). - 'F': "January", // Month: A full textual representation of a month, such as January or March. Eg: January through December. - 'm': "01", // Month: Numeric representation of a month, with leading zeros. Eg: 01 through 12. - 'M': "Jan", // Month: A short textual representation of a month, three letters. Eg: Jan through Dec. - 'n': "1", // Month: Numeric representation of a month, without leading zeros. Eg: 1 through 12. - 't': "", // Month: Number of days in the given month. Eg: 28 through 31. - 'Y': "2006", // Year: A full numeric representation of a year, 4 digits. Eg: 1999 or 2003. - 'y': "06", // Year: A two digit representation of a year. Eg: 99 or 03. - 'a': "pm", // Time: Lowercase Ante meridiem and Post meridiem. Eg: am or pm. - 'A': "PM", // Time: Uppercase Ante meridiem and Post meridiem. Eg: AM or PM. - 'g': "3", // Time: 12-hour format of an hour without leading zeros. Eg: 1 through 12. - 'G': "=G=15", // Time: 24-hour format of an hour without leading zeros. Eg: 0 through 23. - 'h': "03", // Time: 12-hour format of an hour with leading zeros. Eg: 01 through 12. - 'H': "15", // Time: 24-hour format of an hour with leading zeros. Eg: 00 through 23. - 'i': "04", // Time: Minutes with leading zeros. Eg: 00 to 59. - 's': "05", // Time: Seconds with leading zeros. Eg: 00 through 59. - 'u': "=u=.000", // Time: Milliseconds. Eg: 234, 678. - 'U': "", // Time: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). - 'O': "-0700", // Zone: Difference to Greenwich time (GMT) in hours. Eg: +0200. - 'P': "-07:00", // Zone: Difference to Greenwich time (GMT) with colon between hours and minutes. Eg: +02:00. - 'T': "MST", // Zone: Timezone abbreviation. Eg: UTC, EST, MDT ... + 'd': "02", // Day: Day of the month, 2 digits with leading zeros. Eg: 01 to 31. + 'D': "Mon", // Day: A textual representation of a day, three letters. Eg: Mon through Sun. + 'w': "Monday", // Day: Numeric representation of the day of the week. Eg: 0 (for Sunday) through 6 (for Saturday). + 'N': "Monday", // Day: ISO-8601 numeric representation of the day of the week. Eg: 1 (for Monday) through 7 (for Sunday). + 'j': "=j=02", // Day: Day of the month without leading zeros. Eg: 1 to 31. + 'S': "02", // Day: English ordinal suffix for the day of the month, 2 characters. Eg: st, nd, rd or th. Works well with j. + 'l': "Monday", // Day: A full textual representation of the day of the week. Eg: Sunday through Saturday. + 'z': "", // Day: The day of the year (starting from 0). Eg: 0 through 365. + 'W': "", // Week: ISO-8601 week number of year, weeks starting on Monday. Eg: 42 (the 42nd week in the year). + 'F': "January", // Month: A full textual representation of a month, such as January or March. Eg: January through December. + 'm': "01", // Month: Numeric representation of a month, with leading zeros. Eg: 01 through 12. + 'M': "Jan", // Month: A short textual representation of a month, three letters. Eg: Jan through Dec. + 'n': "1", // Month: Numeric representation of a month, without leading zeros. Eg: 1 through 12. + 't': "", // Month: Number of days in the given month. Eg: 28 through 31. + 'Y': "2006", // Year: A full numeric representation of a year, 4 digits. Eg: 1999 or 2003. + 'y': "06", // Year: A two-digit representation of a year. Eg: 99 or 03. + 'a': "pm", // Time: Lowercase Ante meridiem and Post meridiem. Eg: am or pm. + 'A': "PM", // Time: Uppercase Ante meridiem and Post meridiem. Eg: AM or PM. + 'g': "3", // Time: 12-hour format of an hour without leading zeros. Eg: 1 through 12. + 'G': "=G=15", // Time: 24-hour format of an hour without leading zeros. Eg: 0 through 23. + 'h': "03", // Time: 12-hour format of an hour with leading zeros. Eg: 01 through 12. + 'H': "15", // Time: 24-hour format of an hour with leading zeros. Eg: 00 through 23. + 'i': "04", // Time: Minutes with leading zeros. Eg: 00 to 59. + 's': "05", // Time: Seconds with leading zeros. Eg: 00 through 59. + 'u': "=u=.000", // Time: Milliseconds. Eg: 234, 678. + 'U': "", // Time: Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). + 'O': "-0700", // Zone: Difference to Greenwich time (GMT) in hours. Eg: +0200. + 'P': "-07:00", // Zone: Difference to Greenwich time (GMT) with colon between hours and minutes. Eg: +02:00. + 'T': "MST", // Zone: Timezone abbreviation. Eg: UTC, EST, MDT ... 'c': "2006-01-02T15:04:05-07:00", // Format: ISO 8601 date. Eg: 2004-02-12T15:19:21+00:00. 'r': "Mon, 02 Jan 06 15:04 MST", // Format: RFC 2822 formatted date. Eg: Thu, 21 Dec 2000 16:01:07 +0200. } @@ -159,7 +159,11 @@ func (t *Time) LayoutNew(layout string) *Time { if t == nil { return nil } - return NewFromStr(t.Layout(layout)) + newTime, err := StrToTimeLayout(t.Layout(layout), layout) + if err != nil { + panic(err) + } + return newTime } // LayoutTo formats `t` with stdlib layout. @@ -167,7 +171,11 @@ func (t *Time) LayoutTo(layout string) *Time { if t == nil { return nil } - t.Time = NewFromStr(t.Layout(layout)).Time + newTime, err := StrToTimeLayout(t.Layout(layout), layout) + if err != nil { + panic(err) + } + t.Time = newTime.Time return t } diff --git a/os/gtime/gtime_z_unit_issue_test.go b/os/gtime/gtime_z_unit_issue_test.go new file mode 100644 index 000000000..655fe6b6b --- /dev/null +++ b/os/gtime/gtime_z_unit_issue_test.go @@ -0,0 +1,37 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gtime_test + +import ( + "testing" + + "github.com/gogf/gf/v2/os/gtime" + "github.com/gogf/gf/v2/test/gtest" +) + +// https://github.com/gogf/gf/issues/1681 +func Test_Issue1681(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(gtime.New("2022-03-08T03:01:14-07:00").Local().Time, gtime.New("2022-03-08T10:01:14Z").Local().Time) + t.Assert(gtime.New("2022-03-08T03:01:14-08:00").Local().Time, gtime.New("2022-03-08T11:01:14Z").Local().Time) + t.Assert(gtime.New("2022-03-08T03:01:14-09:00").Local().Time, gtime.New("2022-03-08T12:01:14Z").Local().Time) + t.Assert(gtime.New("2022-03-08T03:01:14+08:00").Local().Time, gtime.New("2022-03-07T19:01:14Z").Local().Time) + }) +} + +// https://github.com/gogf/gf/issues/2803 +func Test_Issue2803(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + newTime := gtime.New("2023-07-26").LayoutTo("2006-01") + t.Assert(newTime.Year(), 2023) + t.Assert(newTime.Month(), 7) + t.Assert(newTime.Day(), 1) + t.Assert(newTime.Hour(), 0) + t.Assert(newTime.Minute(), 0) + t.Assert(newTime.Second(), 0) + }) +} diff --git a/os/gtime/gtime_z_unit_time_test.go b/os/gtime/gtime_z_unit_time_test.go index 05c90248b..9cd64583c 100644 --- a/os/gtime/gtime_z_unit_time_test.go +++ b/os/gtime/gtime_z_unit_time_test.go @@ -8,10 +8,10 @@ package gtime_test import ( "fmt" - "github.com/gogf/gf/v2/internal/json" "testing" "time" + "github.com/gogf/gf/v2/internal/json" "github.com/gogf/gf/v2/os/gtime" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/util/gutil" @@ -523,16 +523,6 @@ func Test_OnlyTime(t *testing.T) { }) } -// https://github.com/gogf/gf/issues/1681 -func Test_Issue1681(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - t.Assert(gtime.New("2022-03-08T03:01:14-07:00").Local().Time, gtime.New("2022-03-08T10:01:14Z").Local().Time) - t.Assert(gtime.New("2022-03-08T03:01:14-08:00").Local().Time, gtime.New("2022-03-08T11:01:14Z").Local().Time) - t.Assert(gtime.New("2022-03-08T03:01:14-09:00").Local().Time, gtime.New("2022-03-08T12:01:14Z").Local().Time) - t.Assert(gtime.New("2022-03-08T03:01:14+08:00").Local().Time, gtime.New("2022-03-07T19:01:14Z").Local().Time) - }) -} - func Test_DeepCopy(t *testing.T) { type User struct { Id int