1

I'm facing an issue where Go time parse is returning different values for two times in the same timezone.

func timeParse() {
    layout := "Mon, 2 Jan 2006 03:04:05 -0700 (MST)"

    value1 := "Mon, 18 Jan 2016 01:48:52 -0800 (PST)"
    value2 :=  "Tue, 19 Jan 2016 17:49:33 -0800 (PST)"

    t1, _ := time.Parse(layout, value1)
    fmt.Println(t1)

    t2, _ := time.Parse(layout, value2)
    fmt.Println(t2)
}

Output:

2016-01-18 01:48:52 -0800 PST
0001-01-01 00:00:00 +0000 UTC

Notice that the second one didn't parse properly.

1
  • 3
    t2, _ := time.Parse(). The "error" is with you omitting the returned error (the 2nd return value is an error). Never omit errors. Would you have printed it, you would have seen: hour out of range Commented Feb 2, 2016 at 7:20

1 Answer 1

1

Found my mistake. The layout expects a 24 hour times. Fixed with:

layout := "Mon, 2 Jan 2006 15:04:05 -0700 (MST)"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.