convert - String2Time accept HH:mm:ss format (#2074)
resolves #2073 Co-authored-by: arturwwl <> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/2074 Co-authored-by: arturwwl <arturwwl@noreply.gitea.com> Co-committed-by: arturwwl <arturwwl@noreply.gitea.com>
This commit is contained in:
parent
3626de1459
commit
24a672be3c
|
@ -15,6 +15,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// String2Time converts a string to time with original location
|
// String2Time converts a string to time with original location
|
||||||
|
// be aware for time strings (HH:mm:ss) returns zero year (LMT) for converted location
|
||||||
func String2Time(s string, originalLocation *time.Location, convertedLocation *time.Location) (*time.Time, error) {
|
func String2Time(s string, originalLocation *time.Location, convertedLocation *time.Location) (*time.Time, error) {
|
||||||
if len(s) == 19 {
|
if len(s) == 19 {
|
||||||
if s == utils.ZeroTime0 || s == utils.ZeroTime1 {
|
if s == utils.ZeroTime0 || s == utils.ZeroTime1 {
|
||||||
|
@ -32,6 +33,7 @@ func String2Time(s string, originalLocation *time.Location, convertedLocation *t
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dt = dt.In(convertedLocation)
|
dt = dt.In(convertedLocation)
|
||||||
|
dt.IsZero()
|
||||||
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(time.RFC3339, s)
|
dt, err := time.Parse(time.RFC3339, s)
|
||||||
|
@ -48,7 +50,7 @@ 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) >= 21 && s[19] == '.' {
|
} else if len(s) >= 21 && s[19] == '.' {
|
||||||
var layout = "2006-01-02 15:04:05." + strings.Repeat("0", len(s)-20)
|
layout := "2006-01-02 15:04:05." + strings.Repeat("0", len(s)-20)
|
||||||
dt, err := time.ParseInLocation(layout, s, originalLocation)
|
dt, err := time.ParseInLocation(layout, s, originalLocation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -65,6 +67,18 @@ 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] == ':' {
|
||||||
|
currentDate := time.Now()
|
||||||
|
dt, err := time.ParseInLocation("15:04:05", s, originalLocation)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// add current date for correct time locations
|
||||||
|
dt = dt.AddDate(currentDate.Year(), int(currentDate.Month()), currentDate.Day())
|
||||||
|
dt = dt.In(convertedLocation)
|
||||||
|
// back to zero year
|
||||||
|
dt = dt.AddDate(-currentDate.Year(), int(-currentDate.Month()), -currentDate.Day())
|
||||||
|
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 {
|
||||||
|
|
|
@ -15,7 +15,7 @@ func TestString2Time(t *testing.T) {
|
||||||
expectedLoc, err := time.LoadLocation("Asia/Shanghai")
|
expectedLoc, err := time.LoadLocation("Asia/Shanghai")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
var kases = 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),
|
||||||
"2021-07-11 10:44:00": time.Date(2021, 7, 11, 18, 44, 0, 0, expectedLoc),
|
"2021-07-11 10:44:00": time.Date(2021, 7, 11, 18, 44, 0, 0, expectedLoc),
|
||||||
"2021-07-11 10:44:00.999": time.Date(2021, 7, 11, 18, 44, 0, 999000000, expectedLoc),
|
"2021-07-11 10:44:00.999": time.Date(2021, 7, 11, 18, 44, 0, 999000000, expectedLoc),
|
||||||
|
@ -25,12 +25,13 @@ func TestString2Time(t *testing.T) {
|
||||||
"2021-06-06T22:58:20.999+08:00": time.Date(2021, 6, 6, 22, 58, 20, 999000000, expectedLoc),
|
"2021-06-06T22:58:20.999+08:00": time.Date(2021, 6, 6, 22, 58, 20, 999000000, expectedLoc),
|
||||||
"2021-06-06T22:58:20.999999+08:00": time.Date(2021, 6, 6, 22, 58, 20, 999999000, expectedLoc),
|
"2021-06-06T22:58:20.999999+08:00": time.Date(2021, 6, 6, 22, 58, 20, 999999000, expectedLoc),
|
||||||
"2021-06-06T22:58:20.999999999+08:00": time.Date(2021, 6, 6, 22, 58, 20, 999999999, expectedLoc),
|
"2021-06-06T22:58:20.999999999+08:00": time.Date(2021, 6, 6, 22, 58, 20, 999999999, expectedLoc),
|
||||||
"2021-08-10T10:33:04Z": time.Date(2021, 8, 10, 18, 33, 04, 0, expectedLoc),
|
"2021-08-10T10:33:04Z": time.Date(2021, 8, 10, 18, 33, 0o4, 0, expectedLoc),
|
||||||
"2021-08-10T10:33:04.999Z": time.Date(2021, 8, 10, 18, 33, 04, 999000000, expectedLoc),
|
"2021-08-10T10:33:04.999Z": time.Date(2021, 8, 10, 18, 33, 0o4, 999000000, expectedLoc),
|
||||||
"2021-08-10T10:33:04.999999Z": time.Date(2021, 8, 10, 18, 33, 04, 999999000, expectedLoc),
|
"2021-08-10T10:33:04.999999Z": time.Date(2021, 8, 10, 18, 33, 0o4, 999999000, expectedLoc),
|
||||||
"2021-08-10T10:33:04.999999999Z": time.Date(2021, 8, 10, 18, 33, 04, 999999999, expectedLoc),
|
"2021-08-10T10:33:04.999999999Z": time.Date(2021, 8, 10, 18, 33, 0o4, 999999999, expectedLoc),
|
||||||
|
"10:22:33": time.Date(0, 1, 1, 18, 22, 33, 0, expectedLoc),
|
||||||
}
|
}
|
||||||
for layout, tm := range kases {
|
for layout, tm := range cases {
|
||||||
t.Run(layout, func(t *testing.T) {
|
t.Run(layout, func(t *testing.T) {
|
||||||
target, err := String2Time(layout, time.UTC, expectedLoc)
|
target, err := String2Time(layout, time.UTC, expectedLoc)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
Loading…
Reference in New Issue