Merge abb667dc950c141a9828a198c27cd3b09cf169f4 into 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d

This commit is contained in:
Fernando Lisboa 2025-03-20 17:54:04 -03:00 committed by GitHub
commit 9caae9ea3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View File

@ -397,6 +397,11 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
timeFormat = time.RFC3339
}
if val == "" {
value.Set(reflect.ValueOf(time.Time{}))
return nil
}
switch tf := strings.ToLower(timeFormat); tf {
case "unix", "unixmilli", "unixmicro", "unixnano":
tv, err := strconv.ParseInt(val, 10, 64)
@ -420,11 +425,6 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
return nil
}
if val == "" {
value.Set(reflect.ValueOf(time.Time{}))
return nil
}
l := time.Local
if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {
l = time.UTC

View File

@ -635,3 +635,25 @@ func TestMappingCustomArrayForm(t *testing.T) {
expected, _ := convertTo(val)
assert.EqualValues(t, expected, s.FileData)
}
func TestMappingTimeUnixAndUnixNano(t *testing.T) {
var s struct {
Unix time.Time `time_format:"unix"`
UnixNano time.Time `time_format:"unixnano"`
EmptyValueUnix time.Time `time_format:"unix"`
EmptyValueUnixNano time.Time `time_format:"unixnano"`
}
err := mapForm(&s, map[string][]string{
"Unix": {"1548000000"},
"UnixNano": {"1548000000000000000"},
"EmptyValue": {""},
"EmptyValueUnixNano": {""},
})
require.NoError(t, err)
assert.Equal(t, "2019-01-20 16:00:00 +0000 UTC", s.Unix.UTC().String())
assert.Equal(t, "2019-01-20 16:00:00 +0000 UTC", s.UnixNano.UTC().String())
assert.Zero(t, s.EmptyValueUnix)
assert.Zero(t, s.EmptyValueUnixNano)
}