Fix string2time
This commit is contained in:
parent
a320eff05f
commit
2287b1429f
|
@ -19,14 +19,14 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
return &dt, nil
|
return &dt, nil
|
||||||
} else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' {
|
} else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' {
|
||||||
dt, err := time.ParseInLocation("2006-01-02T15:04:05Z", s, originalLocation)
|
dt, err := time.ParseInLocation(time.RFC3339, s, originalLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
return &dt, nil
|
return &dt, nil
|
||||||
} else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' {
|
} else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' {
|
||||||
dt, err := time.Parse("2006-01-02T15:04:05+07:00", s)
|
dt, err := time.Parse(time.RFC3339, s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
// Copyright 2021 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package convert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestString2Time(t *testing.T) {
|
||||||
|
var kases = map[string]time.Time{
|
||||||
|
"2021-06-06T22:58:20+08:00": time.Date(2021, 6, 6, 22, 58, 20, 0, time.Local),
|
||||||
|
"2021-07-11 10:44:00": time.Date(2021, 7, 11, 18, 44, 0, 0, time.Local),
|
||||||
|
"2021-08-10T10:33:04Z": time.Date(2021, 8, 10, 18, 33, 04, 0, time.Local),
|
||||||
|
}
|
||||||
|
for layout, tm := range kases {
|
||||||
|
t.Run(layout, func(t *testing.T) {
|
||||||
|
target, err := String2Time(layout, time.UTC, time.Local)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.EqualValues(t, tm, *target)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue