Repository navigation
fix(time): parse layouts like Go's time.Parse - #255
Merged
Merged
Conversation
Replace the JS Date based Parse/ParseInLocation stub with a port of Go's layout-driven parser, accept *Location values held in variables or fields, port ParseDuration for compound durations, build Date values correctly for years below 100, and format zero offsets as Z and whole-hour durations as Go does. Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
Member
|
Landed on master as 9439851. I folded package_import_time_parse_location_field into package_import_time_parse_layout. In parseTime, a local fail helper replaces the eight [new Time(), newParseError(alayout, avalue, ...)] returns, the time at a parsed offset is computed once, and the field declarations and result branches have paragraph comments. I also renamed parseBad and layoutChunk to PascalCase and shared one TextEncoder in quoteParseValue. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
time.Parse and time.ParseInLocation were a stub built on the JS Date parser. They accepted values that do not match the layout (for example, "2025-05-15" parsed with RFC3339) and ignored the location for layouts like DateTime, using the host zone instead. github.com/BurntSushi/toml relies on Go's behavior: it tries a list of layouts in order and uses the first that parses, so the stub gave TOML dates and times the wrong type and value. ParseInLocation also did not accept a *Location held in a variable or struct field, which BurntSushi passes, so its parser failed to typecheck.
This replaces the stub with a port of Go's layout-driven parser from time/format.go: the same chunk tokenization, strict literal matching, fractional seconds (including implicit ones after the seconds field), numeric and named zones, day-of-year, range checks, and "extra text" errors, with ParseError messages that match Go's text. Zone offsets are matched against the given location, as in Go.
Related fixes in the same area:
The new fixtures package_import_time_parse_layout and package_import_time_parse_location_field cover BurntSushi's layouts with matching and non-matching values, error text, other layout elements, ParseDuration, and *Location struct fields. The full compiler suite, the gs/time unit tests, and typecheck pass.