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.
Go's time parsing uses UTC when the format doesn't have a timezone, and has even weirder behavior when it has a zone name but no numeric offset. A caller to
cast.ToTime
cannot know if the returned time was explicitly in UTC or was set by default, so they cannot fix it. These new functions allow a user to supply a different timezone to default to, with nil using the local zone.This addresses gohugoio/hugo#1882 by allowing that code to use
cast.ToTimeInDefaultLocation(raw, nil)
.A note on the implementation: It's not possible to use
time.ParseInLocation
because parsing with a format that has a named zone but no numeric offset (such astime.RC1123Z
) returns a time with a 0 offset in that name iff that's not the current local timezone (!!). You can verify this by changing like 521 to usetime.ParseInLocation(format.format, s, defaultLocation)
and commenting out the manual override in the body of theif
clause – tests will fail.I think the root cause is that the
time
pkg has two parsing functions:time.Parse
andtime.ParseInLocation
, both of which use a private workhorse fntime.parse
. That private fn takes two zones: a default and what's considered "local". I'm not sure, but I think in order to get the effect we want, we'd need to call that fn with our default and the current local zone. However,time.Parse
uses default of UTC, whereastime.ParseInLocation
uses the given zone for both default & local.