gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
https://gin-gonic.com/
MIT License
78.02k stars 7.97k forks source link

time param with timezome in Get method ShouldBindQuery parse error #4038

Closed chengjk closed 4 weeks ago

chengjk commented 4 weeks ago

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:

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(&param)
        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

JimChenWYU commented 4 weeks ago

2024-08-18T00:00:00+08:00 -> 2024-08-18T00:00:00%2B08:00 + -> %2B

image

because the query value will be unescaped.

JimChenWYU commented 4 weeks ago

2024-08-18T00:00:00+08:00 -> 2024-08-18T00:00:00%2B08:00 + -> %2B

image

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"`
    ...
}
chengjk commented 4 weeks ago

It works ,thank you very much.