From 25c8ef72c7fea9f82e1a3dfc7d8f0660874ff53d Mon Sep 17 00:00:00 2001 From: Oleg Ozimok Date: Thu, 20 Sep 2018 13:38:01 +0300 Subject: [PATCH 1/2] add float64 to time cast --- cast_test.go | 1 + caste.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/cast_test.go b/cast_test.go index d9b0b01..b5d9f18 100644 --- a/cast_test.go +++ b/cast_test.go @@ -1131,6 +1131,7 @@ func TestToTimeEE(t *testing.T) { {uint(1482597504), time.Date(2016, 12, 24, 16, 38, 24, 0, time.UTC), false}, {uint64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false}, {uint32(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false}, + {float64(1234567890), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false}, {time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), time.Date(2009, 2, 13, 23, 31, 30, 0, time.UTC), false}, // errors {"2006", time.Time{}, true}, diff --git a/caste.go b/caste.go index 4fe1928..c2c02a6 100644 --- a/caste.go +++ b/caste.go @@ -39,6 +39,8 @@ func ToTimeE(i interface{}) (tim time.Time, err error) { return time.Unix(int64(v), 0), nil case uint32: return time.Unix(int64(v), 0), nil + case float64: + return time.Unix(int64(v), 0), nil default: return time.Time{}, fmt.Errorf("unable to cast %#v of type %T to Time", i, i) } From b05689020576cecff615801a7fea003fb38ec11a Mon Sep 17 00:00:00 2001 From: Oleg Ozimok Date: Wed, 26 Sep 2018 15:01:47 +0300 Subject: [PATCH 2/2] support custom timezone for date parsing --- caste.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/caste.go b/caste.go index c2c02a6..22ff907 100644 --- a/caste.go +++ b/caste.go @@ -16,6 +16,8 @@ import ( "time" ) +var TimeLocation = time.UTC + var errNegativeNotAllowed = errors.New("unable to cast negative value") // ToTimeE casts an interface to a time.Time type. @@ -1153,7 +1155,7 @@ func StringToDate(s string) (time.Time, error) { func parseDateWith(s string, dates []string) (d time.Time, e error) { for _, dateType := range dates { - if d, e = time.Parse(dateType, s); e == nil { + if d, e = time.ParseInLocation(dateType, s, TimeLocation); e == nil { return } }