在golang中没有时间处理日期的惯用方法是什么?

Dan*_*ner 19 date go

我正在Go中编写REST API,使用不代表单个时间点的日期.

它是以"2006-01-02"格式进出服务器的JSON数据,该数据使用DATE列与mysql数据库通信.

我尝试过的一件事是创建一个嵌入Time的结构,并实现JSON和SQL转换接口实现,以便能够正确地与端点交互,同时仍然有Time方法可用于日期数学和格式化.例如:

package localdate

import (
    "time"
    "encoding/json"
    "database/sql/driver"
)

type LocalDate struct {
    time.Time
}

func NewLocalDate(year int, month time.Month, day int) LocalDate {
    time := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
    return LocalDate{Time: time}
}

const LocalDateFormat = "2006-01-02" // yyyy-mm-dd

func (ld *LocalDate) UnmarshalJSON(data []byte) error {
    // parse and set the ld.Time variable
}

func (ld *LocalDate) MarshalJSON() ([]byte, error) {
    return json.Marshal(ld.Format(LocalDateFormat))
}

// sql.Scanner implementation to convert a time.Time column to a LocalDate
func (ld *LocalDate) Scan(value interface{}) error {}

// sql/driver.Valuer implementation to go from LocalDate -> time.Time
func (ld *LocalDate) Value() (driver.Value, error)  {}

// used to convert a LocalDate into something we can plug into a query
// we could just use ld.Time, but that would send '2015-01-01 00:00:00 +0000 UTC'
// instead of '2015-01-01' for the DATE query parameter.  (Which works for mysql, but is officially invalid SQL)
func (ld *LocalDate) SqlDate() string  {
    return ld.Format(LocalDateFormat)
}
Run Code Online (Sandbox Code Playgroud)

然后其他结构可以是这种类型,并在那里得到90%来表示我的问题域中的日期类型.

上面的代码有效,但我觉得我正在反对Go当前.所以对于该语言的退伍军人来说有几个问题:

你认为这段代码会导致更多的痛苦吗?
如果是这样,你会推荐什么样的风格?

mvn*_*aai 5

civil.Date从包cloud.google.com/go/civil 使用

  • 如果您像我一样还是新手 - 导入此模块的命令是“go get cloud.google.com/go/civil” (2认同)

Ale*_*eev 2

我认为您可以将数据存储为time.Time但将其转换为字符串以用于 JSON 目的:

type LocalDate struct {
  t time.Time `json:",string"` // might even work anonymously here
}
Run Code Online (Sandbox Code Playgroud)

要了解如何使用 SQL 实现此功能: https: //github.com/go-sql-driver/mysql#timetime-support