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

Automatic Time pars #1062

Closed
almgwary opened this issue Aug 13, 2017 · 4 comments
Closed

Automatic Time pars #1062

almgwary opened this issue Aug 13, 2017 · 4 comments

Comments

@almgwary
Copy link

almgwary commented Aug 13, 2017

i am sending time in time.RFC3339 format it has the following problem with automatic binding.

my modle

type MyModel struct {
	From   time.Time   `form:"from" json:"from"`
}

binding

var myModel MyModel 
context.Bind(&myModel)

error "parsing time ""2017-08-16P15:00:00+00:00"" as ""2006-01-02T15:04:05Z07:00"": cannot parse "P15:00:00+00:00"" as "T""

How to setup automatic binding to use format time.RFC3339 ?
i cannot find any middle-ware configuration

@easonlin404
Copy link
Contributor

@almgwary Could you please post your example code?
Because json accepted RFC 3339 in Go if you binding with json content type. Like following date-string:

2002-10-02T10:00:00-05:00

2002-10-02T15:00:00Z

2002-10-02T15:00:00.05Z

@ckybonist
Copy link

I also encountered a similar problem, please check out my example code.

In the example code, I need to manually add time.RFC3339 format to time_format field tag, otherwise Gin will throw an error: Blank time format.

But adding these field tag makes my struct definition cumbersome:

type struct Question {
    Title string `json:"title" form:"title"`
    CreatedAt time.Time `json:"createdAt" form:"createdAt" time_format:"2006-01-02T15:04:05Z07:00"`
}

NOTE: "2006-01-02T15:04:05Z07:00" is just the time.RFC3339

Any workarounds to solve this problem?

@thinkerou
Copy link
Member

thinkerou commented Aug 20, 2018

Hi all, please see #1487 thanks!

@liulwx
Copy link

liulwx commented Mar 23, 2019

I also encountered a similar problem, please check out my example code.

In the example code, I need to manually add time.RFC3339 format to time_format field tag, otherwise Gin will throw an error: Blank time format.

But adding these field tag makes my struct definition cumbersome:

type struct Question {
    Title string `json:"title" form:"title"`
    CreatedAt time.Time `json:"createdAt" form:"createdAt" time_format:"2006-01-02T15:04:05Z07:00"`
}

NOTE: "2006-01-02T15:04:05Z07:00" is just the time.RFC3339

Any workarounds to solve this problem?

In fact, I am confused by the same problem. How did you solved this problems?

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

5 participants