convert nanoseconds since epoch to time.Time

2,116 views
Skip to first unread message

Lax Clarke

unread,
Dec 29, 2015, 11:01:37 AM12/29/15
to golang-nuts
package main

import (
"fmt"
"time"
)

func main() {
fmt.Println("Hello, playground")
tNow := time.Now()
fmt.Println("time.Now() = ", tNow)
tNano := tNow.UnixNano()
fmt.Println("time.Now().UnixNano() = ", tNano)
}

Above I convert from time.Time to nanoseconds since epoch, but I need to do the reverse.

How would I convert from tNano back to tNow in example above ?

I am guessing I need to use time.Parse, but how to do so is not clear from doc: https://golang.org/pkg/time/#Time.Nanosecond

Frits van Bommel

unread,
Dec 29, 2015, 11:12:38 AM12/29/15
to golang-nuts
Use time.Unix(0, tNano), see https://play.golang.org/p/3kzV5sOJ8L
Reply all
Reply to author
Forward
0 new messages