donglu9872 2012-11-08 17:41
浏览 654
已采纳

如何在Go中解析毫秒级以来的时间戳字符串?

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条回答 默认 最新

  • 普通网友 2012-11-08 18:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择