Skip to content

Commit 3acd440

Browse files
cuishuanggopherbot
authored andcommitted
time: add examples for AppendBinary and AppendText
Change-Id: I61529b5162f8a77d3bbffcbbac98c834a7626e3a Reviewed-on: https://go-review.googlesource.com/c/go/+/661935 Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Sean Liao <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 8433412 commit 3acd440

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/time/example_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,31 @@ func ExampleTime_Sub() {
754754
// difference = 12h0m0s
755755
}
756756

757+
func ExampleTime_AppendBinary() {
758+
t := time.Date(2025, 4, 1, 15, 30, 45, 123456789, time.UTC)
759+
760+
var buffer []byte
761+
buffer, err := t.AppendBinary(buffer)
762+
if err != nil {
763+
panic(err)
764+
}
765+
766+
var parseTime time.Time
767+
err = parseTime.UnmarshalBinary(buffer[:])
768+
if err != nil {
769+
panic(err)
770+
}
771+
772+
fmt.Printf("t: %v\n", t)
773+
fmt.Printf("parseTime: %v\n", parseTime)
774+
fmt.Printf("equal: %v\n", parseTime.Equal(t))
775+
776+
// Output:
777+
// t: 2025-04-01 15:30:45.123456789 +0000 UTC
778+
// parseTime: 2025-04-01 15:30:45.123456789 +0000 UTC
779+
// equal: true
780+
}
781+
757782
func ExampleTime_AppendFormat() {
758783
t := time.Date(2017, time.November, 4, 11, 0, 0, 0, time.UTC)
759784
text := []byte("Time: ")
@@ -765,6 +790,21 @@ func ExampleTime_AppendFormat() {
765790
// Time: 11:00AM
766791
}
767792

793+
func ExampleTime_AppendText() {
794+
t := time.Date(2025, 4, 1, 15, 30, 45, 123456789, time.UTC)
795+
796+
buffer := []byte("t: ")
797+
798+
buffer, err := t.AppendText(buffer)
799+
if err != nil {
800+
panic(err)
801+
}
802+
803+
fmt.Printf("%s\n", buffer)
804+
805+
// Output:
806+
// t: 2025-04-01T15:30:45.123456789Z
807+
}
768808
func ExampleFixedZone() {
769809
loc := time.FixedZone("UTC-8", -8*60*60)
770810
t := time.Date(2009, time.November, 10, 23, 0, 0, 0, loc)

0 commit comments

Comments
 (0)