【问题标题】:How to check if a timestamp is a whole hour如何检查时间戳是否为一小时
【发布时间】:2015-01-03 13:29:15
【问题描述】:

我正在构建一个 python 应用程序,我从不同的时间序列(从 1 分钟到 1 天)中获取大量数据。我只想存储正好在一小时(xx:00 分钟)上的时间(unix 时间戳)。如何构建此检查?

一个简单的if timestamp % 3600 == 0: save the timestamp 就足够了吗?还是有更好的办法?

【问题讨论】:

  • 使用模块timedatetime
  • 就像整小时一样意味着 11:00 或 10:00 那样???
  • @Hackaholic 是的,这正是我的意思。我看到我使用了模 60。那应该是模 3600(我想要整个小时,而不是分钟)
  • 向我们展示你想要执行的数据

标签: python timestamp modulo epoch


【解决方案1】:

时间戳以秒为单位,因此时间 mod 3600 应该为零。

if timestamp % 3600 == 0:
    # save the timestamp-value tuple

这就足够了,您不必创建一个日期时间对象并检查您获得的每个时间戳的分钟和秒。

【讨论】:

    【解决方案2】:

    使用datetime.fromtimestamp

    from datetime import datetime  
    
    ts = 1415309268
    cts = datetime.fromtimestamp(ts)
    print(cts.minute==00)
    

    如果你还想要秒:

    cts.minute==00 and cts.second==00
    

    【讨论】:

    • cts.second 你可以在这里使用
    • @Andrey,op 似乎只需要分钟,我会添加秒以防万一
    • @PadraicCunningham 他说了整整一个小时,所以他自己可能没有想到秒 :)
    • @Andrey,我过去了(xx:00 分钟),但你可能是对的;)
    猜你喜欢
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2011-08-12
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多