Reputation: 14699
A unix_timestamp of 1405936049
corresponds to: 2014-07-21 09:47:29
. My goal is to derive the latter form from the timestamp.
After reading the format documentation, I came up with the following:
fmt.Println(time.Unix(1405936049, 0).Format("2006-01-02 15:04:05"))
which yields: 2014-07-21 02:47:29
, which makes sense, since time.Unix(1405936049, 0)
gives: 2014-07-21 02:47:29 -0700 PDT
(to be clear, I want: 2014-07-21 09:47:29, the hour is incorrect).
I'm sure if I knew the correct terminology, I'd be able to find a solution in the documentation, but at this point, I'm uncertain how to tell the parser to account for -0700
or perhaps an alternative solution would be to use something besides time.Unix()
, so that the resulting time would have already accounted for the hour difference? Any help would be appreciated.
Upvotes: 2
Views: 243
Reputation: 166549
You want the UTC time, not your local PDT time. For example,
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(time.Unix(1405936049, 0).UTC().Format("2006-01-02 15:04:05"))
}
Output:
2014-07-21 09:47:29
Upvotes: 3
Reputation: 20305
You have to use Location
for this:
loc, _ := time.LoadLocation("Europe/Paris")
fmt.Println(time.Unix(1405936049, 0).In(loc).Format("2006-01-02 15:04:05"))
I think the location you want is "UTC", but I let you check (otherwise, here is a list of all available locations). The reason why in playground the format is already 09:47:29
is that playground does not include locations and uses UTC by default.
Upvotes: 0