Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binding:extended time.Time type, support Unix time #1979

Closed
guonaihong opened this issue Jul 6, 2019 · 1 comment
Closed

binding:extended time.Time type, support Unix time #1979

guonaihong opened this issue Jul 6, 2019 · 1 comment

Comments

@guonaihong
Copy link
Contributor

guonaihong commented Jul 6, 2019

I sometimes use createTime (unix time) as the key for data segmentation. A common usage is that Service A is responsible for generating creatTime. Service B routes to the corresponding database table based on createTime in the data.
It is found that gin does not currently support binding of unix time to time.Time type.

Pseudo code is as follows

package main

import (
        "fmt"
        "github.com/gin-gonic/gin"
        "time"
)

type shareTime struct {
        CreateTime time.Time `form:"createTime" time_format:"unixNano"`
        UnixTime   time.Time `form:"unixTime" time_format:"unix"`
}

func main() {
        r := gin.Default()
        unix := r.Group("/unix")

        testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
        fmt.Printf("%d\n", testCT.UnixNano())

        testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
        fmt.Printf("%d\n", testUT.Unix())

        unix.GET("/nano", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testCT.Equal(s.CreateTime) {
                        c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
                        return
                }

                c.JSON(200, s)
        })

        unix.GET("/sec", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testUT.Equal(s.UnixTime) {
                        c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
                        return
                }

                c.JSON(200, s)

        })

        r.Run()
}

// client
//
// curl -X GET "127.0.0.1:8080/unix/sec?unixTime=1562400033"
//   {"CreateTime":"0001-01-01T00:00:00Z","UnixTime":"2019-07-06T16:00:33+08:00"}

// curl -X GET "127.0.0.1:8080/unix/nano?createTime=1562400033000000123"
//   {"CreateTime":"2019-07-06T16:00:33.000000123+08:00","UnixTime":"0001-01-01T00:00:00Z"}
guonaihong added a commit to guonaihong/gin that referenced this issue Jul 6, 2019
thinkerou pushed a commit that referenced this issue Jul 10, 2019
* binding: support unix time

ref:#1979

* binding: support unix time

add test file
modify readme

```golang
package main

import (
        "fmt"
        "github.com/gin-gonic/gin"
        "time"
)

type shareTime struct {
        CreateTime time.Time `form:"createTime" time_format:"unixNano"`
        UnixTime   time.Time `form:"unixTime" time_format:"unix"`
}

func main() {
        r := gin.Default()
        unix := r.Group("/unix")

        testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
        fmt.Printf("%d\n", testCT.UnixNano())

        testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
        fmt.Printf("%d\n", testUT.Unix())

        unix.GET("/nano", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testCT.Equal(s.CreateTime) {
                        c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
                        return
                }

                c.JSON(200, s)
        })

        unix.GET("/sec", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testUT.Equal(s.UnixTime) {
                        c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
                        return
                }

                c.JSON(200, s)

        })

        r.Run()
}

```

* Contraction variable scope
ThomasObenaus pushed a commit to ThomasObenaus/gin that referenced this issue Feb 19, 2020
* binding: support unix time

ref:gin-gonic#1979

* binding: support unix time

add test file
modify readme

```golang
package main

import (
        "fmt"
        "github.com/gin-gonic/gin"
        "time"
)

type shareTime struct {
        CreateTime time.Time `form:"createTime" time_format:"unixNano"`
        UnixTime   time.Time `form:"unixTime" time_format:"unix"`
}

func main() {
        r := gin.Default()
        unix := r.Group("/unix")

        testCT := time.Date(2019, 7, 6, 16, 0, 33, 123, time.Local)
        fmt.Printf("%d\n", testCT.UnixNano())

        testUT := time.Date(2019, 7, 6, 16, 0, 33, 0, time.Local)
        fmt.Printf("%d\n", testUT.Unix())

        unix.GET("/nano", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testCT.Equal(s.CreateTime) {
                        c.String(500, "want %d got %d", testCT.UnixNano(), s.CreateTime)
                        return
                }

                c.JSON(200, s)
        })

        unix.GET("/sec", func(c *gin.Context) {
                s := shareTime{}

                c.ShouldBindQuery(&s)

                if !testUT.Equal(s.UnixTime) {
                        c.String(500, "want %d got %d", testCT.Unix(), s.UnixTime)
                        return
                }

                c.JSON(200, s)

        })

        r.Run()
}

```

* Contraction variable scope
@IsQiao
Copy link

IsQiao commented Sep 6, 2023

the feature can't bind requrest json body property. any solution about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants