【问题标题】:Python Arrow millisecondsPython箭头毫秒
【发布时间】:2018-02-08 22:41:25
【问题描述】:

我想弄清楚一件简单的事情 - 如何将arrow.Arrow 对象转换为毫秒。我正在阅读thread,但我仍然不清楚如何以毫秒为单位获得一个长数字。

我想要类似的东西:

def get_millis(time: arrow.Arrow):
     ... some magic goes here ...


print(get_millis(time))     
OUTPUT: 
1518129553227 

谢谢

【问题讨论】:

    标签: python python-arrow


    【解决方案1】:

    基本上你要找的属性是

    float_timestamp
    

    例如

    now_millisecs = round(arrow.utcnow().float_timestamp, 3)
    now_microsecs = round(arrow.utcnow().float_timestamp, 6)
    

    如果你不喜欢浮点数,你可以从这里获取:

    str(now_millisecs).replace('.', '')
    

    出于视觉上的方便和计算(比较等)的方便,我个人保留浮点表示。

    【讨论】:

    • 如果您想要整数毫秒数,而不是四舍五入、转换为字符串、删除小数点并转换回整数,您可以乘以 1,000 然后四舍五入:@987654324 @。它更简单、更清晰、更快。
    • 也就是说,你的答案绝对是这里最好的。
    【解决方案2】:

    这是一个不雅的答案:从您的链接问题中,您可以将毫秒作为字符串获取,然后将它们添加到时间戳:

    import arrow
    now = arrow.utcnow()
    s = now.timestamp
    ms = int(now.format("SSS"))
    print(s * 1000 + ms)
    

    哪些打印:

    1518131043594
    

    【讨论】:

      【解决方案3】:
      import arrow
      
      
      def get_millis(time):
          return time.timestamp * 1000 + time.microsecond / 1000
      
      
      print(get_millis(arrow.now()))
      

      【讨论】:

        【解决方案4】:

        您也可以格式化为 ms official docs,然后解析为 int 并将其剪切到您需要的长度。

        int(arrow.utcnow().format("x")[:13])
        

        【讨论】:

          【解决方案5】:

          这是一种可读的方式:

          import arrow
          
          def get_millis():
              return int(arrow.utcnow().timestamp() * 1000)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-17
            • 1970-01-01
            • 2010-12-26
            • 2012-06-08
            • 1970-01-01
            • 2022-12-07
            • 2013-07-28
            • 2014-10-12
            相关资源
            最近更新 更多