time: add examples for AppendBinary and AppendText

Change-Id: I61529b5162f8a77d3bbffcbbac98c834a7626e3a
Reviewed-on: https://go-review.googlesource.com/c/go/+/661935
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
cuishuang 2025-04-01 11:46:17 +08:00 committed by Gopher Robot
parent 8433412b74
commit 3acd440219

View File

@ -754,6 +754,31 @@ func ExampleTime_Sub() {
// difference = 12h0m0s
}
func ExampleTime_AppendBinary() {
t := time.Date(2025, 4, 1, 15, 30, 45, 123456789, time.UTC)
var buffer []byte
buffer, err := t.AppendBinary(buffer)
if err != nil {
panic(err)
}
var parseTime time.Time
err = parseTime.UnmarshalBinary(buffer[:])
if err != nil {
panic(err)
}
fmt.Printf("t: %v\n", t)
fmt.Printf("parseTime: %v\n", parseTime)
fmt.Printf("equal: %v\n", parseTime.Equal(t))
// Output:
// t: 2025-04-01 15:30:45.123456789 +0000 UTC
// parseTime: 2025-04-01 15:30:45.123456789 +0000 UTC
// equal: true
}
func ExampleTime_AppendFormat() {
t := time.Date(2017, time.November, 4, 11, 0, 0, 0, time.UTC)
text := []byte("Time: ")
@ -765,6 +790,21 @@ func ExampleTime_AppendFormat() {
// Time: 11:00AM
}
func ExampleTime_AppendText() {
t := time.Date(2025, 4, 1, 15, 30, 45, 123456789, time.UTC)
buffer := []byte("t: ")
buffer, err := t.AppendText(buffer)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", buffer)
// Output:
// t: 2025-04-01T15:30:45.123456789Z
}
func ExampleFixedZone() {
loc := time.FixedZone("UTC-8", -8*60*60)
t := time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)