Thanks to visit codestin.com
Credit goes to github.com

Skip to content

binding: support unix time#1980

Merged
thinkerou merged 4 commits into
gin-gonic:masterfrom
guonaihong:binding-support-unix-time
Jul 10, 2019
Merged

binding: support unix time#1980
thinkerou merged 4 commits into
gin-gonic:masterfrom
guonaihong:binding-support-unix-time

Conversation

@guonaihong

@guonaihong guonaihong commented Jul 6, 2019

Copy link
Copy Markdown
Contributor

ref:#1979

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"}

@codecov

codecov Bot commented Jul 6, 2019

Copy link
Copy Markdown

Codecov Report

Merging #1980 into master will decrease coverage by 0.44%.
The diff coverage is 16.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1980      +/-   ##
==========================================
- Coverage   98.69%   98.25%   -0.45%     
==========================================
  Files          40       40              
  Lines        2218     2230      +12     
==========================================
+ Hits         2189     2191       +2     
- Misses         16       26      +10     
  Partials       13       13
Impacted Files Coverage Δ
binding/form_mapping.go 95.26% <16.66%> (-4.74%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e602d52...e29ae3a. Read the comment docs.

@codecov

codecov Bot commented Jul 6, 2019

Copy link
Copy Markdown

Codecov Report

Merging #1980 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1980      +/-   ##
==========================================
+ Coverage   98.69%   98.69%   +<.01%     
==========================================
  Files          40       40              
  Lines        2218     2229      +11     
==========================================
+ Hits         2189     2200      +11     
  Misses         16       16              
  Partials       13       13
Impacted Files Coverage Δ
binding/form_mapping.go 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e602d52...4773be0. Read the comment docs.

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()
}

```
appleboy
appleboy previously approved these changes Jul 9, 2019
@appleboy appleboy requested a review from thinkerou July 9, 2019 16:35
@appleboy appleboy added this to the 1.5 milestone Jul 9, 2019
Comment thread binding/form_mapping.go Outdated
timeFormat = time.RFC3339
}

var t time.Time

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need it?

Comment thread binding/form_mapping.go Outdated
d = time.Second
}

t = time.Unix(tv/int64(d), tv%int64(d))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use t := time.Unix(tv/int64(d), tv%int64(d))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,Already modified

@thinkerou thinkerou merged commit 502c898 into gin-gonic:master Jul 10, 2019
ThomasObenaus pushed a commit to ThomasObenaus/gin that referenced this pull request 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants