Skip to content

Commit

Permalink
pkg/time: fix hermeticity issue
Browse files Browse the repository at this point in the history
Time parsing was dependent on Location.

Fixes #1522

Change-Id: Ibf43e2f89e92eb1660d8f24dd25ba5a7afc01c8b
Signed-off-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Feb 18, 2022
1 parent 910ff4d commit afbbbb2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
17 changes: 17 additions & 0 deletions pkg/time/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import "time"
t1: time.Time & "1937-01-01T12:00:27.87+00:20"
t2: time.Time & "no time"
t3: time.Unix(1500000000, 123456)

parse: {
t1: time.Parse(time.RFC822, "01 Jul 21 17:54 CEST")
t1: time.Parse(time.RFC822, "01 Jul 21 17:54 CDST")
t1: time.Parse(time.RFC822, "01 Jul 21 17:54 PST")
t1: time.Parse(time.RFC822, "01 Jul 21 17:54 PDT")
t1: time.Parse(time.RFC822, "01 Jul 21 17:54 EST")
t1: time.Parse(time.RFC822, "01 Jul 21 17:54 EDT")

_layout: "01/02 03:04:05PM '06 -0700"
t2: time.Parse(_layout, _layout)
}

-- out/time --
Errors:
t2: invalid value "no time" (does not satisfy time.Time): error in call to time.Time: invalid time "no time":
Expand All @@ -15,4 +28,8 @@ Result:
t1: "1937-01-01T12:00:27.87+00:20"
t2: _|_ // t2: invalid value "no time" (does not satisfy time.Time): error in call to time.Time: invalid time "no time"
t3: "2017-07-14T02:40:00.000123456Z"
parse: {
t1: "2021-07-01T17:54:00Z"
t2: "2006-01-02T22:04:05Z"
}

16 changes: 5 additions & 11 deletions pkg/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func Time(s string) (bool, error) {
}

func timeFormat(value, layout string) (bool, error) {
_, err := time.Parse(layout, value)
_, err := time.ParseInLocation(layout, value, nil)
if err != nil {
// Use our own error, the time package's error as the Go error is too
// confusing within this context.
Expand Down Expand Up @@ -175,17 +175,11 @@ func Format(value, layout string) (bool, error) {
// location and zone in the returned time. Otherwise it records the time as
// being in a fabricated location with time fixed at the given zone offset.
//
// When parsing a time with a zone abbreviation like MST, if the zone abbreviation
// has a defined offset in the current location, then that offset is used.
// The zone abbreviation "UTC" is recognized as UTC regardless of location.
// If the zone abbreviation is unknown, Parse records the time as being
// in a fabricated location with the given zone abbreviation and a zero offset.
// This choice means that such a time can be parsed and reformatted with the
// same layout losslessly, but the exact instant used in the representation will
// differ by the actual zone offset. To avoid such problems, prefer time layouts
// that use a numeric zone offset, or use ParseInLocation.
// Parse currently does not support zone abbreviations like MST. All are
// interpreted as UTC.
func Parse(layout, value string) (string, error) {
t, err := time.Parse(layout, value)
// TODO: support named variadic argument "location:" to pass in a location.
t, err := time.ParseInLocation(layout, value, nil)
if err != nil {
return "", err
}
Expand Down

0 comments on commit afbbbb2

Please sign in to comment.