Merge 5433c4cd50d8478b5fea05b3a090e46006e2f08f into 674522db91d637d179c16c372d87756ea26fa089

This commit is contained in:
Bo-Yi Wu 2025-05-21 19:24:11 +08:00 committed by GitHub
commit b696e554b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 21 deletions

View File

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

View File

@ -183,13 +183,11 @@ func TestMapFormWithTag(t *testing.T) {
func TestMappingTime(t *testing.T) { func TestMappingTime(t *testing.T) {
var s struct { var s struct {
Time time.Time Time time.Time
LocalTime time.Time `time_format:"2006-01-02"` LocalTime time.Time `time_format:"2006-01-02"`
ZeroValue time.Time ZeroValue time.Time
ZeroUnixTime time.Time `time_format:"unix"` CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"`
ZeroUnixNanoTime time.Time `time_format:"unixnano"` UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"`
CSTTime time.Time `time_format:"2006-01-02" time_location:"Asia/Shanghai"`
UTCTime time.Time `time_format:"2006-01-02" time_utc:"1"`
} }
var err error var err error
@ -197,13 +195,11 @@ func TestMappingTime(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
err = mapForm(&s, map[string][]string{ err = mapForm(&s, map[string][]string{
"Time": {"2019-01-20T16:02:58Z"}, "Time": {"2019-01-20T16:02:58Z"},
"LocalTime": {"2019-01-20"}, "LocalTime": {"2019-01-20"},
"ZeroValue": {}, "ZeroValue": {},
"ZeroUnixTime": {}, "CSTTime": {"2019-01-20"},
"ZeroUnixNanoTime": {}, "UTCTime": {"2019-01-20"},
"CSTTime": {"2019-01-20"},
"UTCTime": {"2019-01-20"},
}) })
require.NoError(t, err) require.NoError(t, err)
@ -211,8 +207,6 @@ func TestMappingTime(t *testing.T) {
assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String()) assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String())
assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String()) assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String())
assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String()) assert.Equal(t, "0001-01-01 00:00:00 +0000 UTC", s.ZeroValue.String())
assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixTime.UTC().String())
assert.Equal(t, "1970-01-01 00:00:00 +0000 UTC", s.ZeroUnixNanoTime.UTC().String())
assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String()) assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String())
assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String()) assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String())
assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String()) assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String())