Description
I have Param Struct to QueryBind with, I think it parse time failed when bind query.
I use the defult format: time.RFC3339, it should works at param: - start=2024-08-18T00:00:00+08:00 - start=2024-08-18T00:00:00Z
How to reproduce
package main
import (
"github.com/gin-gonic/gin"
"time"
)
type Param struct {
Start time.Time `json:"start" form:"start"`
End time.Time `json:"end" form:"end"`
}
func main() {
g := gin.Default()
g.GET("/report", func(c *gin.Context) {
param := Param{}
err := c.ShouldBindQuery(¶m)
if err != nil {
c.JSON(200, err)
return
}
c.JSON(200, "ok")
})
g.Run(":9000")
}
Expectations
Expect (bad case)
$ curl --location --request GET 'http://localhost:9000/report?start=2024-08-18T00:00:00+08:00&end=2024-08-19T00:00:00+08:00'
ok
Actual result
$ curl --location --request GET 'http://localhost:9000/report?start=2024-08-18T00:00:00+08:00&end=2024-08-19T00:00:00+08:00'
{"Layout":"2006-01-02T15:04:05Z07:00","Value":"2024-08-18T00:00:00 08:00","LayoutElem":"Z07:00","ValueElem":" 08:00","Message":""}
Expect (good case)
use default zone
$ curl --location --request GET 'http://localhost:9000/report?start=2024-08-18T00:00:00Z&end=2024-08-19T00:00:00Z'
ok
Actual result
used utc timezone
$ curl --location --request GET 'http://localhost:9000/report?start=2024-08-18T00:00:00Z&end=2024-08-19T00:00:00Z'
ok
Environment
- go: 1.22
- gin: v1.10.0
- operating system: mac, Debian GNU/Linux 12 (bookworm)
Comment From: JimChenWYU
2024-08-18T00:00:00+08:00
-> 2024-08-18T00:00:00%2B08:00
+
-> %2B
because the query value will be unescaped.
Comment From: JimChenWYU
2024-08-18T00:00:00+08:00
->2024-08-18T00:00:00%2B08:00
+
->%2B
because the query value will be unescaped.
by the way, you can set tag like time_format
, time_utc
, time_location
type Param struct {
Start time.Time `json:"start" form:"start" time_format:"2006-01-02T15:04:05Z07:00" time_utc:"true" time_location:"UTC"`
...
}
Comment From: chengjk
It works ,thank you very much.