2019-02-03 01:38
浏览 815


Never tried Go before and currently doing a small project. One of the task is to get current system time and represent it in YYYY-MM-DDT00:00:00Z format. I believe that Z means that time is represented in UTC format but when i looked into db, all timestamps are like this i.e., 2011-11-22T15:22:10Z.

So how can i format like this in Go?

Update I was able to format it using following code

t := time.Now()

Now the question remains, what Z signifies here. Should i get UTC Time?

Another question, it looks like that the value i am using to format impacts the output i.e., when i used 2019-01-02T15:04:05Z the output became 2029-02-02T20:45:11Z, why?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douci1918
    douci1918 2019-02-03 01:52

    Go provides very flexible way to parse the time by example. For this, you have to write the "reference" time in the format of your choice. The reference time is Mon Jan 2 15:04:05 MST 2006. In my case, I used this reference time to parse the Now():

    点赞 评论
  • doupa2871
    doupa2871 2019-02-03 06:28

    "If a time is in Coordinated Universal Time (UTC), a "Z" is added directly after the time without a separating space. "Z" is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". Likewise, "14:45:15 UTC" is written as "14:45:15Z" or "144515Z".[16]"

    From https://en.wikipedia.org/wiki/Time_zone#UTC

    // Some valid layouts are invalid time values for time.Parse, due to formats
    // such as _ for space padding and Z for zone information.


    // Replacing the sign in the format with a Z triggers
    // the ISO 8601 behavior of printing Z instead of an
    // offset for the UTC zone. Thus:
    //  Z0700  Z or ±hhmm
    //  Z07:00 Z or ±hh:mm
    //  Z07    Z or ±hh

    From the source for package time/format.go

    点赞 评论