Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: time.Parse does not work properly #20880

Closed
malisit opened this issue Jul 1, 2017 · 3 comments
Closed

time: time.Parse does not work properly #20880

malisit opened this issue Jul 1, 2017 · 3 comments

Comments

@malisit
Copy link

malisit commented Jul 1, 2017

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"

@malisit malisit changed the title time.Parse does not work properly time: time.Parse does not work properly Jul 1, 2017
@bradfitz
Copy link
Contributor

bradfitz commented Jul 1, 2017

Time.String does not have a defined format. Use Format if you want specific output.

Related: #20876

I'm going to close this because #20876 will probably involve more docs in this area.

@bradfitz bradfitz closed this as completed Jul 1, 2017
@ALTree
Copy link
Member

ALTree commented Jul 1, 2017

If you are in Europe/Istanbul, there was an abbreviation change in 2016

$ zdump -v /usr/share/zoneinfo/Europe/Istanbul | grep 2016

Europe/Istanbul  Tue Sep  6 20:59:59 2016 UT = Tue Sep  6 23:59:59 2016 EEST isdst=1 gmtoff=10800
Europe/Istanbul  Tue Sep  6 21:00:00 2016 UT = Wed Sep  7 00:00:00 2016 +03 isdst=0 gmtoff=10800

Don't assume the timezone abbreviation will be printed as EET or EEST:

Turkey switched from EET/EEST (+02/+03) to permanent +03,
effective 2016-09-07. (Thanks to Burak AYDIN.) Use "+03" rather
than an invented abbreviation for the new time.

In your 2018 date the abbreviation is printed as +03, that's why it doesn't match the MST in the standard string that you provided.

@malisit
Copy link
Author

malisit commented Jul 2, 2017

@ALTree I'm well aware of that. I believe Go should have a work around to have things working despite this issue, at least Go should count (+02/+03) as EET/EEST.

@golang golang locked and limited conversation to collaborators Jul 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants