golang based time conversion problem

  • 2020-06-07 04:40:44
  • OfStack

Generally, there are two functions when the time string is obtained and the time string needs to be formatted as golang's "time.Time" object.


time.Parse(layout, value string) (Time, error)
time.ParseInLocation(layout, value string, loc *Location) (Time, error)

The difference between the two functions is that when the Parse() function is parsed, the UTC time will be used. The acquired Time object will be converted to Unix() object, which will be 8 hours longer than the current time.


tm, err := time.Parse("2006-01-02T15:04:05Z", s) // Time after conversion if converted again unix Time, need -8 hours 

If the parsing source is GMT's time, it is better to use ParseInLocation() and specify "*Location" as "time.Local", such as:


tm, err = time.ParseInLocation("2006-01-02T15:04:05Z", s, time.Local) // The converted time if converted again unix Time, no need to deal with. 

Related articles: