2021-07-04 13:23:17 +00:00
|
|
|
// 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 (
|
|
|
|
"fmt"
|
2021-07-18 16:21:46 +00:00
|
|
|
"strconv"
|
2021-07-04 13:23:17 +00:00
|
|
|
"time"
|
2021-07-19 05:43:53 +00:00
|
|
|
|
|
|
|
"xorm.io/xorm/internal/utils"
|
2021-07-04 13:23:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// String2Time converts a string to time with original location
|
|
|
|
func String2Time(s string, originalLocation *time.Location, convertedLocation *time.Location) (*time.Time, error) {
|
|
|
|
if len(s) == 19 {
|
2021-07-19 05:43:53 +00:00
|
|
|
if s == utils.ZeroTime0 || s == utils.ZeroTime1 {
|
|
|
|
return &time.Time{}, nil
|
|
|
|
}
|
2021-07-04 13:23:17 +00:00
|
|
|
dt, err := time.ParseInLocation("2006-01-02 15:04:05", s, originalLocation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dt = dt.In(convertedLocation)
|
|
|
|
return &dt, nil
|
|
|
|
} else if len(s) == 20 && s[10] == 'T' && s[19] == 'Z' {
|
2021-07-18 16:21:46 +00:00
|
|
|
dt, err := time.ParseInLocation("2006-01-02T15:04:05", s[:19], originalLocation)
|
2021-07-11 12:05:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dt = dt.In(convertedLocation)
|
|
|
|
return &dt, nil
|
|
|
|
} else if len(s) == 25 && s[10] == 'T' && s[19] == '+' && s[22] == ':' {
|
|
|
|
dt, err := time.Parse(time.RFC3339, s)
|
2021-07-04 13:23:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dt = dt.In(convertedLocation)
|
|
|
|
return &dt, nil
|
2021-07-18 16:21:46 +00:00
|
|
|
} else {
|
|
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
tm := time.Unix(i, 0).In(convertedLocation)
|
|
|
|
return &tm, nil
|
|
|
|
}
|
2021-07-04 13:23:17 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unsupported convertion from %s to time", s)
|
|
|
|
}
|