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
+11k Golang : List running EC2 instances and descriptions
+4.8k Cash Flow : 50 days to pay your credit card debt
+12k Golang : Get user input until a command or receive a word to stop
+5.8k Golang : Output or print out JSON stream/encoded data
+11.7k Generate salted password with OpenSSL example
+19.1k Golang : How to force compile or remove object files first before rebuild?
+18.2k Golang : Convert seconds to human readable time format example
+7k Findstr command the Grep equivalent for Windows
+25.7k Golang : Find files by name - cross platform example
+8.8k Golang : Test a slice of integers for odd and even numbers
+8.2k Golang : Extract or copy items from map based on value
+6k Golang : Mapping Iban to Dunging alphabets