Fix time test

This commit is contained in:
Lunny Xiao 2023-10-30 12:20:57 +08:00
parent a4f9c21c17
commit 10b89a89c0
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 3 additions and 8 deletions

View File

@ -68,17 +68,13 @@ 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) == 8 && s[2] == ':' && s[5] == ':' { } else if len(s) == 8 && s[2] == ':' && s[5] == ':' {
currentDate := time.Now()
dt, err := time.ParseInLocation("15:04:05", s, originalLocation) dt, err := time.ParseInLocation("15:04:05", s, originalLocation)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// add current date for correct time locations
dt = dt.AddDate(currentDate.Year(), int(currentDate.Month()), currentDate.Day())
dt = dt.In(convertedLocation) dt = dt.In(convertedLocation)
// back to zero year res := time.Date(0, time.January, 1, dt.Hour(), dt.Minute(), dt.Second(), 0, convertedLocation)
dt = dt.AddDate(-currentDate.Year(), int(-currentDate.Month()), -currentDate.Day()) return &res, nil
return &dt, nil
} else { } else {
i, err := strconv.ParseInt(s, 10, 64) i, err := strconv.ParseInt(s, 10, 64)
if err == nil { if err == nil {

View File

@ -12,8 +12,7 @@ import (
) )
func TestString2Time(t *testing.T) { func TestString2Time(t *testing.T) {
expectedLoc, err := time.LoadLocation("Asia/Shanghai") expectedLoc := time.FixedZone("CST", 8*3600)
assert.NoError(t, err)
cases := map[string]time.Time{ cases := map[string]time.Time{
"2021-08-10": time.Date(2021, 8, 10, 8, 0, 0, 0, expectedLoc), "2021-08-10": time.Date(2021, 8, 10, 8, 0, 0, 0, expectedLoc),