如何检查时间戳是否为一小时

Wou*_*rti 6 python timestamp epoch modulo

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

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

Pad*_*ham 5

使用datetime.fromtimestamp

from datetime import datetime  

ts = 1415309268
cts = datetime.fromtimestamp(ts)
print(cts.minute==00)
Run Code Online (Sandbox Code Playgroud)

如果你还想要秒:

cts.minute==00 and cts.second==00
Run Code Online (Sandbox Code Playgroud)


esp*_*ang 5

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

if timestamp % 3600 == 0:
    # save the timestamp-value tuple
Run Code Online (Sandbox Code Playgroud)

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