github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/timeutil/time_zone_util_test.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package timeutil 12 13 import ( 14 "fmt" 15 "testing" 16 "time" 17 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestTimeZoneStringToLocation(t *testing.T) { 23 aus, err := time.LoadLocation("Australia/Sydney") 24 require.NoError(t, err) 25 26 testCases := []struct { 27 tz string 28 std TimeZoneStringToLocationStandard 29 loc *time.Location 30 expectedResult bool 31 }{ 32 {"UTC", TimeZoneStringToLocationISO8601Standard, time.UTC, true}, 33 {"Australia/Sydney", TimeZoneStringToLocationISO8601Standard, aus, true}, 34 {"fixed offset:3600 (3600)", TimeZoneStringToLocationISO8601Standard, FixedOffsetTimeZoneToLocation(3600, "3600"), true}, 35 {`GMT-3:00`, TimeZoneStringToLocationISO8601Standard, FixedOffsetTimeZoneToLocation(3*60*60, "GMT-3:00"), true}, 36 {"+10", TimeZoneStringToLocationISO8601Standard, FixedOffsetTimeZoneToLocation(10*60*60, "+10"), true}, 37 {"-10", TimeZoneStringToLocationISO8601Standard, FixedOffsetTimeZoneToLocation(-10*60*60, "-10"), true}, 38 {" +10", TimeZoneStringToLocationISO8601Standard, FixedOffsetTimeZoneToLocation(10*60*60, " +10"), true}, 39 {" -10 ", TimeZoneStringToLocationISO8601Standard, FixedOffsetTimeZoneToLocation(-10*60*60, " -10 "), true}, 40 {"-10:30", TimeZoneStringToLocationISO8601Standard, FixedOffsetTimeZoneToLocation((10*60*60 + 30*60), "-10:30"), true}, 41 {"10:30", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(-(10*60*60 + 30*60), "10:30"), true}, 42 {"asdf", TimeZoneStringToLocationISO8601Standard, nil, false}, 43 44 {"UTC", TimeZoneStringToLocationPOSIXStandard, time.UTC, true}, 45 {"Australia/Sydney", TimeZoneStringToLocationPOSIXStandard, aus, true}, 46 {"fixed offset:3600 (3600)", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(3600, "3600"), true}, 47 {`GMT-3:00`, TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(3*60*60, "GMT-3:00"), true}, 48 {"+10", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(-10*60*60, "+10"), true}, 49 {"-10", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(10*60*60, "-10"), true}, 50 {" +10", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(-10*60*60, " +10"), true}, 51 {" -10", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(10*60*60, " -10"), true}, 52 {"-10:30", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation((10*60*60 + 30*60), "-10:30"), true}, 53 {"10:30", TimeZoneStringToLocationPOSIXStandard, FixedOffsetTimeZoneToLocation(-(10*60*60 + 30*60), "10:30"), true}, 54 {"asdf", TimeZoneStringToLocationPOSIXStandard, nil, false}, 55 } 56 57 for _, tc := range testCases { 58 t.Run(fmt.Sprintf("%s_%d", tc.tz, tc.std), func(t *testing.T) { 59 loc, err := TimeZoneStringToLocation(tc.tz, tc.std) 60 if tc.expectedResult { 61 assert.NoError(t, err) 62 assert.Equal(t, tc.loc, loc) 63 } else { 64 assert.Error(t, err) 65 } 66 }) 67 } 68 } 69 70 func TestTimeZoneOffsetStringConversion(t *testing.T) { 71 testCases := []struct { 72 timezone string 73 std TimeZoneStringToLocationStandard 74 offsetSecs int64 75 ok bool 76 }{ 77 {`10`, TimeZoneStringToLocationPOSIXStandard, -10 * 60 * 60, true}, 78 {`-10`, TimeZoneStringToLocationPOSIXStandard, 10 * 60 * 60, true}, 79 80 {`10`, TimeZoneStringToLocationISO8601Standard, 10 * 60 * 60, true}, 81 {`-10`, TimeZoneStringToLocationISO8601Standard, -10 * 60 * 60, true}, 82 {`10:15`, TimeZoneStringToLocationISO8601Standard, -(10*60*60 + 15*60), true}, 83 {`+10:15`, TimeZoneStringToLocationISO8601Standard, -(10*60*60 + 15*60), true}, 84 {`-10:15`, TimeZoneStringToLocationISO8601Standard, (10*60*60 + 15*60), true}, 85 {`GMT+00:00`, TimeZoneStringToLocationISO8601Standard, 0, true}, 86 {`UTC-1:00:00`, TimeZoneStringToLocationISO8601Standard, 3600, true}, 87 {`UTC-1:0:00`, TimeZoneStringToLocationISO8601Standard, 3600, true}, 88 {`UTC+15:59:0`, TimeZoneStringToLocationISO8601Standard, -57540, true}, 89 {` GMT +167:59:00 `, TimeZoneStringToLocationISO8601Standard, -604740, true}, 90 {`GMT-15:59:59`, TimeZoneStringToLocationISO8601Standard, 57599, true}, 91 {`GMT-06:59`, TimeZoneStringToLocationISO8601Standard, 25140, true}, 92 {`GMT+167:59:00`, TimeZoneStringToLocationISO8601Standard, -604740, true}, 93 {`GMT+ 167: 59:0`, TimeZoneStringToLocationISO8601Standard, -604740, true}, 94 {`GMT+5`, TimeZoneStringToLocationISO8601Standard, -18000, true}, 95 {`UTC+5:9`, TimeZoneStringToLocationISO8601Standard, -(5*60*60 + 9*60), true}, 96 {`UTC-5:9:1`, TimeZoneStringToLocationISO8601Standard, (5*60*60 + 9*60 + 1), true}, 97 {`GMT-15:59:5Z`, TimeZoneStringToLocationISO8601Standard, 0, false}, 98 {`GMT-15:99:1`, TimeZoneStringToLocationISO8601Standard, 0, false}, 99 {`GMT+6:`, TimeZoneStringToLocationISO8601Standard, 0, false}, 100 {`GMT-6:00:`, TimeZoneStringToLocationISO8601Standard, 0, false}, 101 {`GMT+168:00:00`, TimeZoneStringToLocationISO8601Standard, 0, false}, 102 {`GMT-170:00:59`, TimeZoneStringToLocationISO8601Standard, 0, false}, 103 } 104 105 for i, testCase := range testCases { 106 offset, ok := timeZoneOffsetStringConversion(testCase.timezone, testCase.std) 107 if offset != testCase.offsetSecs || ok != testCase.ok { 108 t.Errorf("%d: Expected offset: %d, success: %v for time %s, but got offset: %d, success: %v", 109 i, testCase.offsetSecs, testCase.ok, testCase.timezone, offset, ok) 110 } 111 } 112 }