## 简介
Arrow是一个 Python 库,它提供了一种明智且人性化的方法来创建、操作、格式化和转换日期、时间和时间戳。它实现并更新 datetime 类型,填补了功能上的空白,并提供了支持许多常见创建方案的智能模块 API。简而言之,它可以帮助您以更少的导入和更少的代码来处理日期和时间。
## 为什么使用 Arrow 而不是内置模块?
Python的标准库和其他一些低级模块具有接近完整的日期,时间和时区功能,但从可用性的角度来看效果不佳:
太多模块:datetime、time、calendar、dateutil、pytz等
类型太多:date、time、datetime、tzinfo、timedelta、relativedelta 等。
时区和时间戳转换是冗长和不愉快的
时区幼稚是常态
功能差距:ISO 8601 解析、时间跨度、人性化
arrow.now().float_timestamp
now.format('YYYY-MM-DD HH:mm:ss ZZ')
now.format('YYYY-MM-DD HH:mm:ss')
arrow.get(1651800761).format('YYYY-MM-DD HH:mm:ss')
arrow.get("Cool date: 2019-10-31T09:12:45.123456+04:30.", "YYYY-MM-DDTHH:mm:ss.SZZ")
arrow.get("Tomorrow (2019-10-31) is Halloween!", "YYYY-MM-DD")
arrow.get("Halloween is on 2019.10.31.", "YYYY.MM.DD")
arrow.get("It's Halloween tomorrow (2019-10-31)!", "YYYY-MM-DD")
arrow.get("2022-05-01").int_timestamp
arrow.get("2022-05-01").float_timestamp
arrow.get("2022-05-01").timestamp()
now.shift(days=3).format('YYYY:MM:DD HH:mm:ss')
now.shift(days=-2).format('YYYY:MM:DD HH:mm:ss')
now.shift(years=-1).format('YYYY:MM:DD HH:mm:ss')
now.shift(months=2).format('YYYY:MM:DD HH:mm:ss')
now.shift(hours=2).format('YYYY:MM:DD HH:mm:ss')
now.shift(minutes=-1).format('YYYY:MM:DD HH:mm:ss')
now.shift(seconds=-30).format('YYYY:MM:DD HH:mm:ss')
now.shift(hours=2, minutes=-1, seconds=-30).format('YYYY:MM:DD HH:mm:ss')
now.humanize(locale='zh-cn')
future = now.shift(hours=2)
future.humanize(now, locale='zh-cn')

from datetime import date
from boltons.timeutils import daterange
from typing import Any, Union, Text, List, AnyStr
def get_appointed_date(self) -> List:
step (int):元组 (year, month, day)
inclusive (bool) :是否包含 stop 的日期
return: [datetime.date(2022, 1, 25), datetime.date(2022, 2, 25)]
return [day for day in daterange(date(year=2022, month=1, day=25),
date(year=2023, month=1, day=1),
def get_add_date(self, default_date={"year": 2022, "month": 11, "day": 1}, date_format='%Y-%m-%d',
weeks=0, days=1, hours=0, minutes=0, seconds=0, milliseconds=0, microseconds=0):
from datetime import datetime
from datetime import timedelta
handle_time = datetime(default_date["year"], default_date["month"], default_date["day"]) + timedelta(weeks=weeks, days=days, hours=hours, minutes=minutes, seconds=seconds, milliseconds=milliseconds, microseconds=microseconds, )
date = handle_time.strftime(date_format)
if __name__ == '__main__':
print(test.get_appointed_date())
print(test.get_add_date())
from typing import List, Any
class Handle_arrow_time():
def get_someDate_byDescribe(self, date_describe: str, format: str = "YYYY-MM-DD HH:mm:ss") -> Any:
if date_describe == 'today':
return arrow.now().format(format)
elif date_describe == 'tomorrow':
return arrow.now().shift(days=1).format(format)
elif date_describe == 'yesterday':
return arrow.now().shift(days=-1).format(format)
elif date_describe.endswith('days later'):
date_num = int(date_describe.split(' ')[0])
return arrow.now().shift(years=0, months=0, days=date_num, hours=0, minutes=0, seconds=0).format(format)
elif date_describe.endswith('days ago'):
date_num = int(date_describe.split(' ')[0])
return arrow.now().shift(years=0, months=0, days=0 - date_num, hours=0, minutes=0, seconds=0).format(format)
def get_someDate_byTimeKey(self, years: int = 0, months: int = 0, days: int = 0, hours: int = 0, minutes: int = 0, seconds: int = 0, format: str = "YYYY-MM-DD HH:mm:ss") -> str:
return arrow.now().shift(years=years, months=months, days=days, hours=hours, minutes=minutes, seconds=seconds).format(format)
def get_nowDate_someAttribute(self, key="year") -> Any:
elif key == "int_timestamp":
elif key == "float_timestamp":
return now.float_timestamp
def get_someDate_fromString(self, key="year") -> Any:
arrow.get("Cool date: 2019-10-31T09:12:45.123456+04:30.", "YYYY-MM-DDTHH:mm:ss.SZZ")
arrow.get("Tomorrow (2019-10-31) is Halloween!", "YYYY-MM-DD")
arrow.get("Halloween is on 2019.10.31.", "YYYY.MM.DD")
arrow.get("It's Halloween tomorrow (2019-10-31)!", "YYYY-MM-DD")
arrow.get("2022-05-01").int_timestamp
arrow.get("2022-05-01").float_timestamp
arrow.get("2022-05-01").timestamp()
if __name__ == '__main__':
test = Handle_arrow_time()
print(test.get_someDate_byDescribe("today"))
print(test.get_someDate_byTimeKey())
print(test.get_nowDate_someAttribute())
print(test.get_someDate_fromString())
print(vars(Handle_arrow_time).items())
