This commit is contained in:
John Guo 2022-03-21 22:24:59 +08:00
parent f2e1f63396
commit f670c24e2c
2 changed files with 18 additions and 6 deletions

View File

@ -295,17 +295,19 @@ func StrToTime(str string, format ...string) (*Time, error) {
if h > 24 || m > 59 || s > 59 {
return nil, gerror.NewCodef(gcode.CodeInvalidParameter, `invalid zone string "%s"`, match[6])
}
operation := match[5]
if operation != "+" && operation != "-" {
operation = "-"
}
// Comparing the given time zone whether equals to current time zone,
// it converts it to UTC if they does not equal.
// it converts it to UTC if they do not equal.
_, localOffset := time.Now().Zone()
// Comparing in seconds.
if (h*3600 + m*60 + s) != localOffset {
if (h*3600+m*60+s) != localOffset ||
(localOffset > 0 && operation == "-") ||
(localOffset < 0 && operation == "+") {
local = time.UTC
// UTC conversion.
operation := match[5]
if operation != "+" && operation != "-" {
operation = "-"
}
switch operation {
case "+":
if h > 0 {

View File

@ -397,3 +397,13 @@ func Test_OnlyTime(t *testing.T) {
t.Assert(obj.String(), "18:24:06")
})
}
// 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)
})
}