I'm trying to parse an Unix timestamp but I get out of range error. That doesn't really makes sense to me, because the layout is correct (as in the Go docs):
package main
import "fmt"
import "time"
func main() {
tm, err := time.Parse("1136239445", "1405544146")
if err != nil{
panic(err)
}
fmt.Println(tm)
}
Just use time.Parse
example:
Working example on play.golang.org.
Sharing a few functions which I created for dates:
Please note that I wanted to get time for a particular location (not just UTC time). If you want UTC time, just remove loc variable and .In(loc) function call.
The
time.Parse
function does not do Unix timestamps. Instead you can usestrconv.ParseInt
to parse the string toint64
and create the timestamp withtime.Unix
:Output:
Playground: http://play.golang.org/p/v_j6UIro7a
Edit:
Changed from
strconv.Atoi
tostrconv.ParseInt
to avoid int overflows on 32 bit systems.}
Check this repo: https://github.com/araddon/dateparse
You can use
According to the go documentation, Unix returns a local time.
This means the output would depend on the machine your code runs on, which, most often is what you need, but sometimes, you may want to have the value in UTC.
To do so, I adapted the snippet to make it return a time in UTC:
This prints on my machine (in CEST)