MCPcopy
hub / github.com/spf13/cast / TestTimeWithTimezones

Function TestTimeWithTimezones

time_test.go:119–239  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

117}
118
119func TestTimeWithTimezones(t *testing.T) {
120 c := qt.New(t)
121
122 est, err := time.LoadLocation("EST")
123 c.Assert(err, qt.IsNil)
124
125 irn, err := time.LoadLocation("Iran")
126 c.Assert(err, qt.IsNil)
127
128 swd, err := time.LoadLocation("Europe/Stockholm")
129 c.Assert(err, qt.IsNil)
130
131 // Test same local time in different timezones
132 utc2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)
133 est2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, est)
134 irn2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, irn)
135 swd2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, swd)
136 loc2016 := time.Date(2016, time.January, 1, 0, 0, 0, 0, time.Local)
137
138 for i, format := range internal.TimeFormats {
139 format := format
140 if format.Typ == internal.TimeFormatTimeOnly {
141 continue
142 }
143
144 nameBase := fmt.Sprintf("%d;timeFormatType=%d;%s", i, format.Typ, format.Format)
145
146 t.Run(path.Join(nameBase), func(t *testing.T) {
147 est2016str := est2016.Format(format.Format)
148 swd2016str := swd2016.Format(format.Format)
149
150 t.Run("without default location", func(t *testing.T) {
151 c := qt.New(t)
152 converted, err := cast.ToTimeE(est2016str)
153 c.Assert(err, qt.IsNil)
154 if format.HasTimezone() {
155 // Converting inputs with a timezone should preserve it
156 assertTimeEqual(t, est2016, converted)
157 assertLocationEqual(t, est, converted.Location())
158 } else {
159 // Converting inputs without a timezone should be interpreted
160 // as a local time in UTC.
161 assertTimeEqual(t, utc2016, converted)
162 assertLocationEqual(t, time.UTC, converted.Location())
163 }
164 })
165
166 t.Run("local timezone without a default location", func(t *testing.T) {
167 c := qt.New(t)
168 converted, err := cast.ToTimeE(swd2016str)
169 c.Assert(err, qt.IsNil)
170 if format.HasTimezone() {
171 // Converting inputs with a timezone should preserve it
172 assertTimeEqual(t, swd2016, converted)
173 assertLocationEqual(t, swd, converted.Location())
174 } else {
175 // Converting inputs without a timezone should be interpreted
176 // as a local time in UTC.

Callers

nothing calls this directly

Calls 5

ToTimeEFunction · 0.92
ToTimeInDefaultLocationEFunction · 0.92
assertTimeEqualFunction · 0.85
assertLocationEqualFunction · 0.85
HasTimezoneMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…