import timeimport datetimedef zero_timestamp(): """获取本地时间当天零点时间戳 Returns: int """ return int(time.mktime( datetime.date.today().timetuple() ))
测试assert zero_timestamp() == 1645027200 # True 2022/02/17
解析通过 datetime.date.today() 获取当天的日期 输出格式 %Y-%m-%d
timetuple() 返回时间结构元组 当天的时分秒都为零了
time.struct_time(tm_year=2022, tm_mon=2, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=48, tm_isdst=-1)
time.maketime() 将时间元组转换为时间戳
def mktime(t: _TimeTuple | struct_time) -> float: ...