MCPcopy
hub / github.com/gin-gonic/gin / TestMappingTime

Function TestMappingTime

binding/form_mapping_test.go:185–228  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

183}
184
185func TestMappingTime(t *testing.T) {
186 var s struct {
187 Time time.Time
188 LocalTime time.Time `time_format:"2006-01-02"`
189 ZeroValue time.Time
190 CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"`
191 UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"`
192 }
193
194 var err error
195 time.Local, err = time.LoadLocation("Europe/Berlin")
196 require.NoError(t, err)
197
198 err = mapForm(&s, map[string][]string{
199 "Time": {"2019-01-20T16:02:58Z"},
200 "LocalTime": {"2019-01-20"},
201 "ZeroValue": {},
202 "CSTTime": {"2019-01-20"},
203 "UTCTime": {"2019-01-20"},
204 })
205 require.NoError(t, err)
206
207 assert.Equal(t, "2019-01-20 16:02:58 +0000 UTC", s.Time.String())
208 assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String())
209 assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String())
210 assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String())
211 assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String())
212 assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String())
213 assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String())
214
215 // wrong location
216 var wrongLoc struct {
217 Time time.Time `time_location:"wrong"`
218 }
219 err = mapForm(&wrongLoc, map[string][]string{"Time": {"2019-01-20T16:02:58Z"}})
220 require.Error(t, err)
221
222 // wrong time value
223 var wrongTime struct {
224 Time time.Time
225 }
226 err = mapForm(&wrongTime, map[string][]string{"Time": {"wrong"}})
227 require.Error(t, err)
228}
229
230type bindTestData struct {
231 need any

Callers

nothing calls this directly

Calls 3

mapFormFunction · 0.85
StringMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected