From 8d504b7902ea191d7b0a7a5bcd5e39f26f2d8fdd Mon Sep 17 00:00:00 2001 From: liushaobo Date: Fri, 12 Mar 2021 23:38:38 +0800 Subject: [PATCH] #102 --- cast_test.go | 1 + caste.go | 22 ++++++++++++++++++++++ go.mod | 2 ++ 3 files changed, 25 insertions(+) diff --git a/cast_test.go b/cast_test.go index 270a5ed..acef3e2 100644 --- a/cast_test.go +++ b/cast_test.go @@ -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}, diff --git a/caste.go b/caste.go index 35da14e..c24aab3 100644 --- a/caste.go +++ b/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 diff --git a/go.mod b/go.mod index c1c0232..f0137fd 100644 --- a/go.mod +++ b/go.mod @@ -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