Failing to parse a specific date/time format

2,713 views
Skip to first unread message

Alexandru Ungur

unread,
Dec 17, 2013, 1:22:44 PM12/17/13
to golan...@googlegroups.com
Hi all,

Trying to learn about time parsing.
Played with some simple examples, but I just can't figure out how to parse this one: "2012-12-05 23:00:34 +0200". The error I get is "parsing time "2012-12-05 23:00:34 +0200": month out of range".
Sample here code here: http://play.golang.org/p/tvPekI8AVS

Any ideas, pointers, etc. greatly appreciated. Nor 12 neither 05 seem like out of range for a month (unless months are counted from 0?).

Thank you,

Cheers!
Alex Ungur

luz...@gmail.com

unread,
Dec 18, 2013, 8:51:29 AM12/18/13
to golan...@googlegroups.com

chris dollin

unread,
Dec 18, 2013, 8:54:00 AM12/18/13
to Alexandru Ungur, golang-nuts
The first argument to time.Parse mist be your chosen format's representation
of the reference time (Mon Jan _2 15:04:05 2006). It certainly doesn't look like
that's true, so I suspect that whatever this does to Parse's understanding of
the comversion makes the result illegal.
Chris

--
Chris "allusive" Dollin

Alexandru Ungur

unread,
Dec 19, 2013, 4:41:48 PM12/19/13
to golan...@googlegroups.com
Thank you very much and sorry for the noise, I figured that out shortly after posting :( by reading the time package help slower this time...

Have a great day!
Alex Ungur

Kevin Gillette

unread,
Dec 19, 2013, 6:19:45 PM12/19/13
to golan...@googlegroups.com, luz...@gmail.com
This really could use a better error message, though it's unclear how to achieve that while still meeting Go 1 compatibility guarantees. Given http://play.golang.org/p/5nALeDw-IJ (2007 in the format string instead of 2006), it seems the format is parsed as `<1 digit day><3 digit offset><2 digit month><2 digit day>...` -- both the day and offset are recognized twice, while the year isn't recognized at all. While technically legitimate, intentional uses of a format like this would be exceedingly rare; we could check this in go vet, if not done already, but few of the people who've run into this issue know about or use vet.

If error messages are not covered by the Go 1 guarantee, then if a date string parse error occurs, and the format string contains duplicate or undelimited, odd sequences of fields, then the error message could be expanded to indicate that the format date must correspond to the reference time.
Reply all
Reply to author
Forward
0 new messages