Skip to content

Commit

Permalink
Improve robustness of dumping time.Time timezone.
Browse files Browse the repository at this point in the history
The String method of time.Local may not always return "Local", e.g., if
TZ variable is set to another timezone, or if local timezone happens to
be UTC anyway.
  • Loading branch information
dmitshur committed Sep 22, 2017
1 parent 4658ef8 commit 37c2f52
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,16 @@ func (d *dumpState) dump(v reflect.Value) {
t := v.Interface().(time.Time)
switch t.IsZero() {
case false:
fmt.Fprintf(d.w, "time.Date(%d, %d, %d, %d, %d, %d, %d, time.%s)", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location().String())
var location string
switch t.Location() {
case time.UTC:
location = "time.UTC"
case time.Local:
location = "time.Local"
default:
location = fmt.Sprintf("must(time.LoadLocation(%q))", t.Location().String())
}
fmt.Fprintf(d.w, "time.Date(%d, %d, %d, %d, %d, %d, %d, %s)", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), location)
case true:
d.w.Write([]byte("time.Time{}"))
}
Expand Down

0 comments on commit 37c2f52

Please sign in to comment.