How should I get string datetime from API and unmarshal it into struct time.Time field in Gonic?

huangapple go评论89阅读模式
英文:

How should I get string datetime from API and unmarshal it into struct time.Time field in Gonic?

问题

我使用gin context来获取JSON数据并将其转换为struct,这方面运行得很好。但是我在使用time.Time作为字段类型时遇到了问题:

type User struct {
    CreatedAt  time.Time `json:"created_at"`
}

gin中,我使用ShouldBind

var user User
if err := c.ShouldBind(&user); err != nil {
    c.JSON(200, g.H{})
    return
}

我得到的错误是:

parsing time "2019-01-01T00:00:00" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"

似乎需要时区段。我也尝试了Z00:00,但仍然出现解析错误。

我该如何将类似于"2022-01-01 20:00:00"的日期时间转换为Go中的time.Time,甚至带有时区?

英文:

I use gin context to get json data and convert it into struct, it works fine. But what I have issues with is using time.Time as one of the field types:

type User struct {
    CreatedAt  time.Time `json:"created_at"`
}

In gin I use ShouldBind:

  var user User
  if err := c.ShouldBind(&user); err != nil {
  		c.JSON(200, g.H{})
  		return
  }

The error I get is:

parsing time "2019-01-01T00:00:00" as "2006-01-02T15:04:05Z07:00": cannot parse "" as "Z07:00"

It seems that timezone segment is required. I also gave Z00:00 but again parse error raised.

How I can get datetime like "2022-01-01 20:00:00" and convert it into time.Time in Go or even with timezone?

答案1

得分: 0

func HandleTime(c *gin.Context) {
	type User struct {
		CreatedAt time.Time `json:"created_at" binding:"required" time_format:"2006-01-02T15:04:05Z07:00"`
	}
	var user User
	fmt.Println(user.CreatedAt.String())
	if err := c.ShouldBindJSON(&user); err != nil {
		fmt.Println(err)
		return
	}
	c.JSON(200, gin.H{
		"created": user.CreatedAt.String(),
	})
}
curl -X POST 'http://127.0.0.1:8092/${YOUR_PATH}' \
-H 'Content-Type: application/json' -d '{
    "created_at": "2019-01-01T01:00:09+08:00"
}'

响应:

{
    "created": "2019-01-01 01:00:09 +0800 CST"
}

在go doc中查看此示例:https://pkg.go.dev/time@go1.18.4#example-Parse

例如,RFC3339 布局 2006-01-02T15:04:05Z07:00 包含 Z 和时区偏移量,以处理两个有效选项:

  • 2006-01-02T15:04:05Z
  • 2006-01-02T15:04:05+07:00。
英文:
func HandleTime(c *gin.Context) {
	type User struct {
		CreatedAt time.Time `json:"created_at" binding:"required" time_format:"2006-01-02T15:04:05Z07:00"`
	}
	var user User
	fmt.Println(user.CreatedAt.String())
	if err := c.ShouldBindJSON(&user); err != nil {
		fmt.Println(err)
		return
	}
	c.JSON(200, gin.H{
		"created": user.CreatedAt.String(),
	})
}
curl -X POST 'http://127.0.0.1:8092/${YOUR_PATH}' \
-H 'Content-Type: application/json' -d '{
    "created_at": "2019-01-01T01:00:09+08:00"
}'

Response:

{
    "created": "2019-01-01 01:00:09 +0800 CST"
}

See this in go doc: https://pkg.go.dev/time@go1.18.4#example-Parse

For example the RFC3339 layout 2006-01-02T15:04:05Z07:00
contains both Z and a time zone offset in order to handle both valid options:

  • 2006-01-02T15:04:05Z
  • 2006-01-02T15:04:05+07:00.

huangapple
  • 本文由 发表于 2022年9月21日 19:44:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/73800157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定