mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
fix issue #1681
This commit is contained in:
parent
f2e1f63396
commit
f670c24e2c
@ -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])
|
||||
}
|
||||
// Comparing the given time zone whether equals to current time zone,
|
||||
// it converts it to UTC if they does not equal.
|
||||
_, localOffset := time.Now().Zone()
|
||||
// Comparing in seconds.
|
||||
if (h*3600 + m*60 + s) != localOffset {
|
||||
local = time.UTC
|
||||
// UTC conversion.
|
||||
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 do not equal.
|
||||
_, localOffset := time.Now().Zone()
|
||||
// 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 {
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user