publish epoch

This commit is contained in:
Mark McGranaghan 2012-10-31 09:02:54 -07:00
parent 13ef82a928
commit 3d50c64ebd
5 changed files with 31 additions and 26 deletions

View File

@ -50,8 +50,8 @@ Regular Expressions
# JSON
# Protocol Buffers
Time
Epoch
# Time Parsing / Formatting
# Epochs
# Elapsed Time
# Random Numbers
Number Parsing

27
examples/epoch/epoch.go Normal file
View File

@ -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)
}

View File

@ -1,4 +1,4 @@
$ go run epochs.go
$ go run epoch.go
Secs: 1348240948
Millis: 1348240948517
Nanos: 1348240948517870000

View File

@ -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)
}

View File

@ -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.