将Unix纪元作为字符串转换为Go中的time.Time。

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

Convert Unix epoch as a string to time.Time on Go

问题

可以使用Go语言的time包将字符串形式的Unix Epoch日期转换为Go的time.Time类型。你可以使用time.Parse函数来实现这个转换。下面是一个示例代码:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. str := "1490846400"
  8. epoch, err := time.Parse("20060102", str)
  9. if err != nil {
  10. fmt.Println("日期解析错误:", err)
  11. return
  12. }
  13. fmt.Println(epoch)
  14. }

在上面的代码中,我们使用time.Parse函数将字符串"1490846400"解析为一个time.Time类型的值。解析时,我们使用了一个格式字符串"20060102",它指定了Unix Epoch日期的格式。在这个格式字符串中,"2006"表示年份,"01"表示月份,"02"表示日期。通过将字符串解析为time.Time类型,我们可以对日期进行各种操作和格式化。

请注意,上述代码中的时间解析结果将会是UTC时间。如果你需要将其转换为其他时区的时间,可以使用time.Time类型的In方法。例如,如果你想将时间转换为北京时间,可以使用epoch.In(time.FixedZone("CST", 8*60*60))

希望对你有帮助!如果你有其他问题,请随时提问。

英文:

I'm reading a JSON file that contains Unix Epoch dates, but they are strings in the JSON. In Go, can I convert a string in the form "1490846400" into a Go time.Time?

答案1

得分: 13

time包中没有这样的函数,但是很容易编写一个:

  1. func stringToTime(s string) (time.Time, error) {
  2. sec, err := strconv.ParseInt(s, 10, 64)
  3. if err != nil {
  4. return time.Time{}, err
  5. }
  6. return time.Unix(sec, 0), nil
  7. }

Playground: https://play.golang.org/p/2h0Vd7plgk

英文:

There is no such function in time package, but it's easy to write:

  1. func stringToTime(s string) (time.Time, error) {
  2. sec, err := strconv.ParseInt(s, 10, 64)
  3. if err != nil {
  4. return time.Time{}, err
  5. }
  6. return time.Unix(sec, 0), nil
  7. }

Playground: https://play.golang.org/p/2h0Vd7plgk.

答案2

得分: 7

这里没有任何问题或错误,@Ainar-G提供的答案是正确的,但更好的方法是使用自定义的JSON解析器:

  1. type EpochTime time.Time
  2. func (et *EpochTime) UnmarshalJSON(data []byte) error {
  3. t := strings.Trim(string(data), `"`) // 从JSON字符串中去除引号
  4. sec, err := strconv.ParseInt(t, 10, 64)
  5. if err != nil {
  6. return err
  7. }
  8. epochTime := time.Unix(sec, 0)
  9. *et = EpochTime(epochTime)
  10. return nil
  11. }
  12. type SomeDocument struct {
  13. Timestamp EpochTime `json:"time"`
  14. // 其他字段
  15. }

在你的结构体中,用EpochTime替换time.Time

英文:

There's nothing wrong, or incorrect about the answer provided by @Ainar-G, but likely a better way to do this is with a custom JSON unmarshaler:

  1. type EpochTime time.Time
  2. func (et *EpochTime) UnmarshalJSON(data []byte) error {
  3. t := strings.Trim(string(data), `"`) // Remove quote marks from around the JSON string
  4. sec, err := strconv.ParseInt(t, 10, 64)
  5. if err != nil {
  6. return err
  7. }
  8. epochTime := time.Unix(sec,0)
  9. *et = EpochTime(epochTime)
  10. return nil
  11. }

Then in your struct, replace time.Time with EpochTime:

  1. type SomeDocument struct {
  2. Timestamp EpochTime `json:"time"`
  3. // other fields
  4. }

huangapple
  • 本文由 发表于 2017年4月16日 04:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/43431046.html
匿名

发表评论

匿名网友

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

确定