Issues with parsing `time.String()` in Go 1.19

80 views
Skip to first unread message

Erez Rokah

unread,
Nov 9, 2022, 11:33:48 AM11/9/22
to golang-nuts
Hello!

I'm not sure if this is a bug or not. The following test will print both error messages:

```
func TestTimeStringParse(t *testing.T) {
  for i := 0; i < 100000; i++ {
    timeString := time.Now().Add(time.Duration(rand.Int63())).String()
    _, err := time.Parse(defaultStringFormat, timeString)
   if err == nil {
     t.Errorf("time.Parse(%q) should return error", timeString)
    } else {
      t.Errorf("time.Parse(%q) should not return error", timeString)
    }
  }
}
```

The failure stems from Go 1.19 adding the extra text `m=+<int>` for the monotonic clock.
This doesn't happen for every `time.String()` invocation, which is a bit confusing.
I understand one shouldn't try to parse `time.String()`, especially with the private format, however this does looks like something that worked before Go 1.19.

Would be glad to know what others think.
Cheers,
Erez

Ian Lance Taylor

unread,
Nov 9, 2022, 11:48:09 AM11/9/22
to Erez Rokah, golang-nuts
What is defaultStringFormat?

Adding m=<int> is not new in Go 1.19.

Ian

Erez Rokah

unread,
Nov 9, 2022, 12:00:40 PM11/9/22
to golang-nuts
The string format used is `"2006-01-02 15:04:05.999999999 -0700 MST"` which is `time.String()` internal format

Ian Lance Taylor

unread,
Nov 9, 2022, 2:54:18 PM11/9/22
to Erez Rokah, golang-nuts
On Wed, Nov 9, 2022 at 9:00 AM Erez Rokah <er...@cloudquery.io> wrote:
>
> The string format used is `"2006-01-02 15:04:05.999999999 -0700 MST"` which is `time.String()` internal format

Thanks.

You originally asked whether this is a bug or not. I don't think this
is a bug. It's true that it's not easy to use time.Parse to parse a
string generated by time.String. That is perhaps unfortunate, but
it's a consequence of how the various functions work.

As I noted earlier the -m= has been around for a long time. I don't
know what changed in Go 1.19. When I run your test with Go 1.18, it
fails for me.

Ian
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/bc51019f-9794-41eb-8a86-96234bc1ea11n%40googlegroups.com.

Erez Rokah

unread,
Nov 9, 2022, 3:00:35 PM11/9/22
to golang-nuts
Thanks for the response, I somehow thought that was added in Go 1.19. Sorry for the confusion and thanks again for the help
Reply all
Reply to author
Forward
0 new messages