mirror of https://github.com/spf13/cast.git
This commit is contained in:
parent
ab1c95bdd4
commit
8d504b7902
|
@ -1230,6 +1230,7 @@ func TestToTimeEE(t *testing.T) {
|
|||
{"2006-01-02", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
|
||||
{"02 Jan 2006", time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC), false},
|
||||
{1472574600, time.Date(2016, 8, 30, 16, 30, 0, 0, time.UTC), false},
|
||||
{"1600285405", time.Date(2020, 9, 16, 19, 43, 25, 0, time.UTC), false},
|
||||
{int(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false},
|
||||
{int64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
||||
{int32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false},
|
||||
|
|
22
caste.go
22
caste.go
|
@ -1275,6 +1275,28 @@ func StringToDate(s string) (time.Time, error) {
|
|||
}
|
||||
|
||||
func parseDateWith(s string, dates []string) (d time.Time, e error) {
|
||||
if len(s) == len("1499979655583057426") { // 19
|
||||
// nano-seconds
|
||||
if nanoSecs, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||
return time.Unix(0, nanoSecs), nil
|
||||
|
||||
}
|
||||
} else if len(s) == len("1499979795437000") { // 16
|
||||
// micro-seconds
|
||||
if microSecs, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||
return time.Unix(0, microSecs*1000), nil
|
||||
|
||||
}
|
||||
} else if len(s) == len("1332151919000") { // 13
|
||||
if miliSecs, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||
return time.Unix(0, miliSecs*1000*1000), nil
|
||||
}
|
||||
} else if len(s) == len("1332151919") { //10
|
||||
if secs, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||
return time.Unix(secs, 0), nil
|
||||
}
|
||||
}
|
||||
|
||||
for _, dateType := range dates {
|
||||
if d, e = time.Parse(dateType, s); e == nil {
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue