mirror of https://github.com/gin-gonic/gin.git
Support time location on form binding (#1117)
This commit is contained in:
parent
5afc5b1973
commit
a8c53949e5
|
@ -163,6 +163,14 @@ func setTimeField(val string, structField reflect.StructField, value reflect.Val
|
||||||
l = time.UTC
|
l = time.UTC
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if locTag := structField.Tag.Get("time_location"); locTag != "" {
|
||||||
|
loc, err := time.LoadLocation(locTag)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
l = loc
|
||||||
|
}
|
||||||
|
|
||||||
t, err := time.ParseInLocation(timeFormat, val, l)
|
t, err := time.ParseInLocation(timeFormat, val, l)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -45,6 +45,7 @@ func createMultipartRequest() *http.Request {
|
||||||
must(mw.WriteField("id", ""))
|
must(mw.WriteField("id", ""))
|
||||||
must(mw.WriteField("time_local", "31/12/2016 14:55"))
|
must(mw.WriteField("time_local", "31/12/2016 14:55"))
|
||||||
must(mw.WriteField("time_utc", "31/12/2016 14:55"))
|
must(mw.WriteField("time_utc", "31/12/2016 14:55"))
|
||||||
|
must(mw.WriteField("time_location", "31/12/2016 14:55"))
|
||||||
req, err := http.NewRequest("POST", "/", body)
|
req, err := http.NewRequest("POST", "/", body)
|
||||||
must(err)
|
must(err)
|
||||||
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+boundary)
|
||||||
|
@ -451,6 +452,7 @@ func TestContextPostFormMultipart(t *testing.T) {
|
||||||
ID string `form:"id"`
|
ID string `form:"id"`
|
||||||
TimeLocal time.Time `form:"time_local" time_format:"02/01/2006 15:04"`
|
TimeLocal time.Time `form:"time_local" time_format:"02/01/2006 15:04"`
|
||||||
TimeUTC time.Time `form:"time_utc" time_format:"02/01/2006 15:04" time_utc:"1"`
|
TimeUTC time.Time `form:"time_utc" time_format:"02/01/2006 15:04" time_utc:"1"`
|
||||||
|
TimeLocation time.Time `form:"time_location" time_format:"02/01/2006 15:04" time_location:"Asia/Tokyo"`
|
||||||
BlankTime time.Time `form:"blank_time" time_format:"02/01/2006 15:04"`
|
BlankTime time.Time `form:"blank_time" time_format:"02/01/2006 15:04"`
|
||||||
}
|
}
|
||||||
assert.NoError(t, c.Bind(&obj))
|
assert.NoError(t, c.Bind(&obj))
|
||||||
|
@ -463,6 +465,9 @@ func TestContextPostFormMultipart(t *testing.T) {
|
||||||
assert.Equal(t, obj.TimeLocal.Location(), time.Local)
|
assert.Equal(t, obj.TimeLocal.Location(), time.Local)
|
||||||
assert.Equal(t, obj.TimeUTC.Format("02/01/2006 15:04"), "31/12/2016 14:55")
|
assert.Equal(t, obj.TimeUTC.Format("02/01/2006 15:04"), "31/12/2016 14:55")
|
||||||
assert.Equal(t, obj.TimeUTC.Location(), time.UTC)
|
assert.Equal(t, obj.TimeUTC.Location(), time.UTC)
|
||||||
|
loc, _ := time.LoadLocation("Asia/Tokyo")
|
||||||
|
assert.Equal(t, obj.TimeLocation.Format("02/01/2006 15:04"), "31/12/2016 14:55")
|
||||||
|
assert.Equal(t, obj.TimeLocation.Location(), loc)
|
||||||
assert.True(t, obj.BlankTime.IsZero())
|
assert.True(t, obj.BlankTime.IsZero())
|
||||||
|
|
||||||
value, ok := c.GetQuery("foo")
|
value, ok := c.GetQuery("foo")
|
||||||
|
|
Loading…
Reference in New Issue