From deead262aecc2500a4ca579d30a038992a6fa234 Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Tue, 23 Jan 2024 03:41:39 +0530 Subject: [PATCH] Allow for timezone from TZ database (#5078) Graph sometimes just send timezone value which are available in TZ database. This PR makes sure that we don't always try to convert, but check if the timezone is already in the format that we need first. --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [x] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) * # #### Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [ ] :green_heart: E2E --------- Co-authored-by: zackrossman --- src/internal/converters/ics/ics.go | 24 ++++++++++++++++-------- src/internal/converters/ics/ics_test.go | 7 +++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/internal/converters/ics/ics.go b/src/internal/converters/ics/ics.go index 4b294f42f..3e8277561 100644 --- a/src/internal/converters/ics/ics.go +++ b/src/internal/converters/ics/ics.go @@ -72,6 +72,11 @@ func getLocationString(location models.Locationable) string { } func GetUTCTime(ts, tz string) (time.Time, error) { + var ( + loc *time.Location + err error + ) + // Timezone is always converted to UTC. This is the easiest way to // ensure we have the correct time as the .ics file expects the same // timezone everywhere according to the spec. @@ -80,15 +85,18 @@ func GetUTCTime(ts, tz string) (time.Time, error) { return time.Time{}, clues.Wrap(err, "parsing time").With("given_time_string", ts) } - timezone, ok := GraphTimeZoneToTZ[tz] - if !ok { - return it, clues.New("unknown timezone").With("timezone", tz) - } - - loc, err := time.LoadLocation(timezone) + loc, err = time.LoadLocation(tz) if err != nil { - return time.Time{}, clues.Wrap(err, "loading timezone"). - With("converted_timezone", timezone) + timezone, ok := GraphTimeZoneToTZ[tz] + if !ok { + return it, clues.New("unknown timezone").With("timezone", tz) + } + + loc, err = time.LoadLocation(timezone) + if err != nil { + return time.Time{}, clues.Wrap(err, "loading timezone"). + With("converted_timezone", timezone) + } } // embed timezone diff --git a/src/internal/converters/ics/ics_test.go b/src/internal/converters/ics/ics_test.go index 60649becd..f1434f615 100644 --- a/src/internal/converters/ics/ics_test.go +++ b/src/internal/converters/ics/ics_test.go @@ -138,6 +138,13 @@ func (suite *ICSUnitSuite) TestGetUTCTime() { time: time.Date(2021, 1, 1, 6, 30, 0, 0, time.UTC), errCheck: require.NoError, }, + { + name: "timezone from TZ database", + timestamp: "2021-01-01T12:00:00Z", + timezone: "America/Los_Angeles", + time: time.Date(2021, 1, 1, 20, 0, 0, 0, time.UTC), + errCheck: require.NoError, + }, { name: "invalid time", timestamp: "invalid",