Python 检查白天时间

4 投票
4 回答
2482 浏览
提问于 2025-04-16 00:48

基本上,我想让我的脚本在凌晨4点到5点之间暂停。到目前为止,我想到的唯一方法就是这样:

seconds_into_day = time.time() % (60*60*24)
if 60*60*4 < seconds_into_day < 60*60*5:
    sleep(time_left_till_5am)

有没有什么“正确”的方法来做到这一点?也就是说,有没有什么内置的函数或者库可以用来计算时间,而不是总是用秒数?

4 个回答

1

下面的代码处理了一种更普遍的情况,就是当一个脚本需要在任何少于24小时的固定时间段内暂停时。举个例子:比如说,它必须在晚上11点到凌晨1点之间休眠。

import datetime as dt

def sleep_duration(sleep_from, sleep_to, now=None):
    # sleep_* are datetime.time objects
    # now is a datetime.datetime object
    if now is None:
        now = dt.datetime.now()
    duration = 0
    lo = dt.datetime.combine(now, sleep_from)
    hi = dt.datetime.combine(now, sleep_to)
    if lo <= now < hi:
        duration = (hi - now).seconds
    elif hi < lo:
        if now >= lo:
            duration = (hi + dt.timedelta(hours=24) - now).seconds
        elif now < hi:
            duration = (hi - now).seconds
    return duration

tests = [
    (4, 5, 3, 30),
    (4, 5, 4, 0),
    (4, 5, 4, 30),
    (4, 5, 5, 0),
    (4, 5, 5, 30),
    (23, 1, 0, 0),
    (23, 1, 0, 30),
    (23, 1, 0, 59),
    (23, 1, 1, 0),
    (23, 1, 1, 30),
    (23, 1, 22, 30),
    (23, 1, 22, 59),
    (23, 1, 23, 0),
    (23, 1, 23, 1),
    (23, 1, 23, 59),
    ]

for hfrom, hto, hnow, mnow in tests:
    sfrom = dt.time(hfrom)
    sto = dt.time(hto)
    dnow = dt.datetime(2010, 7, 5, hnow, mnow)
    print sfrom, sto, dnow, sleep_duration(sfrom, sto, dnow)

这是运行后的结果:

04:00:00 05:00:00 2010-07-05 03:30:00 0
04:00:00 05:00:00 2010-07-05 04:00:00 3600
04:00:00 05:00:00 2010-07-05 04:30:00 1800
04:00:00 05:00:00 2010-07-05 05:00:00 0
04:00:00 05:00:00 2010-07-05 05:30:00 0
23:00:00 01:00:00 2010-07-05 00:00:00 3600
23:00:00 01:00:00 2010-07-05 00:30:00 1800
23:00:00 01:00:00 2010-07-05 00:59:00 60
23:00:00 01:00:00 2010-07-05 01:00:00 0
23:00:00 01:00:00 2010-07-05 01:30:00 0
23:00:00 01:00:00 2010-07-05 22:30:00 0
23:00:00 01:00:00 2010-07-05 22:59:00 0
23:00:00 01:00:00 2010-07-05 23:00:00 7200
23:00:00 01:00:00 2010-07-05 23:01:00 7140
23:00:00 01:00:00 2010-07-05 23:59:00 3660
3

你需要用到datetime模块。

这个datetime模块提供了一些类,可以用来简单或复杂地处理日期和时间。

如果你使用date.hourdatetime.now()中获取,你就能得到当前的小时数:

datetimenow = datetime.now();
if datetimenow.hour in range(4, 5)
    sleep(time_left_till_5am)

你可以通过计算time_left_till_5am来得到距离早上5点还有多少时间,方法是用60 - datetimenow.minute的结果乘以60,然后再加上60 - datetimenow.second

2

Python有一个内置的日期时间库,你可以在这里找到它的文档:http://docs.python.org/library/datetime.html

这个库应该能帮你解决问题:

import datetime as dt
from time import sleep

now = dt.datetime.now()

if now.hour >= 4 andnow.hour < 5:
    sleep((60 - now.minute)*60 + (60 - now.second))

好吧,上面的代码可以工作,但这里有一个更简单、更不容易出错的解决方案(其实我最开始想到的就是这个,但突然忘了怎么做):

import datetime as dt
from time import sleep

now = dt.datetime.now()
pause = dt.datetime(now.year, now.month, now.day, 4)
start = dt.datetime(now.year, now.month, now.day, 5)

if now >= pause and now < start:
    sleep((start - now).seconds)

这就是我之前提到的“timedelta”的来源——当你从两个日期时间对象中相减时,得到的是一个timedelta对象(在这个例子中,我们从中提取“秒”这个属性)。

撰写回答