diff options
author | Russ Cox <rsc@golang.org> | 2017-12-05 21:05:52 -0500 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2017-12-06 03:57:37 +0000 |
commit | bb22a697ab30358605292f435ca75d1badcb212c (patch) | |
tree | 272f0194be47557cf02b47e37d80ea44dea349eb /src/time/example_test.go | |
parent | f6be5216279ac0837f895eaeb55be5e9f4ac3ea9 (diff) | |
download | go-bb22a697ab30358605292f435ca75d1badcb212c.tar.gz go-bb22a697ab30358605292f435ca75d1badcb212c.zip |
time: condense, expand Time.Unix example
The new example is shorter but illustrates the
interesting parts of the Unix function and methods.
Change-Id: Ief8ec38909d4ed7829e8d3da58e7b7f712537f99
Reviewed-on: https://go-review.googlesource.com/82079
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/time/example_test.go')
-rw-r--r-- | src/time/example_test.go | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/time/example_test.go b/src/time/example_test.go index 8b2ac07d63..8c64506027 100644 --- a/src/time/example_test.go +++ b/src/time/example_test.go @@ -356,23 +356,21 @@ func ExampleParseInLocation() { } func ExampleTime_Unix() { - // Create a date. - const nsecs = 0 - orig := time.Date(2009, time.January, 1, 1, 9, 30, nsecs, time.UTC) - fmt.Printf("orig = %v\n", orig) + // 1 billion seconds of Unix, three ways. + fmt.Println(time.Unix(1e9, 0).UTC()) // 1e9 seconds + fmt.Println(time.Unix(0, 1e18).UTC()) // 1e18 nanoseconds + fmt.Println(time.Unix(2e9, -1e18).UTC()) // 2e9 seconds - 1e18 nanoseconds - // Get the Unix timestamp of the date. - unix := orig.Unix() - fmt.Printf("orig.Unix() = %v\n", unix) - - // Convert the Unix date back to a time.Time. - parsed := time.Unix(unix, nsecs).UTC() - fmt.Printf("parsed = %v\n", parsed) + t := time.Date(2001, time.September, 9, 1, 46, 40, 0, time.UTC) + fmt.Println(t.Unix()) // seconds since 1970 + fmt.Println(t.UnixNano()) // nanoseconds since 1970 // Output: - // orig = 2009-01-01 01:09:30 +0000 UTC - // orig.Unix() = 1230772170 - // parsed = 2009-01-01 01:09:30 +0000 UTC + // 2001-09-09 01:46:40 +0000 UTC + // 2001-09-09 01:46:40 +0000 UTC + // 2001-09-09 01:46:40 +0000 UTC + // 1000000000 + // 1000000000000000000 } func ExampleTime_Round() { |