Golang : Convert file unix timestamp to UTC time example
This tutorial is my own notes from helping out a friend from my working days in Indonesia. Putting it here in case it maybe useful to you.
- He wanted to use Go to process JSON data output from an old/orphaned server.
- The person in charge of the server left the company long time ago and the JSON output can't be modified at the mean time until someone take ownership of the orphaned server.
- The backup file names are differentiated by milliseconds and the JSON output will only give Unix time stamp.
NOTE : the unix time stamps are used for naming the backup directories.
A file information query to the remote server will return the following JSON encoded data:
{
"filename" : "backupCDRfiles.raw",
"lastModified" : 1439167001616
}
the lastModified field is in unix timestamp format and he needs to convert unix timestamp into UTC format for readability purpose.
The code sample below will convert the file unix timestamp to millisecond format.
package main
import (
"encoding/json"
"fmt"
"time"
)
type FileInfo struct {
FileName string `json:"filename"`
MilliSecond int64 `json:"lastModified"`
}
func (f FileInfo) PrintFileName() string {
return string(f.FileName)
}
// method to convert file unix timestamp to UTC
// need to convert f.MilliSecond to nano seconf first for time.Unix() function to work
// see http://golang.org/pkg/time/#Unix
func (f FileInfo) PrintTime() time.Time {
return time.Unix(0, f.MilliSecond*int64(time.Millisecond))
}
var JSONstr = `{
"filename" : "backupCDRfiles.raw",
"lastModified" : 1439167001616
}`
func main() {
var fInfo FileInfo
err := json.Unmarshal([]byte(JSONstr), &fInfo)
if err != nil {
panic(err)
}
fmt.Println(fInfo)
fmt.Println("File name is : ", fInfo.PrintFileName())
fmt.Println("File last modified time in millisecond : ", fInfo.PrintTime())
}
Output :
{backupCDRfiles.raw 1439167001616}
File name is : backupCDRfiles.raw
File last modified time in millisecond : 2015-08-10 00:36:41.616 +0000 UTC
Play at : https://play.golang.org/p/7fwdmp_0nO
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
+6.8k Web : How to see your website from different countries?
+8.7k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+22.9k Golang : Print out struct values in string format
+12.8k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+9.4k Golang : Format strings to SEO friendly URL example
+35.4k Golang : Get file last modified date and time
+13.3k Golang : Activate web camera and broadcast out base64 encoded images
+6.4k Golang : Join lines with certain suffix symbol example
+5.7k Golang : Extract unicode string from another unicode string example
+6.9k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+13k Android Studio : Password input and reveal password example
+5.7k Golang : Detect variable or constant type