英文:
How to parse a milliseconds-since-epoch timestamp string in Go?
问题
我有一个自纪元以来的毫秒字符串(它最初来自于java.lang.System.currentTimeMillis()
调用)。在Go中,将此字符串转换为可读的时间戳字符串的正确方法是什么?
查看time包,我看到了Parse
函数,但是布局似乎都是基于正常时区的时间。一旦转换为Time对象,我可以使用Format(Time.Stamp)
来获得我想要的输出,但是我不清楚如何将字符串转换为Time对象。
英文:
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.
答案1
得分: 41
格式字符串不支持自纪元以来的毫秒数,因此您需要手动解析。例如:
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
}
请访问http://play.golang.org/p/M1dWGLT8XE以实时尝试示例。
英文:
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.
答案2
得分: 5
2021更新:您可以使用UnixMilli
函数来从整数解析时间。这将改进接受的答案:
func msToTime(ms string) (time.Time, error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.UnixMilli(msInt), nil
}
英文:
2021 update: you can use UnixMilli
to parse from the integer. This would improve the accepted answer to:
func msToTime(ms string) (time.Time, error) {
msInt, err := strconv.ParseInt(ms, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.UnixMilli(msInt), nil
}
答案3
得分: 3
接受的答案对于一些日期范围来说,要么太远的未来,要么太远的过去是不起作用的,因为只有64位可以描述一个有限的日期范围(从纪元开始的正负300年)。
这将适用于可以用毫秒描述的整个范围:
var Epoch = time.Unix(0, 0)
func ParseMillisecondUnixTimestamp(s string) (time.Time, error) {
ts, err := strconv.Atoi(s)
if err != nil {
return time.Time{}, err
}
return Epoch.Add(time.Duration(ts) * time.Millisecond), nil
}
英文:
The accepted answer will not work for some date ranges either too far in the future or too far in the past because only a limited date range can be described using 64 bits (plus or minus 300 years from Epoch).
This will work for the entire range describable by milliseconds:
var Epoch = time.Unix(0, 0)
func ParseMillisecondUnixTimestamp(s string) (time.Time, error) {
ts, err := strconv.Atoi(s)
if err != nil {
return time.Time{}, err
}
return Epoch.Add(time.Duration(ts) * time.Millisecond), nil
}
答案4
得分: -1
使用time.Unix(ms/1000, 0)
可以解决问题。
首先使用已知的关系1秒=1000毫秒
将毫秒时间戳转换为Unix时间戳,然后将结果作为time.Unix
函数的第一个参数使用。
英文:
Using time.Unix(ms/1000, 0)
would do the trick.
First convert the ms-timestamp to unix-timestamp using the well known relation 1 s = 1000 ms
and then use the result as first argument to the time.Unix
function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论