Skip to content

Commit

Permalink
Respect zero time and print time.Time{}
Browse files Browse the repository at this point in the history
  • Loading branch information
SchumacherFM authored and alecthomas committed Sep 25, 2020
1 parent f2046c2 commit 8eaab51
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ func isZero(v reflect.Value) bool {
}

func timeToGo(w io.Writer, t time.Time) {
if t.IsZero() {
fmt.Fprint(w, "time.Time{}")
return
}

var zone string
switch loc := t.Location(); loc {
case nil:
Expand Down
4 changes: 3 additions & 1 deletion repr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ func TestReprTime(t *testing.T) {
nil,
{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, time.UTC)},
{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, loc)},
{Date: time.Time{}},
}
const want = "[]*repr.timeStruct{&repr.timeStruct{Date: time.Date(2001, 5, 13, 21, 15, 54, 987654, time.FixedZone(\"Repr\", 10800))}, nil, &repr.timeStruct{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, time.UTC)}, &repr.timeStruct{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, time.FixedZone(\"AEDT\", 39600))}}"
const want = "[]*repr.timeStruct{&repr.timeStruct{Date: time.Date(2001, 5, 13, 21, 15, 54, 987654, time.FixedZone(\"Repr\", 10800))}, nil, &repr.timeStruct{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, time.UTC)}, &repr.timeStruct{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, time.FixedZone(\"AEDT\", 39600))}, &repr.timeStruct{Date: time.Time{}}}"
s := String(arr)
equal(t, want, s)

Expand All @@ -154,6 +155,7 @@ func TestReprTime(t *testing.T) {
nil,
{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, time.UTC)},
{Date: time.Date(2011, 3, 23, 11, 15, 54, 987654, time.FixedZone("AEDT", 39600))},
{Date: time.Time{}},
}
s = String(arr)
equal(t, want, s)
Expand Down

0 comments on commit 8eaab51

Please sign in to comment.