user27111987
user27111987

Reputation: 1085

Golang Time parse issue

I am executing below code to parse a time

var time_format = "2006-01-02T15:04:05.000+0700"
var s = "2018-08-23T14:10:31.692+0700"
p, _ := time.Parse(time_format, s)
fmt.Println(p.String())

The output of above program is as below.

2018-08-23 14:10:31.692 +0000 UTC

It is the same time in UTC while I am parsing a time which is +0700 ahead of UTC so as expeceted result should be

2018-08-23 7:10:31.692 +0000 UTC

Can anyone tell what is the issue here.

Upvotes: 1

Views: 495

Answers (1)

icza
icza

Reputation: 418585

It's because your format string is not correct. The timezone indication must be -0700 (not +0700). time.Parse():

The layout defines the format by showing how the reference time, defined to be

Mon Jan 2 15:04:05 -0700 MST 2006

With that change it works:

var format = "2006-01-02T15:04:05.000-0700"

var s = "2018-08-23T14:10:31.692+0700"
p, err := time.Parse(format, s)

fmt.Println(p.String(), err)

This will output (try it on the Go Playground):

2018-08-23 14:10:31.692 +0700 +0700 <nil>

Upvotes: 5

Related Questions