From e85dcc8b3809ddf4afe858d2ee084c0c82749732 Mon Sep 17 00:00:00 2001 From: Valera Date: Wed, 16 Sep 2020 23:01:43 +0300 Subject: [PATCH 1/5] [bugfix] parse unix timestamp to time.Time from string Fixing https://github.com/spf13/cast/issues/102 --- caste.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/caste.go b/caste.go index 9ac1015..af9e9bc 100644 --- a/caste.go +++ b/caste.go @@ -1273,6 +1273,10 @@ func parseDateWith(s string, dates []string) (d time.Time, e error) { return } } + if i, err := strconv.Atoi(t); err == nil { + return time.Unix(int64(i), 0), nil + } + return d, fmt.Errorf("unable to parse date: %s", s) } From 157e4947bf37c0fc89cddb92c01e85723dd6ba82 Mon Sep 17 00:00:00 2001 From: Valera Date: Wed, 16 Sep 2020 23:04:35 +0300 Subject: [PATCH 2/5] Update caste.go --- caste.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/caste.go b/caste.go index af9e9bc..8f5885e 100644 --- a/caste.go +++ b/caste.go @@ -1272,9 +1272,10 @@ func parseDateWith(s string, dates []string) (d time.Time, e error) { if d, e = time.Parse(dateType, s); e == nil { return } - } - if i, err := strconv.Atoi(t); err == nil { - return time.Unix(int64(i), 0), nil + if i, err := strconv.Atoi(dateType); err == nil { + return time.Unix(int64(i), 0), nil + } + } return d, fmt.Errorf("unable to parse date: %s", s) From 8d351a48273b7e468b7dada1812bc6d1ce0d9791 Mon Sep 17 00:00:00 2001 From: Valera Date: Wed, 16 Sep 2020 23:08:23 +0300 Subject: [PATCH 3/5] Update cast_test.go --- cast_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cast_test.go b/cast_test.go index cc4ebce..8ee2e6d 100644 --- a/cast_test.go +++ b/cast_test.go @@ -1218,6 +1218,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}, + {"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}, From e5ac093b2d03cfd85726e551fc741bed26d1afd3 Mon Sep 17 00:00:00 2001 From: Valeriy Solovyov Date: Wed, 16 Sep 2020 23:29:57 +0300 Subject: [PATCH 4/5] Fixing parsing from string --- cast_test.go | 1 - caste.go | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cast_test.go b/cast_test.go index 8ee2e6d..c54ba26 100644 --- a/cast_test.go +++ b/cast_test.go @@ -1221,7 +1221,6 @@ func TestToTimeEE(t *testing.T) { {"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}, {testing.T{}, time.Time{}, true}, } diff --git a/caste.go b/caste.go index 8f5885e..d196942 100644 --- a/caste.go +++ b/caste.go @@ -1272,11 +1272,10 @@ func parseDateWith(s string, dates []string) (d time.Time, e error) { if d, e = time.Parse(dateType, s); e == nil { return } - if i, err := strconv.Atoi(dateType); err == nil { - return time.Unix(int64(i), 0), nil - } - } + if i, err := strconv.Atoi(s); err == nil { + return time.Unix(int64(i), 0), nil + } return d, fmt.Errorf("unable to parse date: %s", s) } From 6cd11a28a2684d817e45f0aea4240cff414233bb Mon Sep 17 00:00:00 2001 From: Valeriy Solovyov Date: Wed, 16 Sep 2020 23:33:31 +0300 Subject: [PATCH 5/5] Fixing style --- caste.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/caste.go b/caste.go index d196942..e662a35 100644 --- a/caste.go +++ b/caste.go @@ -1273,9 +1273,9 @@ func parseDateWith(s string, dates []string) (d time.Time, e error) { return } } - if i, err := strconv.Atoi(s); err == nil { - return time.Unix(int64(i), 0), nil - } + if i, err := strconv.Atoi(s); err == nil { + return time.Unix(int64(i), 0), nil + } return d, fmt.Errorf("unable to parse date: %s", s) }