Golang : Convert Unix timestamp to UTC timestamp
Problem :
You have a Unix timestamp and you want to convert it to UTC time format. How to do that in Golang?
For example :
>date +%s
1432572732
Solution :
Use the strconv.ParseInt()
function to convert unix time stamp to integer value first and then use time.Unix()
to convert the integer value to proper UTC (Coordinated Universal Time) time stamp.
Here you go:
package main
import (
"fmt"
"os"
"strconv"
"time"
)
func main() {
unixTimeStamp := "1432572732"
unixIntValue, err := strconv.ParseInt(unixTimeStamp, 10, 64)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
timeStamp := time.Unix(unixIntValue, 0)
fmt.Println(timeStamp)
}
Output :
2015-05-25 16:52:12 +0000 UTC
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+15.5k Golang : Get input from keyboard
+3.9k HTTP common errors and their meaning explained
+15.2k Golang : Send email and SMTP configuration example
+19.6k Curl usage examples with Golang
+21.1k Golang : Convert Unix timestamp to UTC timestamp
+35.2k Golang : Convert(cast) int64 to string
+9.7k Golang : Removes punctuation or defined delimiter from the user's input
+7.9k Golang : Generate EAN barcode
+42.4k Golang : Use wildcard patterns with filepath.Glob() example
+12.3k Golang : Convert spaces to tabs and back to spaces example
+8.5k Golang : Eroding and dilating image with OpenCV example
+11.7k Golang : Skip blank/empty lines in CSV file and trim whitespaces example