I've got a string with milliseconds since the epoch (it came originally from a java.lang.System.currentTimeMillis()
call). What's the right way to convert this string into a human readable timestamp string in Go?
Looking at the time package I see the Parse
function, but the layouts all seem to be normal timezone-based times. Once into a Time object, I can use Format(Time.Stamp)
to get the output I want, but I'm not clear on how to get the string into a Time object.
The format string does not support milliseconds since the epoch, so you need to parse manually. For example:
func msToTime(ms string) (time.Time, error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(0, msInt*int64(time.Millisecond)), nil
}
Check out http://play.golang.org/p/M1dWGLT8XE to play with the example live.