Skip to content
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

time: Parse thinks _2006 means _2 followed by 006 #11334

Closed
jayp opened this issue Jun 22, 2015 · 9 comments
Closed

time: Parse thinks _2006 means _2 followed by 006 #11334

jayp opened this issue Jun 22, 2015 · 9 comments
Milestone

Comments

@jayp
Copy link

jayp commented Jun 22, 2015

The time we want to parse is of the format -> HH:MM_YYYYMMDD. Go's time.Parse seems to barf on this format.

time, err := time.Parse("15:04_20060102", "14:38_20150618")

err is not nil:

&time.ParseError{Layout:"15:04_20060102", Value:"14:38_20150618", LayoutElem:"_2", ValueElem:"_20150618", Message:""}
@jayp jayp changed the title time.Parse does not accept year after following an underscore time.Parse does not accept year (YYYY) following an underscore Jun 22, 2015
@mikioh mikioh changed the title time.Parse does not accept year (YYYY) following an underscore time: Parse does not accept year (YYYY) following an underscore Jun 23, 2015
@dtertman
Copy link

dtertman commented Jul 7, 2015

The trouble seems to be here. The code is assuming that _2 is a date example. So, the next token it finds is "006". That's unrecognized by the zero pattern, so the error is thrown.

Not sure how to fix this. The whole model of specification-by-example seems curious to me. Suppose I (oddly enough, admittedly) wanted to use the sequence "02" as my separator - in an example it would look like this: Mon02Jan022022006 which is unparsable.

@cespare
Copy link
Contributor

cespare commented Jul 7, 2015

@dtertman sufficiently exotic timestamp formats require custom parsing routines. (Not saying that's the case for the original issue, but it could be.)

@dtertman
Copy link

dtertman commented Jul 7, 2015

@cespare fair enough. The question then becomes whether _yyyy is "sufficiently exotic".

@dtertman
Copy link

dtertman commented Jul 7, 2015

Proposed fix : Allow \ to indicate that the next character is not part of the example, but text. Submitter's pattern could then be written as 15:04\_20060102 and mine from above might even be workable as Mon\0\2Jan\0\22\0\22006

EDIT : Nope. This would break backwards compatibility with any mm\dd\yyyy pattern.

@jayp
Copy link
Author

jayp commented Jul 7, 2015

FYI, I have been bypassing this bug using this specification replacement method:

func bypassTimeParseBug(s string) string {
        // NB: Looks like there is a bug in Golang's time.Parse when handing format strings
        // with _2 in the format string. Here is a snippet that exhibits this issue:
        //     t, e := time.Parse("15:04_20060102", "14:38_20150618")
        // We replace underscores with space to bypass this bug.
        return strings.Replace(s, "_", " ", -1)
}

I would posit that the format I specified is not exotic at all. At least, I didn't invent it. It's the format used by Graphite to specify absolute timestamps.

@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Jul 9, 2015
@freeformz
Copy link

Pretty much any change made is likely to break someone's time format string. But time format strings are probably almost always in ASCII. So would a unicode character that's unlikely to be part of a format string be considered as an escape character? Or _ could be special cased so that (U+2017; double low line) means a literal _.

@rsc
Copy link
Contributor

rsc commented Oct 24, 2015

I think _2006 should be parsed as _ followed by 2006, not as _2 followed by 006.
That is, there's no need for new API or notation here; just fix the current notation.

/cc @robpike

@rsc rsc changed the title time: Parse does not accept year (YYYY) following an underscore time: Parse thinks _2006 means _2 followed by 006 Oct 24, 2015
@gopherbot
Copy link

CL https://golang.org/cl/16311 mentions this issue.

@freeformz
Copy link

@rsc I was over thinking the issue, my bad. Please see my CL.

@golang golang locked and limited conversation to collaborators Nov 17, 2016
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

7 participants