Skip to content

Commit 674522d

Browse files
authored
fix(time): binding time with empty value (#4103)
* fix: binding time with empty value (#4098) * refact: simplify null-to-zero filling logic * test: add test for zeroUnixNanoTime
1 parent 8f7c340 commit 674522d

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

binding/form_mapping.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,11 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
397397
timeFormat = time.RFC3339
398398
}
399399

400+
if val == "" {
401+
value.Set(reflect.ValueOf(time.Time{}))
402+
return nil
403+
}
404+
400405
switch tf := strings.ToLower(timeFormat); tf {
401406
case "unix", "unixmilli", "unixmicro", "unixnano":
402407
tv, err := strconv.ParseInt(val, 10, 64)
@@ -420,11 +425,6 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
420425
return nil
421426
}
422427

423-
if val == "" {
424-
value.Set(reflect.ValueOf(time.Time{}))
425-
return nil
426-
}
427-
428428
l := time.Local
429429
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
430430
l = time.UTC

binding/form_mapping_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,36 @@ func TestMapFormWithTag(t *testing.T) {
183183

184184
func TestMappingTime(t *testing.T) {
185185
var s struct {
186-
Time time.Time
187-
LocalTime time.Time `time_format:"2006-01-02"`
188-
ZeroValue time.Time
189-
CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"`
190-
UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"`
186+
Time time.Time
187+
LocalTime time.Time `time_format:"2006-01-02"`
188+
ZeroValue time.Time
189+
ZeroUnixTime time.Time `time_format:"unix"`
190+
ZeroUnixNanoTime time.Time `time_format:"unixnano"`
191+
CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"`
192+
UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"`
191193
}
192194

193195
var err error
194196
time.Local, err = time.LoadLocation("Europe/Berlin")
195197
require.NoError(t, err)
196198

197199
err = mapForm(&s, map[string][]string{
198-
"Time": {"2019-01-20T16:02:58Z"},
199-
"LocalTime": {"2019-01-20"},
200-
"ZeroValue": {},
201-
"CSTTime": {"2019-01-20"},
202-
"UTCTime": {"2019-01-20"},
200+
"Time": {"2019-01-20T16:02:58Z"},
201+
"LocalTime": {"2019-01-20"},
202+
"ZeroValue": {},
203+
"ZeroUnixTime": {},
204+
"ZeroUnixNanoTime": {},
205+
"CSTTime": {"2019-01-20"},
206+
"UTCTime": {"2019-01-20"},
203207
})
204208
require.NoError(t, err)
205209

206210
assert.Equal(t, "2019-01-20 16:02:58 +0000 UTC", s.Time.String())
207211
assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String())
208212
assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String())
209213
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String())
214+
assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixTime.UTC().String())
215+
assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixNanoTime.UTC().String())
210216
assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String())
211217
assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String())
212218
assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String())

0 commit comments

Comments
 (0)