Skip to content

Commit

Permalink
os/gtime: fix #3558 time zone issues (#3561)
Browse files Browse the repository at this point in the history
  • Loading branch information
wln32 committed May 10, 2024
1 parent 74338c4 commit 0765741
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
34 changes: 6 additions & 28 deletions os/gtime/gtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,36 +277,14 @@ func StrToTime(str string, format ...string) (*Time, error) {
operation = "-"
}
// Comparing the given time zone whether equals to current time zone,
// it converts it to UTC if they do not equal.
_, localOffset := time.Now().Zone()
zoneOffset := h*3600 + m*60 + s
if operation == "-" {
zoneOffset = -zoneOffset
}
// Comparing in seconds.
if (h*3600+m*60+s) != localOffset ||
(localOffset > 0 && operation == "-") ||
(localOffset < 0 && operation == "+") {
local = time.UTC
// UTC conversion.
switch operation {
case "+":
if h > 0 {
hour -= h
}
if m > 0 {
min -= m
}
if s > 0 {
sec -= s
}
case "-":
if h > 0 {
hour += h
}
if m > 0 {
min += m
}
if s > 0 {
sec += s
}
}
if localOffset != zoneOffset {
local = time.FixedZone("", zoneOffset)
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions os/gtime/gtime_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gtime_test

import (
"testing"
"time"

"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/test/gtest"
Expand Down Expand Up @@ -35,3 +36,38 @@ func Test_Issue2803(t *testing.T) {
t.Assert(newTime.Second(), 0)
})
}

// https://github.com/gogf/gf/issues/3558
func Test_Issue3558(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
timeStr := "1880-10-24T00:00:00+08:05"
gfTime := gtime.NewFromStr(timeStr)
t.Assert(gfTime.Year(), 1880)
t.Assert(gfTime.Month(), 10)
t.Assert(gfTime.Day(), 24)
t.Assert(gfTime.Hour(), 0)
t.Assert(gfTime.Minute(), 0)
t.Assert(gfTime.Second(), 0)

stdTime, err := time.Parse(time.RFC3339, timeStr)
t.AssertNil(err)
stdTimeFormat := stdTime.Format("2006-01-02 15:04:05")
gfTimeFormat := gfTime.Format("Y-m-d H:i:s")
t.Assert(gfTimeFormat, stdTimeFormat)
})
gtest.C(t, func(t *gtest.T) {
timeStr := "1880-10-24T00:00:00-08:05"
gfTime := gtime.NewFromStr(timeStr)
t.Assert(gfTime.Year(), 1880)
t.Assert(gfTime.Month(), 10)
t.Assert(gfTime.Day(), 24)
t.Assert(gfTime.Hour(), 0)
t.Assert(gfTime.Minute(), 0)
t.Assert(gfTime.Second(), 0)
stdTime, err := time.Parse(time.RFC3339, timeStr)
t.AssertNil(err)
stdTimeFormat := stdTime.Format("2006-01-02 15:04:05")
gfTimeFormat := gfTime.Format("Y-m-d H:i:s")
t.Assert(gfTimeFormat, stdTimeFormat)
})
}
19 changes: 13 additions & 6 deletions os/gtime/gtime_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,26 @@ func Test_StrToTime(t *testing.T) {
"2006.01.02 15:04:05.000",
"2006.01.02 - 15:04:05",
"2006.01.02 15:04:05 +0800 CST",
"2006-01-02T20:05:06+05:01:01",
"2006-01-02T14:03:04Z01:01:01",
"2006-01-02T15:04:05Z",
"2006-01-02T12:05:05+05:01",
"2006-01-02T02:03:05-05:01",
"2006-01-02T15:04:05",
"02-jan-2006 15:04:05",
"02/jan/2006 15:04:05",
"02.jan.2006 15:04:05",
"02.jan.2006:15:04:05",
}

for _, item := range testDateTimes {
// Save the previous time zone
local := *time.Local
defer func() {
*time.Local = local
}()
time.Local = time.FixedZone("Asia/Shanghai", 8*3600)

for i, item := range testDateTimes {
timeTemp, err := gtime.StrToTime(item)
t.AssertNil(err)
t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05")
t.Log(i)
t.Assert(timeTemp.Time.Local().Format("2006-01-02 15:04:05"), "2006-01-02 15:04:05")
}

// Correct date string,.
Expand Down

0 comments on commit 0765741

Please sign in to comment.