Skip to content

Go parse time error? #27746

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
Yukinoshita-Yukino opened this issue Sep 19, 2018 · 3 comments
Closed

Go parse time error? #27746

Yukinoshita-Yukino opened this issue Sep 19, 2018 · 3 comments

Comments

@Yukinoshita-Yukino
Copy link

Yukinoshita-Yukino commented Sep 19, 2018

Please answer these questions before submitting your issue. Thanks!

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

The Go Playground
Code:
https://play.golang.org/p/0oIjit-r3pp

Does this issue reproduce with the latest release?

I guess so?

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

The Go Playground
https://play.golang.org/p/0oIjit-r3pp

What did you do?

I was trying to use gogstash to parse logs, and I have some issue parsing time.
I don't have local Go env so I tried the Go Playground and seems like the problem is from Go.
The log I was trying to parse uses "YYYYMMdd-HHmmss-SSS" format for timestamp, so I tried "20060102-150405-999" and "20060102-150405-000" for time format but I got error for both of them.

What did you expect to see?

Time being parsed correctly.

What did you see instead?

parsing time "20180724-101112-111" as "20060102-150405-999": cannot parse "111" as "-999"

@mattn
Copy link
Member

mattn commented Sep 19, 2018

Document says

A fractional second is represented by adding a period and zeros to the end of the seconds section of layout string, as in "15:04:05.000" to format a time stamp with millisecond precision.

Similar issue #26002

@agnivade
Copy link
Contributor

That is correct. This is working as intended. Please follow #26002 if you want other separators than a period. Or otherwise, change the code to replace - with ..

package main

import (
	"fmt"
	"time"
)

func main() {
	t, err := time.Parse("20060102-150405.000", "20180724-101112.111")
	fmt.Printf("%v %v\n", t, err)
}

@AlekSi
Copy link
Contributor

AlekSi commented Aug 9, 2019

See also #6189.

@golang golang locked and limited conversation to collaborators Aug 8, 2020
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

5 participants