Go convert int64 (from UnixNano) to location time string -


i have int64 like:

1502712864232

which result of rest service. can convert string enough. unixnano timestamp.

what need convert string related timezone "europe/london". such as:-

"14/08/2017, 13:14:24"

such produced handy utility: http://www.freeformatter.com/epoch-timestamp-to-date-converter.html

would appreciate assistance.

==> update.

thanks @evanmcdonnal such useful answer. appreciated. turns out data had not unixnano @ (sorry) milliseconds epoch. source jenkins timestamp....

so... wrote following helper function need:

// arg 1 int64 representing millis since epoch // arg 2 timezome. eg: "europe/london" // arg 3 int relating formatting of returned string // needs time package. obviously.  func getformattedtimefromepochmillis(z int64, zone string, style int) string {     var x string     secondssinceepoch := z / 1000     unixtime := time.unix(secondssinceepoch, 0)     timezonelocation, err := time.loadlocation(zone)     if err != nil {         fmt.println("error loading timezone:", err)     }      timeinzone := unixtime.in(timezonelocation)      switch style {         case 1:             timeinzonestyleone := timeinzone.format("mon jan 2 15:04:05")             //mon aug 14 13:36:02             return timeinzonestyleone         case 2:             timeinzonestyletwo := timeinzone.format("02-01-2006 15:04:05")             //14-08-2017 13:36:02             return timeinzonestyletwo         case 3:             timeinzonestylethree := timeinzone.format("2006-02-01 15:04:05")             //2017-14-08 13:36:02             return timeinzonestylethree     }     return x } 

rather converting string, you'll want convert time.time , there string. can use handy unix method time object time stamp.

import "time" import "fmt"  t := time.unix(0, 1502712864232) fmt.println(t.format("02/01/2006, 15:04:05")) 

edit: added format println - note, testing unix stamp in go playground, value neither nano seconds nor seconds, in both cases time value produced way off of should be. code above still demonstrates basic idea of want seems additional step necessary or sample int64 gave not correspond string provided.

relevant docs:

https://golang.org/pkg/time/#unix

https://golang.org/pkg/fmt/


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -