Skip to content

time.String produces garbage out for time.Parse (garbage in) #17254

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

Closed
Yndoendo opened this issue Sep 27, 2016 · 3 comments
Closed

time.String produces garbage out for time.Parse (garbage in) #17254

Yndoendo opened this issue Sep 27, 2016 · 3 comments

Comments

@Yndoendo
Copy link

time.String() produces non-standard output that time.Parse cannot handle. This goes against less is more since either a person needs to write a custom parser or change the output format which both increases the code base.

If marked as not a bug nor not fixing, documentation for time package should be updated to inform that that time.String output is incompatible with time.Parse.

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

go1.7.1 linux/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="$HOME/.local/share/go"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build766838131=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

https://play.golang.org/p/wmfQP5wX0S

func main() {
    t := time.Now().UTC()
    s := t.String()

    t, err := time.Parse(s,s)
    if err != nil {
        fmt.Println(s, "parsed with error", err)
    } else {
        fmt.Println(s, "equals", t.String())
    }
}
@freeformz
Copy link

freeformz commented Sep 27, 2016

The first value to time.Parse is the layout. time.String's docs indicate what the layout is. So this works.

If anything it would be nice to export that layout as a constant instead of embedding the string directly in time.String().

@bradfitz
Copy link
Contributor

You already filed this bug in #17253. What @freeformz is correct. You're just not using the time package correctly.

I recommend https://golang.org/wiki/Questions for questions about learning Go.

@Yndoendo
Copy link
Author

Sorry, didn't know layout had to be certain date and time, looked only to be just the format.

@golang golang locked and limited conversation to collaborators Sep 27, 2017
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