【问题标题】:Python: puts the seconds to zero in a milliseconds numberPython:以毫秒为单位将秒数归零
【发布时间】:2018-12-21 09:05:35
【问题描述】:

我的代码如下所示:

from pymongo import MongoClient
client = MongoClient()
client = MongoClient('localhost', 27017)
db = client.local
collection = db.orderbook_update
orderbook = collection.find({}).sort('lastUpdated', pymongo.DESCENDING).limit(1)
for order in orderbook:
    print(order['lastUpdated'])

我的输出是这样的:1538589898191.0 我想做的是将此输出的秒数设为零,我不知道该怎么做......有什么帮助吗?谢谢!

【问题讨论】:

  • 1538589898191.0 的“秒”是多少?那是时间戳吗?
  • 它确实是一个以毫秒为单位的 unix 时间戳
  • 您的意思是要显示从“上次更新”到“现在”的持续时间,例如天、小时、分钟?
  • 不,只需取 lastUpdated 值 (1538589898191.0 ) 并将秒数归零
  • 顺便说一句,第一个client = MongoClient()是多余的。

标签: python python-3.x time


【解决方案1】:

一种方法是将// 除以60000 以消除毫秒和秒,然后再次乘以60 以获得秒的时间戳,或乘以60000 以获得毫秒的时间戳。

>>> t = 1538589898191.0 
>>> import time
>>> time.gmtime(t//1000)
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=3, tm_hour=18, tm_min=4, tm_sec=58, tm_wday=2, tm_yday=276, tm_isdst=0)
>>> time.gmtime(t//60000*60)
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=3, tm_hour=18, tm_min=4, tm_sec=0, tm_wday=2, tm_yday=276, tm_isdst=0)
>>> t // 60000 * 60000
1538589840000.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多