From f670c24e2c4d6d7b95dab63c12647d16e0b65662 Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 21 Mar 2022 22:24:59 +0800 Subject: [PATCH] fix issue #1681 --- os/gtime/gtime.go | 14 ++++++++------ os/gtime/gtime_z_unit_time_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/os/gtime/gtime.go b/os/gtime/gtime.go index cdf3ca156..e3968281c 100644 --- a/os/gtime/gtime.go +++ b/os/gtime/gtime.go @@ -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 { diff --git a/os/gtime/gtime_z_unit_time_test.go b/os/gtime/gtime_z_unit_time_test.go index 31d954d03..350bc8c19 100644 --- a/os/gtime/gtime_z_unit_time_test.go +++ b/os/gtime/gtime_z_unit_time_test.go @@ -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) + }) +}