Skip to content

time: time.Parse does not work properly #20880

@malisit

Description

@malisit

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.8.3 darwin/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"

What did you do?

This issue come up when I need to parse a datetime string that is generated by a pseudo random datetime generator that uses Unix() function. The generated datetime takes various date and time pairs between 2010 and 2017. Here's the problem, this version of the code works on both my computers and the playground,

package main

import (
    "fmt"
    "time"
)

func main() {
    a := time.Date(2016, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
    kk := time.Unix(a, 0)
    t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", kk.String())
    if err != nil {
        panic(err)
    }
    fmt.Println(t)
}

But the version below works on playground and panics on my computers. I believe that the problem is that my computers' timezone is setted for GMT+3.

package main

import (
    "fmt"
    "time"
)

func main() {
    a := time.Date(2018, 1, 0, 0, 0, 0, 0, time.UTC).Unix()
    kk := time.Unix(a, 0)
    t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", kk.String())
    if err != nil {
        panic(err)
    }
    fmt.Println(t)
}

What did you expect to see?

I was expecting to have a time.Time object that is parsed from output of Stringer method.

What did you see instead?

panic: parsing time "2017-12-31 03:00:00 +0300 +03" as "2006-01-02 15:04:05.999999999 -0700 MST": cannot parse "+03" as "MST"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions