125

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?

Jo Liss
  • 30,333
  • 19
  • 121
  • 170
crc
  • 1,425
  • 2
  • 11
  • 7

4 Answers4

226
import "time"
...
port[5] = time.Now().Unix()
Zombo
  • 1
  • 62
  • 391
  • 407
  • 1
    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. – Breedly Jan 28 '18 at 14:34
51

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
}
Fatih Arslan
  • 16,499
  • 9
  • 54
  • 55
  • 1
    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(...)? – Salvador Dali Jun 20 '16 at 06:15
  • 16
    @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. – fIwJlxSzApHEZIl Jun 24 '16 at 21:18
  • 7
    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. – Abu Junayd Oct 07 '18 at 12:57
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

Mike Graf
  • 5,077
  • 4
  • 45
  • 58
Nate
  • 5,237
  • 7
  • 42
  • 52
  • 1
    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 http://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? – Niriel Jan 30 '13 at 17:42
  • 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: https://stackoverflow.com/questions/14610459/how-precise-is-gos-time-really – anonymous Feb 16 '23 at 19:36
2

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.

ivanbgd
  • 171
  • 1
  • 5