How to parse unix timestamp to time.Time
Solution 1
The time.Parse
function does not do Unix timestamps. Instead you can use strconv.ParseInt
to parse the string to int64
and create the timestamp with time.Unix
:
package main
import (
"fmt"
"time"
"strconv"
)
func main() {
i, err := strconv.ParseInt("1405544146", 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0)
fmt.Println(tm)
}
Output:
2014-07-16 20:55:46 +0000 UTC
Playground: http://play.golang.org/p/v_j6UIro7a
Edit:
Changed from strconv.Atoi
to strconv.ParseInt
to avoid int overflows on 32 bit systems.
Solution 2
You can directly use time.Unix function of time which converts the unix time stamp to UTC
package main
import (
"fmt"
"time"
)
func main() {
unixTimeUTC:=time.Unix(1405544146, 0) //gives unix time stamp in utc
unitTimeInRFC3339 :=unixTimeUTC.Format(time.RFC3339) // converts utc time to RFC3339 format
fmt.Println("unix time stamp in UTC :--->",unixTimeUTC)
fmt.Println("unix time stamp in unitTimeInRFC3339 format :->",unitTimeInRFC3339)
}
Output
unix time stamp in UTC :---> 2014-07-16 20:55:46 +0000 UTC
unix time stamp in unitTimeInRFC3339 format :----> 2014-07-16T20:55:46Z
Check in Go Playground: https://play.golang.org/p/5FtRdnkxAd
Solution 3
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.
func GetTimeStamp() string {
loc, _ := time.LoadLocation("America/Los_Angeles")
t := time.Now().In(loc)
return t.Format("20060102150405")
}
func GetTodaysDate() string {
loc, _ := time.LoadLocation("America/Los_Angeles")
current_time := time.Now().In(loc)
return current_time.Format("2006-01-02")
}
func GetTodaysDateTime() string {
loc, _ := time.LoadLocation("America/Los_Angeles")
current_time := time.Now().In(loc)
return current_time.Format("2006-01-02 15:04:05")
}
func GetTodaysDateTimeFormatted() string {
loc, _ := time.LoadLocation("America/Los_Angeles")
current_time := time.Now().In(loc)
return current_time.Format("Jan 2, 2006 at 3:04 PM")
}
func GetTimeStampFromDate(dtformat string) string {
form := "Jan 2, 2006 at 3:04 PM"
t2, _ := time.Parse(form, dtformat)
return t2.Format("20060102150405")
}
Solution 4
According to the go documentation, Unix returns a local time.
Unix returns the local Time corresponding to the given Unix 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:
i, err := strconv.ParseInt("1405544146", 10, 64)
if err != nil {
panic(err)
}
tm := time.Unix(i, 0)
fmt.Println(tm.UTC())
This prints on my machine (in CEST)
2014-07-16 20:55:46 +0000 UTC
Solution 5
I do a lot of logging where the timestamps are float64 and use this function to get the timestamps as string:
func dateFormat(layout string, d float64) string{
intTime := int64(d)
t := time.Unix(intTime, 0)
if layout == "" {
layout = "2006-01-02 15:04:05"
}
return t.Format(layout)
}
Related videos on Youtube
hey
Updated on May 09, 2022Comments
-
hey about 2 years
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) }
-
hey almost 10 yearsAny idea why there is no such reference in the docs? They even give the unix timestap layout as a layout example.
-
ANisus almost 10 yearsI guess it is because a unix timestamp isn't requiring parsing as it is not a text representation, but rather an integer representation of time. You just happened to have a string representation of an integer value. And the mentioning of 1136239445 in the docs is probably just to clarify what exact timestamp is to be used as reference time, not that it is to be used as layout.
-
Atmocreations about 8 yearsWouldn't it be better to use
strconv.ParseUint
instead as negative numbers don't make sense anyway? -
ANisus about 8 years@Atmocreations:
func Unix(sec int64, nsec int64) Time
receivesint64
values. Also, negative numbers for sec makes perfect sense - they describe dates prior to 1970! :) As fornsec
, negative values means to remove that many nanoseconds from the seconds. -
karthik over 7 yearsI want only the date and time as " 2014-07-16 20:55:46" how could be it achieved ?
-
Nato Boram almost 5 yearsRemoving
.In(loc)
gave me time in-0400 EDT
. Replacing it with.In(time.UTC)
gave me time in UTC. -
Dr. Jan-Philip Gehrcke over 3 years
time.Unix()
is documented to return the local time. Therefore, theunixTimeUTC
in this answer is not always providing the time in the UTC timezone. Only when the local time zone corresponds to UTC. -
WoJ almost 3 years@Dr.Jan-PhilipGehrcke: I wonder whether it ultimately matters.
time.Unix()
will parse the time into a time in some timezone (with the timezone information). It can then be reused anywhere (for instance converting to a given TZ). True, the variable name may not be fortunate in that case.