]> Cypherpunks.ru repositories - gostls13.git/commit
time: make time.Time print a valid Go string with %#v
authorKevin Burke <kevin@burke.dev>
Thu, 25 Mar 2021 19:33:35 +0000 (12:33 -0700)
committerKevin Burke <kev@inburke.com>
Sun, 2 May 2021 20:59:26 +0000 (20:59 +0000)
commitbb09f8a29b04b8fe4465d0b5d92f164979ee9213
tree66562c9ac2facf0839f651d9061923b264eba5f9
parentfadad851a3222867b374e901ede9c4919594837f
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>
doc/go1.17.html
src/time/format.go
src/time/format_test.go