From 98098e0fc0c5005ab34a9c96547860452e55b4a2 Mon Sep 17 00:00:00 2001 From: Tom <53711814+tbal999@users.noreply.github.com> Date: Wed, 20 Mar 2024 09:37:03 +0000 Subject: [PATCH] fix bug - 000 end to timestamp (#33) * remove the 000 timestmp * remove wednesday checking --------- Co-authored-by: tbal999 --- jlib/timeparse/testdata.json | 18 +++---- jlib/timeparse/testdata_lite.json | 84 +++++++++++++++++++++++++------ jlib/timeparse/timeparse.go | 12 ++--- jlib/timeparse/timeparse_test.go | 3 +- jlib/timeparse/timeparselite.go | 5 +- 5 files changed, 90 insertions(+), 32 deletions(-) diff --git a/jlib/timeparse/testdata.json b/jlib/timeparse/testdata.json index f97c929..c239026 100644 --- a/jlib/timeparse/testdata.json +++ b/jlib/timeparse/testdata.json @@ -30,7 +30,7 @@ { "testDesc": "/* source explicit UTC, Local = 2023-08-06T00:23:41.454Z+01:00 */", "input_srcTs": "2023-08-06T00:23:41.454Z", - "input_srcFormat": "2006-01-02T15:04:05Z", + "input_srcFormat": "2006-01-02T15:04:05.000Z", "input_srcTz": "UTC", "output_srcTz": "Europe/London", "DateDim": { @@ -42,7 +42,7 @@ "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806012341000, + "DateTimeKey": 20230806012341454, "HourId": "Hours_2023080601", "HourKey": 2023080601, "Millis": 1691281421454, @@ -70,7 +70,7 @@ "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806002341000, + "DateTimeKey": 20230806002341454, "HourId": "Hours_2023080600", "HourKey": 2023080600, "Millis": 1691277821454, @@ -98,7 +98,7 @@ "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806002341000, + "DateTimeKey": 20230806002341454, "HourId": "Hours_2023080600", "HourKey": 2023080600, "Millis": 1691277821454, @@ -121,12 +121,12 @@ "TimeZone": "America/New_York", "TimeZoneOffset": "-04:00", "YearMonth": 202308, - "YearWeek": 202330, + "YearWeek": 202331, "YearIsoWeek": 202331, "YearDay": 2023217, "DateId": "Dates_20230805", "DateKey": 20230805, - "DateTimeKey": 20230805192341000, + "DateTimeKey": 20230805192341454, "HourId": "Hours_2023080519", "HourKey": 2023080519, "Millis": 1691277821454, @@ -154,7 +154,7 @@ "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806002341000, + "DateTimeKey": 20230806002341454, "HourId": "Hours_2023080600", "HourKey": 2023080600, "Millis": 1691277821454, @@ -177,12 +177,12 @@ "TimeZone": "Europe/London", "TimeZoneOffset": "+00:00", "YearMonth": 202301, - "YearWeek": 202300, + "YearWeek": 202301, "YearIsoWeek": 202301, "YearDay": 2023006, "DateId": "Dates_20230106", "DateKey": 20230106, - "DateTimeKey": 20230106002341000, + "DateTimeKey": 20230106002341454, "HourId": "Hours_2023010600", "HourKey": 2023010600, "Millis": 1672964621454, diff --git a/jlib/timeparse/testdata_lite.json b/jlib/timeparse/testdata_lite.json index 455db4c..c239026 100644 --- a/jlib/timeparse/testdata_lite.json +++ b/jlib/timeparse/testdata_lite.json @@ -8,35 +8,51 @@ "DateDim": { "TimeZone": "UTC", "TimeZoneOffset": "+00:00", + "YearMonth": 202308, + "YearWeek": 202330, + "YearIsoWeek": 202331, + "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, "DateTimeKey": 20230806002341000, + "HourId": "Hours_2023080600", + "HourKey": 2023080600, "Millis": 1691281421000, "RawValue": "2023-08-06T00:23:41Z", "UTC": "2023-08-06T00:23:41.000Z", "DateUTC": "2023-08-06", + "HourUTC": 0, "Local": "2023-08-06T00:23:41.000+00:00", - "DateLocal": "2023-08-06" + "DateLocal": "2023-08-06", + "HourLocal": 0 } }, { "testDesc": "/* source explicit UTC, Local = 2023-08-06T00:23:41.454Z+01:00 */", "input_srcTs": "2023-08-06T00:23:41.454Z", - "input_srcFormat": "2006-01-02T15:04:05Z", + "input_srcFormat": "2006-01-02T15:04:05.000Z", "input_srcTz": "UTC", "output_srcTz": "Europe/London", "DateDim": { "TimeZone": "Europe/London", "TimeZoneOffset": "+01:00", + "YearMonth": 202308, + "YearWeek": 202330, + "YearIsoWeek": 202331, + "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806012341000, + "DateTimeKey": 20230806012341454, + "HourId": "Hours_2023080601", + "HourKey": 2023080601, "Millis": 1691281421454, "RawValue": "2023-08-06T00:23:41.454Z", "UTC": "2023-08-06T00:23:41.454Z", "DateUTC": "2023-08-06", + "HourUTC": 0, "Local": "2023-08-06T01:23:41.454+01:00", - "DateLocal": "2023-08-06" + "DateLocal": "2023-08-06", + "HourLocal": 1 } }, { @@ -48,15 +64,23 @@ "DateDim": { "TimeZone": "Europe/London", "TimeZoneOffset": "+01:00", + "YearMonth": 202308, + "YearWeek": 202330, + "YearIsoWeek": 202331, + "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806002341000, + "DateTimeKey": 20230806002341454, + "HourId": "Hours_2023080600", + "HourKey": 2023080600, "Millis": 1691277821454, "RawValue": "2023-08-06T00:23:41.454+01:00", "UTC": "2023-08-05T23:23:41.454Z", "DateUTC": "2023-08-05", + "HourUTC": 23, "Local": "2023-08-06T00:23:41.454+01:00", - "DateLocal": "2023-08-06" + "DateLocal": "2023-08-06", + "HourLocal": 0 } }, { @@ -68,15 +92,23 @@ "DateDim": { "TimeZone": "Europe/London", "TimeZoneOffset": "+01:00", + "YearMonth": 202308, + "YearWeek": 202330, + "YearIsoWeek": 202331, + "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806002341000, + "DateTimeKey": 20230806002341454, + "HourId": "Hours_2023080600", + "HourKey": 2023080600, "Millis": 1691277821454, "RawValue": "2023-08-06T00:23:41.454Z", "UTC": "2023-08-05T23:23:41.454Z", "DateUTC": "2023-08-05", + "HourUTC": 23, "Local": "2023-08-06T00:23:41.454+01:00", - "DateLocal": "2023-08-06" + "DateLocal": "2023-08-06", + "HourLocal": 0 } }, { @@ -88,15 +120,23 @@ "DateDim": { "TimeZone": "America/New_York", "TimeZoneOffset": "-04:00", + "YearMonth": 202308, + "YearWeek": 202331, + "YearIsoWeek": 202331, + "YearDay": 2023217, "DateId": "Dates_20230805", "DateKey": 20230805, - "DateTimeKey": 20230805192341000, + "DateTimeKey": 20230805192341454, + "HourId": "Hours_2023080519", + "HourKey": 2023080519, "Millis": 1691277821454, "RawValue": "2023-08-06T00:23:41.454Z", "UTC": "2023-08-05T23:23:41.454Z", "DateUTC": "2023-08-05", + "HourUTC": 23, "Local": "2023-08-05T19:23:41.454-04:00", - "DateLocal": "2023-08-05" + "DateLocal": "2023-08-05", + "HourLocal": 19 } }, { @@ -108,15 +148,23 @@ "DateDim": { "TimeZone": "Europe/London", "TimeZoneOffset": "+01:00", + "YearMonth": 202308, + "YearWeek": 202330, + "YearIsoWeek": 202331, + "YearDay": 2023218, "DateId": "Dates_20230806", "DateKey": 20230806, - "DateTimeKey": 20230806002341000, + "DateTimeKey": 20230806002341454, + "HourId": "Hours_2023080600", + "HourKey": 2023080600, "Millis": 1691277821454, "RawValue": "2023-08-06T00:23:41.454", "UTC": "2023-08-05T23:23:41.454Z", "DateUTC": "2023-08-05", + "HourUTC": 23, "Local": "2023-08-06T00:23:41.454+01:00", - "DateLocal": "2023-08-06" + "DateLocal": "2023-08-06", + "HourLocal": 0 } }, { @@ -128,15 +176,23 @@ "DateDim": { "TimeZone": "Europe/London", "TimeZoneOffset": "+00:00", + "YearMonth": 202301, + "YearWeek": 202301, + "YearIsoWeek": 202301, + "YearDay": 2023006, "DateId": "Dates_20230106", "DateKey": 20230106, - "DateTimeKey": 20230106002341000, + "DateTimeKey": 20230106002341454, + "HourId": "Hours_2023010600", + "HourKey": 2023010600, "Millis": 1672964621454, "RawValue": "2023-01-06T00:23:41.454", "UTC": "2023-01-06T00:23:41.454Z", "DateUTC": "2023-01-06", + "HourUTC": 0, "Local": "2023-01-06T00:23:41.454+00:00", - "DateLocal": "2023-01-06" + "DateLocal": "2023-01-06", + "HourLocal": 0 } } ] \ No newline at end of file diff --git a/jlib/timeparse/timeparse.go b/jlib/timeparse/timeparse.go index e40af54..179f1da 100644 --- a/jlib/timeparse/timeparse.go +++ b/jlib/timeparse/timeparse.go @@ -3,6 +3,7 @@ package timeparse import ( "fmt" "strconv" + "strings" "time" ) @@ -95,7 +96,9 @@ func TimeDateDimensions(inputSrcTs, inputSrcFormat, inputSrcTz, requiredTz strin return nil, err } - dateTimeID := localTime.Format("20060102150405000") + dateTimeID := localTime.Format("20060102150405.000") + + dateTimeID = strings.ReplaceAll(dateTimeID, ".", "") dateTimeKeyInt, err := strconv.Atoi(dateTimeID) if err != nil { @@ -138,12 +141,7 @@ func TimeDateDimensions(inputSrcTs, inputSrcFormat, inputSrcTz, requiredTz strin func getWeekOfYearString(date time.Time) (int, error) { _, week := date.ISOWeek() - firstWednesday := date.AddDate(0, 0, -int(date.Weekday())+1) - if firstWednesday.Weekday() != time.Wednesday { - firstWednesday = firstWednesday.AddDate(0, 0, 7-int(firstWednesday.Weekday())+int(time.Wednesday)) - } - - if date.Weekday() == time.Sunday || date.Before(firstWednesday) { + if date.Weekday() == time.Sunday { week-- } diff --git a/jlib/timeparse/timeparse_test.go b/jlib/timeparse/timeparse_test.go index f3e4be2..c7efc5e 100644 --- a/jlib/timeparse/timeparse_test.go +++ b/jlib/timeparse/timeparse_test.go @@ -1,10 +1,11 @@ package timeparse_test import ( - "github.com/goccy/go-json" "os" "testing" + "github.com/goccy/go-json" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" jsonatatime "github.com/xiatechs/jsonata-go/jlib/timeparse" diff --git a/jlib/timeparse/timeparselite.go b/jlib/timeparse/timeparselite.go index dd4318a..72afb75 100644 --- a/jlib/timeparse/timeparselite.go +++ b/jlib/timeparse/timeparselite.go @@ -2,6 +2,7 @@ package timeparse import ( "strconv" + "strings" "time" ) @@ -76,7 +77,9 @@ func TimeDateDimensionsLite(inputSrcTs, inputSrcFormat, inputSrcTz, requiredTz s } // Input time stamp TIME values (we confirmed there need to be a seperate set of UTC values) - dateTimeID := localTime.Format("20060102150405000") + dateTimeID := localTime.Format("20060102150405.000") + + dateTimeID = strings.ReplaceAll(dateTimeID, ".", "") dateTimeKeyInt, err := strconv.Atoi(dateTimeID) if err != nil {