From 3d50c64ebd67b3823531a26d71b8f9a2b31a6345 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Wed, 31 Oct 2012 09:02:54 -0700 Subject: [PATCH] publish epoch --- examples.txt | 2 +- examples/epoch/epoch.go | 27 +++++++++++++++++++ examples/{epochs/epochs.sh => epoch/epoch.sh} | 2 +- examples/epochs/epochs.go | 22 --------------- examples/time/time.sh | 4 +-- 5 files changed, 31 insertions(+), 26 deletions(-) create mode 100644 examples/epoch/epoch.go rename examples/{epochs/epochs.sh => epoch/epoch.sh} (78%) delete mode 100644 examples/epochs/epochs.go diff --git a/examples.txt b/examples.txt index ed47167..d0214d1 100644 --- a/examples.txt +++ b/examples.txt @@ -50,8 +50,8 @@ Regular Expressions # JSON # Protocol Buffers Time +Epoch # Time Parsing / Formatting -# Epochs # Elapsed Time # Random Numbers Number Parsing diff --git a/examples/epoch/epoch.go b/examples/epoch/epoch.go new file mode 100644 index 0000000..1b2ac4e --- /dev/null +++ b/examples/epoch/epoch.go @@ -0,0 +1,27 @@ +// A common requirement in programs is getting the number +// of seconds, milliseconds, or nanoseconds since the +// [Unix epoch](http://en.wikipedia.org/wiki/Unix_time). +// Here's how to do it in Go. + +package main + +import "fmt" +import "time" + +func main() { + + // Use `time.Now` with `Unix` or `UnixNano` to get + // elapsed time since the Unix epoch in seconds or + // nanoseconds, respectively. + now := time.Now() + secs := now.Unix() + nanos := now.UnixNano() + + // Note that there is no `UnixMillis`, so to get the + // milliseconds since epoch you'll need to manually + // dive from nanoseconds. + millis := nanos / 1000000 + fmt.Println("secs: ", secs) + fmt.Println("millis:", millis) + fmt.Println("nanos: ", nanos) +} diff --git a/examples/epochs/epochs.sh b/examples/epoch/epoch.sh similarity index 78% rename from examples/epochs/epochs.sh rename to examples/epoch/epoch.sh index 981281c..a59ee7b 100644 --- a/examples/epochs/epochs.sh +++ b/examples/epoch/epoch.sh @@ -1,4 +1,4 @@ -$ go run epochs.go +$ go run epoch.go Secs: 1348240948 Millis: 1348240948517 Nanos: 1348240948517870000 diff --git a/examples/epochs/epochs.go b/examples/epochs/epochs.go deleted file mode 100644 index bfef479..0000000 --- a/examples/epochs/epochs.go +++ /dev/null @@ -1,22 +0,0 @@ -// A common requirement in programs is getting the number -// of seconds, milliseconds, or nanoseconds since the Unix -// epoch. Here's how to do it in Go. - -package main - -import "fmt" -import "time" - -func main() { - // Use `time.Now` with `Unix` or `UnixNano` to get - // elapsed time since the Unix epoch. - now := time.Now() - secs := now.Unix() - nanos := now.UnixNano() - - // Note that there is no `UnixMillis`. - millis := nanos / 1000000 - fmt.Println("Secs: ", secs) - fmt.Println("Millis:", millis) - fmt.Println("Nanos: ", nanos) -} diff --git a/examples/time/time.sh b/examples/time/time.sh index 398043d..53c9030 100644 --- a/examples/time/time.sh +++ b/examples/time/time.sh @@ -21,5 +21,5 @@ false 2012-10-31 15:50:13.793654 +0000 UTC 2006-12-05 01:19:43.509120474 +0000 UTC -# Learn more about Go's time support in the -# [`time`](http://golang.org/pkg/time/) package docs. +# Next we'll look at the related idea of time relative to +# the Unix epoch.