aboutsummaryrefslogtreecommitdiff
path: root/src/time/example_test.go
diff options
context:
space:
mode:
authorAdrian Hesketh <adrianhesketh@hushmail.com>2017-09-01 12:38:52 +0100
committerBrad Fitzpatrick <bradfitz@golang.org>2017-11-13 18:52:13 +0000
commitd50e95261db5151955ddd3cf31241f8a2434335f (patch)
tree9d15a6e5a6eb2f84cc22a957ec37647f35f1a8dc /src/time/example_test.go
parent0cee4b7b780053425a24219866b894a46b1cfd5f (diff)
downloadgo-d50e95261db5151955ddd3cf31241f8a2434335f.tar.gz
go-d50e95261db5151955ddd3cf31241f8a2434335f.zip
time: add example for Time.Unix
Change-Id: Ie64eba5b57b609a343ddb381fe83c01f172c0bf4 Reviewed-on: https://go-review.googlesource.com/60890 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/time/example_test.go')
-rw-r--r--src/time/example_test.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/time/example_test.go b/src/time/example_test.go
index bab129db3c..8b2ac07d63 100644
--- a/src/time/example_test.go
+++ b/src/time/example_test.go
@@ -355,6 +355,26 @@ func ExampleParseInLocation() {
// 2012-07-09 00:00:00 +0200 CEST
}
+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)
+
+ // 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)
+
+ // Output:
+ // orig = 2009-01-01 01:09:30 +0000 UTC
+ // orig.Unix() = 1230772170
+ // parsed = 2009-01-01 01:09:30 +0000 UTC
+}
+
func ExampleTime_Round() {
t := time.Date(0, 0, 0, 12, 15, 30, 918273645, time.UTC)
round := []time.Duration{