]> Cypherpunks repositories - gostls13.git/commitdiff
time: add some examples
authorjiahua wang <wjh180909@gmail.com>
Wed, 20 Oct 2021 07:21:46 +0000 (15:21 +0800)
committerIan Lance Taylor <iant@golang.org>
Fri, 5 Nov 2021 21:28:07 +0000 (21:28 +0000)
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>

src/time/example_test.go

index 0afb18aba6b4e5beb9233d54156966cb66cccfca..ec2259b1bae5e2af3454dd94731377cabcc2483b 100644 (file)
@@ -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