douyue4334 2016-08-23 00:40
浏览 45

解析时间时,Mac OSX上的Golang空位置

(Edit/update): Thanks to @djd for pointing out that we can skip all the JSON/struct decoding business; the key issue is with time.Parse.

The same issue comes up here where the Location is "empty" rather than UTC (I would've expected UTC based on the docs: https://golang.org/pkg/time/#Parse

"In the absence of a time zone indicator, Parse returns a time in UTC."

Here's the example code: https://play.golang.org/p/pb3eMbjSmv

package main

import (
    "fmt"
    "time"
)

func main() {
    // Ignoring the err just for this example's sake!
    parsed, _ := time.Parse(time.RFC3339, "2017-08-15T22:30:00+00:00")
    fmt.Printf("String(): %v
", parsed.String())
    fmt.Printf("Location(): %v
", parsed.Location())
}

which outputs

String(): 2017-08-15 22:30:00 +0000 +0000
Location():

So while the offset of the time.Time's Location appears to be correct, its timezone name is just an empty string. Running in on other machines (and The Go Playground) give the expected "UTC" location.

[Original post]:

When decoding a timestamp field from JSON into a struct on my local OS X machine, the Location of the time.Time field is "empty" rather than UTC. This is problematic for me running unit tests locally (vs. on a CI server where the Location is being set correctly to be UTC).

When I run that on my machine, I see

go run main.go TimeField.String(): 2017-08-15 22:30:00 +0000 +0000 TimeField.Location():

So while the offset of the time.Time's Location appears to be correct, its timezone name is just an empty string. This is using Go 1.5:

go version go version go1.5 darwin/amd64

  • 写回答

2条回答 默认 最新

  • douzhigan1687 2016-08-23 05:16
    关注

    I find same behavior using my current setup on Mac and I suspect it will be same behavior on Linux (not sure through)

    $ go version
    go version devel +31ad583 Wed Aug 10 19:44:08 2016 +0000 darwin/amd64
    

    To make it more deterministic, I suggest using a custom json Unmarshal like so:

    package main
    
    import (
      "encoding/json"
      "fmt"
      "strings"
      "time"
    )
    
    type Time struct {
       *time.Time
    }
    
    func (t *Time) UnmarshalJSON(b []byte) error {
      const format = "\"2006-01-02T15:04:05+00:00\""
      t_, err := time.Parse(format, string(b))
      if err != nil {
          return err
      }
      *t = Time{&t_}
      return nil
    }
    
    type Example struct {
      TimeField *Time `json:"time_field"`
    }
    
    func main() {
      inString := "{\"time_field\": \"2017-08-15T22:30:00+00:00\"}"
      var ex Example
      decoder := json.NewDecoder(strings.NewReader(inString))
      decoder.Decode(&ex)
      fmt.Printf("TimeField.String(): %v
    ", ex.TimeField.String())
      fmt.Printf("TimeField.Location(): %v
    ", ex.TimeField.Location())
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP