import time
import calendar
from datetime import datetime, date, time, timedelta, tzinfo, timezone
“”"
print(time.gmtime(0)) # epoch 纪元 起始时间点
struct_time
print(time.gmtime()) # 返回struct_time元组 自纪元以来的秒数 到 UTC 的struct_time
print(time.localtime()) # 返回struct_time元组 自纪元以来的秒数 到 本地时间的struct_time
反过来
print(calendar.timegm(time.gmtime())) # UTC 的 struct_time 到 自纪元以来的秒数
print(time.mktime(time.localtime())) # 本地 的 struct_time 到 自纪元以来的秒数
strftime 返回的时间到由 format 参数指定的字符串
print(time.strftime(“%Y-%m-%d %H:%M:%S”, time.localtime())) # 2024-06-09 23:59:17
print(time.strftime(“%Y-%m-%d %H:%M:%S”, time.gmtime())) # 2024-06-09 15:59:17
strptime 根据格式解析表示时间的字符串。 返回值为一个被 gmtime() 或 localtime() 返回的 struct_time
print(time.strptime(“2024-06-09 23:59:17.123456”, “%Y-%m-%d %H:%M:%S.%f”))
time.struct_time(tm_year=2024, tm_mon=6, tm_mday=9, tm_hour=23, tm_min=59, tm_sec=17, tm_wday=6, tm_yday=161, tm_isdst=-1)
struct_time 由 time.gmtime() 或 time.localtime() 或者 time.strptime() 返回
tm_year =2024, tm_mon=6, tm_mday=9, tm_hour=23, tm_min=59, tm_sec=17, tm_wday=6, tm_yday=161, tm_isdst=-1
time.time() 返回以浮点数表示的从 epoch 开始的秒数形式的时间
print(time.time()) # 1686306851.123456 1717949082.9770646
print(time.localtime(1686306851.123456))
time.struct_time(tm_year=2023, tm_mon=6, tm_mday=9, tm_hour=18, tm_min=34, tm_sec=11, tm_wday=4, tm_yday=160, tm_isdst=0)
print(time.gmtime(1686306851.123456))
time.struct_time(tm_year=2023, tm_mon=6, tm_mday=9, tm_hour=10, tm_min=34, tm_sec=11, tm_wday=4, tm_yday=160, tm_isdst=0)
time.time_ns() 与 time() 相似,但返回时间为用整数表示的自 epoch 以来所经过的纳秒数。
print(f’{time.time_ns()}ns’) # 168630685112345600ns 1717949202375014600ns
“”"
------------------ 接下来是 datetime -----------------------
datetime 模块提供了用于操作日期和时间的类。
datetime.date -> year month day
print(date(2021, 1, 1)) # 2021-01-01
day = date(2022, 12, 2)
print(day) # 2022-12-02
print(day.year) # 2022
print(day.month) # 12
print(day.day) # 2
print(date.today()) # 2024-06-10 返回当前的本地日期。 等价于 date.fromtimestamp(time.time())
datetime.time -> hour minute second microsecond tzinfo fold 取值范围是 [0, 1]。 用于在重复的时间段中消除边界时间的歧义
print(time(12, 30, 45, 999999, None, fold=0)) # 12:30:45.999999
t = time(11, 45, 00, 123456)
print(t) # 11:45:00.123456
print(f’{t.hour=}‘) # t.hour = 11
print(f’{t.minute=}‘) # t.minute = 45
print(f’{t.second=}‘) # t.second = 0
print(f’{t.microsecond=}‘) # t.microsecond = 123456
print(f’{t.tzinfo=}‘) # t.tzinfo = None
print(f’{t.fold=}') # t.fold = 0
datetime.datetime
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
datetime.now()=datetime.datetime(2024, 6, 10, 0, 35, 27, 350516)
print(f’{datetime.today()=}') # 返回表示当前地方时的 datetime 对象
datetime.now()=datetime.datetime(2024, 6, 10, 0, 35, 27, 350516)
print(f’{datetime.now()=}') # 返回表示当前地方时的 date 和 time 对象
datetime.utcnow() = datetime.datetime(2024, 6, 9, 16, 35, 27, 350516)
print(f’{datetime.utcnow()=}') # 返回表示当前 UTC 时间的 date 和 time 对象
datetime.timedelta
class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
所有参数都是可选的并且默认为 0。 这些参数可以是整数或者浮点数,并可以为正值或者负值。
只有 days, seconds 和 microseconds 会存储在内部。 参数单位的换算规则如下
1毫秒会转换成1000微秒。
1分钟会转换成60秒。
1小时会转换成3600秒。
1星期会转换成7天。
delta = timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5)
delta=datetime.timedelta(days=1, seconds=7384, microseconds=5)
print(f’{delta=}')
需求 1小时 10分钟 10秒 后
delta = timedelta(hours=1, minutes=10, seconds=10)
datetime.now() + delta=datetime.datetime(2024, 6, 10, 1, 54, 27, 582716)
print(f’{datetime.now() + delta=}')
需求 1小时 10分钟 10秒 前
delta = timedelta(hours=1, minutes=10, seconds=10)
datetime.now() - delta=datetime.datetime(2024, 6, 9, 23, 34, 7, 582716)
print(f’{datetime.now() - delta=}')