diff options
author | Kevin Burke <kevin@burke.dev> | 2021-03-25 12:33:35 -0700 |
---|---|---|
committer | Kevin Burke <kev@inburke.com> | 2021-05-02 20:59:26 +0000 |
commit | bb09f8a29b04b8fe4465d0b5d92f164979ee9213 (patch) | |
tree | 66562c9ac2facf0839f651d9061923b264eba5f9 /src/time/format_test.go | |
parent | fadad851a3222867b374e901ede9c4919594837f (diff) | |
download | go-git-bb09f8a29b04b8fe4465d0b5d92f164979ee9213.tar.gz |
time: make time.Time print a valid Go string with %#v
Previously calling fmt.Sprintf("%#v", t) on a time.Time value would
yield a result like:
time.Time{wall:0x0, ext:63724924180, loc:(*time.Location)(nil)}
which does not compile when embedded in a Go program, and does not
tell you what value is represented at a glance.
This change adds a GoString method that returns much more legible
output:
"time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.UTC)"
which gives you more information about the time.Time and also can be
usefully embedded in a Go program without additional work.
Update Quote() to hex escape non-ASCII characters (copying logic
from strconv), which makes it safer to embed them in the output of
GoString().
Fixes #39034.
Change-Id: Ic985bafe4e556f64e82223c643f65143c9a45c3b
Reviewed-on: https://go-review.googlesource.com/c/go/+/267017
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com>
Diffstat (limited to 'src/time/format_test.go')
-rw-r--r-- | src/time/format_test.go | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/time/format_test.go b/src/time/format_test.go index 09d3f842e3..1af41e2dfb 100644 --- a/src/time/format_test.go +++ b/src/time/format_test.go @@ -129,6 +129,31 @@ func TestFormat(t *testing.T) { } } +var goStringTests = []struct { + in Time + want string +}{ + {Date(2009, February, 5, 5, 0, 57, 12345600, UTC), + "time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.UTC)"}, + {Date(2009, February, 5, 5, 0, 57, 12345600, Local), + "time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.Local)"}, + {Date(2009, February, 5, 5, 0, 57, 12345600, FixedZone("Europe/Berlin", 3*60*60)), + `time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.Location("Europe/Berlin"))`, + }, + {Date(2009, February, 5, 5, 0, 57, 12345600, FixedZone("Non-ASCII character ⏰", 3*60*60)), + `time.Date(2009, time.February, 5, 5, 0, 57, 12345600, time.Location("Non-ASCII character \xe2\x8f\xb0"))`, + }, +} + +func TestGoString(t *testing.T) { + // The numeric time represents Thu Feb 4 21:00:57.012345600 PST 2009 + for _, tt := range goStringTests { + if tt.in.GoString() != tt.want { + t.Errorf("GoString (%q): got %q want %q", tt.in, tt.in.GoString(), tt.want) + } + } +} + // issue 12440. func TestFormatSingleDigits(t *testing.T) { time := Date(2001, 2, 3, 4, 5, 6, 700000000, UTC) @@ -796,10 +821,13 @@ func TestQuote(t *testing.T) { {`abc"xyz"`, `"abc\"xyz\""`}, {"", `""`}, {"abc", `"abc"`}, + {`☺`, `"\xe2\x98\xba"`}, + {`☺ hello ☺ hello`, `"\xe2\x98\xba hello \xe2\x98\xba hello"`}, + {"\x04", `"\x04"`}, } for _, tt := range tests { if q := Quote(tt.s); q != tt.want { - t.Errorf("Quote(%q) = %q, want %q", tt.s, q, tt.want) + t.Errorf("Quote(%q) = got %q, want %q", tt.s, q, tt.want) } } |