aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjiahua wang <wjh180909@gmail.com>2021-10-20 15:21:46 +0800
committerIan Lance Taylor <iant@golang.org>2021-11-05 21:28:07 +0000
commitad6ce55a55ec445ac946e825532bde5784f8d72b (patch)
tree925c64858442e61e8ed94c0c675609be11205dc0
parent7aed6dd7e18e2ad3ac0f4eb692188ed7a90b778b (diff)
downloadgo-ad6ce55a55ec445ac946e825532bde5784f8d72b.tar.gz
go-ad6ce55a55ec445ac946e825532bde5784f8d72b.zip
time: add some examples
Change-Id: I2668cdea64f75bee87d424730d404834d69362a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/357270 Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Mui <cherryyz@google.com>
-rw-r--r--src/time/example_test.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/time/example_test.go b/src/time/example_test.go
index 0afb18aba6..ec2259b1ba 100644
--- a/src/time/example_test.go
+++ b/src/time/example_test.go
@@ -344,6 +344,23 @@ func ExampleTime_Format_pad() {
}
+func ExampleTime_GoString() {
+ t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
+ fmt.Println(t.GoString())
+ t = t.Add(1 * time.Minute)
+ fmt.Println(t.GoString())
+ t = t.AddDate(0, 1, 0)
+ fmt.Println(t.GoString())
+ t, _ = time.Parse("Jan 2, 2006 at 3:04pm (MST)", "Feb 3, 2013 at 7:54pm (UTC)")
+ fmt.Println(t.GoString())
+
+ // Output:
+ // time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
+ // time.Date(2009, time.November, 10, 23, 1, 0, 0, time.UTC)
+ // time.Date(2009, time.December, 10, 23, 1, 0, 0, time.UTC)
+ // time.Date(2013, time.February, 3, 19, 54, 0, 0, time.UTC)
+}
+
func ExampleParse() {
// See the example for Time.Format for a thorough description of how
// to define the layout string to parse a time.Time value; Parse and
@@ -401,6 +418,39 @@ func ExampleParseInLocation() {
// 2012-07-09 00:00:00 +0200 CEST
}
+func ExampleUnix() {
+ unixTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
+ fmt.Println(unixTime.Unix())
+ t := time.Unix(unixTime.Unix(), 0).UTC()
+ fmt.Println(t)
+
+ // Output:
+ // 1257894000
+ // 2009-11-10 23:00:00 +0000 UTC
+}
+
+func ExampleUnixMicro() {
+ umt := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
+ fmt.Println(umt.UnixMicro())
+ t := time.UnixMicro(umt.UnixMicro()).UTC()
+ fmt.Println(t)
+
+ // Output:
+ // 1257894000000000
+ // 2009-11-10 23:00:00 +0000 UTC
+}
+
+func ExampleUnixMilli() {
+ umt := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
+ fmt.Println(umt.UnixMilli())
+ t := time.UnixMilli(umt.UnixMilli()).UTC()
+ fmt.Println(t)
+
+ // Output:
+ // 1257894000000
+ // 2009-11-10 23:00:00 +0000 UTC
+}
+
func ExampleTime_Unix() {
// 1 billion seconds of Unix, three ways.
fmt.Println(time.Unix(1e9, 0).UTC()) // 1e9 seconds