This commit is contained in:
liushaobo 2021-03-12 23:38:38 +08:00
parent ab1c95bdd4
commit 8d504b7902
3 changed files with 25 additions and 0 deletions

View File

@ -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},

View File

@ -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

2
go.mod
View File

@ -1,5 +1,7 @@
module github.com/spf13/cast
go 1.14
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect