Skip to content
Myr

fix(time): binding time with empty value (#4103)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: binding time with empty value (#4098) * refact: simplify null-to-zero filling logic * test: add test for zeroUnixNanoTime

authored by Kashiwa · committed by GitHub · parent 8f7c340 · Browse files · 2 files+21 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−15 removed

  • other:+21 added−15 removed

2 files+21 added−15 removed

modified:binding/form_mapping_test.go+16 added-10 removed
181 unmodified lines
182}183184func TestMappingTime(t *testing.T) {185 var s struct {186 Time time.Time187 LocalTime time.Time `time_format:"2006-01-02"`188 ZeroValue time.Time189 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.Time187 LocalTime time.Time `time_format:"2006-01-02"`188 ZeroValue time.Time189 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"`193 }194195 var err error196 time.Local, err = time.LoadLocation("Europe/Berlin")197 require.NoError(t, err)198199 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"},207 })208 require.NoError(t, err)209210 assert.Equal(t, "2019-01-20 16:02:58 +0000 UTC", s.Time.String())211 assert.Equal(t, "2019-01-20 00:00:00 +0100 CET", s.LocalTime.String())212 assert.Equal(t, "2019-01-19 23:00:00 +0000 UTC", s.LocalTime.UTC().String())213 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())216 assert.Equal(t, "2019-01-20 00:00:00 +0800 CST", s.CSTTime.String())217 assert.Equal(t, "2019-01-19 16:00:00 +0000 UTC", s.CSTTime.UTC().String())218 assert.Equal(t, "2019-01-20 00:00:00 +0000 UTC", s.UTCTime.String())219
424 unmodified lines
modified:binding/form_mapping.go+5 added-5 removed
395 unmodified lines
396 if timeFormat == "" {397 timeFormat = time.RFC3339398 }399400 if val == "" {401 value.Set(reflect.ValueOf(time.Time{}))402 return nil403 }404405 switch tf := strings.ToLower(timeFormat); tf {406 case "unix", "unixmilli", "unixmicro", "unixnano":407 tv, err := strconv.ParseInt(val, 10, 64)408 if err != nil {
15 unmodified lines
424 value.Set(reflect.ValueOf(t))425 return nil426 }427423 if val == "" {424 value.Set(reflect.ValueOf(time.Time{}))425 return nil426 }427428 l := time.Local429 if isUTC, _ := strconv.ParseBool(structField.Tag.Get("time_utc")); isUTC {430 l = time.UTC431 }
77 unmodified lines