Obtaining a Unix Timestamp in Go Language (current time in seconds since epoch)
Asked Answered
B

4

126

I have some code written in Go which I am trying to update to work with the latest weekly builds. (It was last built under r60). Everything is now working except for the following bit:

 if t, _, err := os.Time(); err == nil {
   port[5] = int32(t)
 }

Any advice on how to update this to work with the current Go implementation?

Bookcraft answered 2/3, 2012 at 19:21 Comment(0)
M
233
import "time"
...
port[5] = time.Now().Unix()
Mose answered 2/3, 2012 at 20:4 Comment(2)
go1.9.2 darwin/amd64 this consistently returns: 1969-12-31 19:00:00, which is roughly equivalent to: time.Unix(0, 0). This is without truncation.Jun
That doesn't sound right @Breedly, or at least not any more: func (t Time) Unix() returns int64 (doc).Elisha
P
54

If you want it as string just convert it via strconv:

package main

import (
    "fmt"
    "strconv"
    "time"
)

func main() {
    timestamp := strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
    fmt.Println(timestamp) // prints: 1436773875771421417
}
Propose answered 13/7, 2015 at 7:51 Comment(3)
What is the point of this? If someone wants to convert a number to a string, he can find how to do this. Or may be I should just add another answer: if you want to convert it to a bytearray, just do []byte(...)?Demission
@SalvadorDali Actually this is exactly what I needed. Go ahead and add your answer to convert it to a byte array if you want I'm sure someone will come along eventually and need the code.Guyton
time.Now().UTC().UnixNano() can be replaced with time.Now().UnixNano() - the doc says that the result does not depend on the location associated with t.Zooplankton
B
18

Another tip. time.Now().UnixNano()(godoc) will give you nanoseconds since the epoch. It's not strictly Unix time, but it gives you sub second precision using the same epoch, which can be handy.

Edit: Changed to match current golang api

Bolyard answered 3/3, 2012 at 16:48 Comment(2)
I know I'm resurrecting an old thread, but I'm wondering... Python is clearly documenting the problems it has with getting a precise time on different platforms. See docs.python.org/2/library/time.html and search for "precision". The point is: it's not because you have a float64 that all the decimals are significant. How is Go's time implemented, how much can I trust this nanosecond precision?Cacao
You ask the operating system for the precision of whatever clock you're using. golang seems not to properly expose this, so you'd need to know which OS clock it is using, and ask it directly through a syscall. More on: #14610959Caroncarotene
Y
1

Building on the idea from another answer here, to get a human-readable interpretation, you can use:

package main

import (
    "fmt"
    "time"
)

func main() {
    timestamp := time.Unix(time.Now().Unix(), 0)
    fmt.Printf("%v", timestamp) // prints: 2009-11-10 23:00:00 +0000 UTC
}

Try it in The Go Playground.

Yemen answered 7/5, 2020 at 23:25 Comment(1)
The question does not ask for human-readable interpretation, but for the Unix timestamp, which is "the number of non-leap seconds which have passed since 00:00:00 UTC on Thursday, 1 January 1970, [and] is typically encoded as a signed integer" (reference).Elisha

© 2022 - 2024 — McMap. All rights reserved.