python 按天生成时间范围序列

有的时候我们希望生成一段时间返回,比如从 2022-01-01 00:00:00 后面的 10 天,这么 10 个 datetime 对象,但是我们又不想自己去计算哪些月有30天哪些月有31天。

方法一:使用 timedelta

datetime 中包含了 timedelta ,可以用来实现这个功能

from datetime import datetime, timedelta, timezone
from pydantic.datetime_parse import parse_datetime
from loguru import logger


SECOND: int = 1
MINUTE: int = SECOND*60
HOUR: int = MINUTE*60
DAY: int = HOUR*24
WEEK: int = DAY*7
MONTH: int = DAY*30


def get_utc_now_timestamp(with_tzinfo: bool = True) -> datetime:
    """ https://blog.csdn.net/ball4022/article/details/101670024 """
    if not with_tzinfo:
        return datetime.utcnow()
    return datetime.utcnow().replace(tzinfo=timezone.utc)


def timedelta_seconds(start_time: datetime, end_time: datetime = None) -> int:
    """ 返回两个时间相差的秒数 """
    if not end_time:
        end_time = get_utc_now_timestamp()

    return int((end_time - start_time).total_seconds())


def custom_timestamp(base_timestamp: datetime, seconds: int, reduce=False):
    return base_timestamp + timedelta(seconds=seconds) \
        if not reduce \
        else base_timestamp - timedelta(seconds=seconds)


start_datetime = parse_datetime('2022-02-27 00:00:00')
data = [
    dt
    for dt in [
        custom_timestamp(start_datetime, DAY*i) for i in range(10)
    ]
]
logger.debug(data)

输出如下:

╰─➤  python -u "/Users/ponponon/Desktop/code/me/ideaboom/main.py"
2022-11-15 15:18:37.653 | DEBUG    | __main__::67 - [datetime.datetime(2022, 2, 27, 0, 0), datetime.datetime(2022, 2, 28, 0, 0), datetime.datetime(2022, 3, 1, 0, 0), datetime.datetime(2022, 3, 2, 0, 0), datetime.datetime(2022, 3, 3, 0, 0), datetime.datetime(2022, 3, 4, 0, 0), datetime.datetime(2022, 3, 5, 0, 0), datetime.datetime(2022, 3, 6, 0, 0), datetime.datetime(2022, 3, 7, 0, 0), datetime.datetime(2022, 3, 8, 0, 0)]

使用 arrow 这个第三方库

import arrow
from loguru import logger
from pydantic.datetime_parse import parse_datetime

for crawl_date in arrow.Arrow.range('day', parse_datetime('2022-02-27 00:00:00'), parse_datetime('2022-03-10 00:00:00')):
    logger.debug(crawl_date.datetime)

输出如下:

╰─➤  python -u "/Users/ponponon/Desktop/code/me/ideaboom/datetime_arrow_range.py"
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-02-27 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-02-28 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-01 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-02 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-03 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-04 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-05 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-06 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-07 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-08 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-09 00:00:00+00:00
2022-11-15 15:28:52.130 | DEBUG    | __main__::6 - 2022-03-10 00:00:00+00:00

你可能感兴趣的:(python)