Franky1
(Franky)
May 18, 2022, 1:46pm
1
I have the following timestamps, that i want to parse:
2022.05.14-00.37.13:544
I tried it with the following code snippet, but i could not parse the milliseconds:
package main
import (
"fmt"
"time"
)
func main() {
s := "2022.05.14-00.37.13:544"
t, err := time.Parse("2006.01.02-15.04.05:000", s)
fmt.Println(t)
fmt.Println(err)
}
It fails with:
parsing time "2022.05.14-00.37.13:544" as "2006.01.02-15.04.05:000": cannot parse "544" as ":000"
I tried different things, but none of them worked.
It only works if i remove the milliseconds.
What is the proper “reference string” to get this working also with the milliseconds?
Thanks for all hints.
Here is the Go Playground with the above code snippet:
Franky1
(Franky)
May 18, 2022, 2:27pm
2
I assume my above use case is currently not possible with the Go time parser, if i read this issue:
opened 09:06PM - 19 Aug 13 UTC
closed 12:28AM - 16 Mar 21 UTC
by **dmitri.m**:
<pre>What steps will reproduce the problem?
If possible, inclu… de a link to a program on play.golang.org.
The Go time package expects the decimal mark in fractional seconds to always be a dot.
Attempting to parse a time string that uses a decimal comma results in an error:
<a href="http://play.golang.org/p/d8qQasN0z1">http://play.golang.org/p/d8qQasN0z1</a>
Decimal commas are standard in many locales, but there is currently no way to parse the
fractional part of the second in a time string if it's separated by a comma.
<a href="https://en.wikipedia.org/wiki/Decimal_mark#Countries_using_Arabic_numerals_with_decimal_comma">https://en.wikipedia.org/wiki/Decimal_mark#Countries_using_Arabic_numerals_with_decimal_comma</a>
What is the expected output?
In case the provided layout also uses a decimal comma, the time string should be parsed
without errors.
What do you see instead?
parsing time "2013-08-19 22:56:01,234" as "2006-01-02 15:04:05,000":
cannot parse "234" as ",000"
Which compiler are you using (5g, 6g, 8g, gccgo)?
6g
Which operating system are you using?
OS X 10.8.4
Which version are you using? (run 'go version')
go version devel +5037426bea2f Mon Aug 19 23:09:24 2013 +0400 darwin/amd64
Please provide any additional information below.
Note that (*time.Time).Format() currently outputs decimal commas just fine since it
doesn't see them as a special character. But making the straightforward change to
support decimal commas in Parse() - by adding them to the stdFracSecond{0,9} classes -
breaks this due to hardcoded '.' in formatNano().</pre>
mje
(Jeff Emanuel)
May 18, 2022, 5:19pm
3
A workaround is to replace the colon with a period and then parse using layout “2006.01.02-15.04.05.000”
system
(system)
Closed
August 16, 2022, 5:20pm
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.