From e6dfb719a733a1751b0d4dc3ec21d93bacdff3e0 Mon Sep 17 00:00:00 2001 From: "Christian G. Warden" Date: Wed, 24 Apr 2024 07:08:49 -0500 Subject: [PATCH] Preserve The Specified Timezone In Parse (#633) * Preserve The Specified Timezone In Parse When parsing a string into a Time using the optional timezone parameter, set the location of the Time to the timezone's location so that Format uses that timezone. * Add More Tests for Preserving Timezone Add test when timezone is used to parse a date-time. Add test when timezone is irrelevant because it's a date. --- builtin/builtin.go | 1 + builtin/builtin_test.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/builtin/builtin.go b/builtin/builtin.go index efc01fc2..1199f59c 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -502,6 +502,7 @@ var Builtins = []*Function{ if err != nil { return nil, err } + t = t.In(tz) return t, nil } diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 307d4a86..dca4bccf 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -113,6 +113,9 @@ func TestBuiltin(t *testing.T) { {`duration("1h")`, time.Hour}, {`date("2006-01-02T15:04:05Z")`, time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)}, {`date("2006.01.02", "2006.01.02")`, time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC)}, + {`date("2023-04-23T00:30:00.000+0100", "2006-01-02T15:04:05-0700", "America/Chicago").Format("2006-01-02")`, "2023-04-22"}, + {`date("2023-04-23T00:30:00", "2006-01-02T15:04:05", "America/Chicago").Format("2006-01-02")`, "2023-04-23"}, + {`date("2023-04-23", "2006-01-02", "America/Chicago").Format("2006-01-02")`, "2023-04-23"}, {`first(ArrayOfString)`, "foo"}, {`first(ArrayOfInt)`, 1}, {`first(ArrayOfAny)`, 1},