From cf0ec3c54df04f9494af57298f54341f4984ac8f Mon Sep 17 00:00:00 2001 From: Daniel Ferstay Date: Fri, 22 Oct 2021 17:33:00 -0700 Subject: [PATCH] support ":" as separator for fractional seconds Fixes #137 Signed-off-by: Daniel Ferstay --- parseany.go | 23 +++++++++-------------- parseany_test.go | 1 + 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/parseany.go b/parseany.go index b9668b2..59b02af 100644 --- a/parseany.go +++ b/parseany.go @@ -1173,8 +1173,10 @@ iterRunes: } switch r { case ',': - // hm, lets just swap out comma for period. for some reason go - // won't parse it. + // swap out comma for period. + // Newer versions of Go support comma as a separator, but colon is still unsupported + // https://go-review.googlesource.com/c/go/+/300996 + // // 2014-05-11 08:20:13,787 ds := []byte(p.datestr) ds[i] = '.' @@ -1250,18 +1252,11 @@ iterRunes: p.seci = i + 1 p.minlen = i - p.mini } else if p.seci > 0 { - // 18:31:59:257 ms uses colon, wtf - p.seclen = i - p.seci - p.set(p.seci, "05") - p.msi = i + 1 - - // gross, gross, gross. manipulating the datestr is horrible. - // https://github.com/araddon/dateparse/issues/117 - // Could not get the parsing to work using golang time.Parse() without - // replacing that colon with period. - p.set(i, ".") - datestr = datestr[0:i] + "." + datestr[i+1:] - p.datestr = datestr + // 18:31:59:257 ms is separated with a colon + // swap out the colon for a period and re-parse + ds := []byte(p.datestr) + ds[i] = '.' + return parseTime(string(ds), loc, opts...) } } case timeOffset: diff --git a/parseany_test.go b/parseany_test.go index 7fea1e6..eb44672 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -384,6 +384,7 @@ var testInputs = []dateTest{ {in: "2016-06-21T19:55+0130", out: "2016-06-21 18:25:00 +0000 UTC"}, // yyyy-mm-ddThh:mm:ss:000+0000 - weird format with additional colon in front of milliseconds {in: "2012-08-17T18:31:59:257+0100", out: "2012-08-17 17:31:59.257 +0000 UTC"}, // https://github.com/araddon/dateparse/issues/117 + {in: "2012-08-17T18:31:59:257", out: "2012-08-17 18:31:59.257 +0000 UTC"}, // https://github.com/araddon/dateparse/issues/137 // yyyy-mm-ddThh:mm:ssZ {in: "2009-08-12T22:15Z", out: "2009-08-12 22:15:00 +0000 UTC"},