收废品做网站,设计app需要的技术,wordpress添加标签,WordPress添加加载用时Python学习之——时间模块 参考time 模块常见接口 datetime 模块常见接口 calendar 模块常见接口 示例 参考
Python datetime模块详解、示例 搞定Python时区的N种姿势 calendar – 日历相关
time 模块
在Python中#xff0c;通常有这几种方式来表示时间#xff1a; 1… Python学习之——时间模块 参考time 模块常见接口 datetime 模块常见接口 calendar 模块常见接口 示例 参考
Python datetime模块详解、示例 搞定Python时区的N种姿势 calendar – 日历相关
time 模块
在Python中通常有这几种方式来表示时间 1时间戳 2格式化的时间字符串 3元组struct_time共九个元素。
UTCCoordinated Universal Time世界协调时即格林威治天文时间世界标准时间在中国为UTC8。 DSTDaylight Saving Time即夏令时。 时间戳timestamp时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。 元组struct_timestruct_time元组共有9个元素返回struct_time的函数主要有gmtime()localtime()strptime()。 下面列出这种方式元组中的几个元素
索引Index属性Attribute值Values0tm_year年比如20111tm_mon月1 - 122tm_mday日1 - 313tm_hour时0 - 234tm_min分0 - 595tm_sec秒0 - 616tm_wdayweekday0 - 60表示周日7tm_yday一年中的第几天1 - 3668tm_isdst是否是夏令时默认为-1
常见接口
import time# 返回当前UTC时间的时间戳(从1970年1月1日00:00:00开始按秒计算的偏移量)
time.time()# 和localtime()类似gmtime()是将一个时间戳转换为UTC-0时区的struct_time。
time.gmtime([secs])# 将一个时间戳转换为当前本地时区的struct_time。secs参数未提供返回当前本地时区的当前时间struct_time
time.localtime([secs])# localtime()的反函数,将一个struct_time转化为时间戳。
# 参数是 struct_time 或者完整的9元组 注意参数是当前本地时区的struct_time而不是UTC-0时区
time.mktime(t)# 把一个表示时间的struct_time或者完整的9元组转化为格式化的时间字符串。
# 如果t未指定将传入time.localtime()
time.strftime(format[, t])# strftime()的逆操作, 把一个格式化时间字符串转化为struct_time
time.strptime(string[, format])# 把一个表示时间的struct_time或者完整的9元组表示为Sat Dec 9 23:16:45 2023。这种形式
# 如果没有参数将会将time.localtime()作为参数传入
time.asctime([t])# 把一个时间戳转化为time.asctime()的形式。
# 如果参数未给或者为None的时候将会默认time.time()为参数
time.ctime([secs])# 线程推迟指定的时间运行单位为秒。
time.sleep(secs)# 在UNIX系统上clock()返回的是秒表示的时间戳
# 在WINDOWS中第一次调用返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。
time.clock()
示例
import time
print(ftime.time():{time.time()})
print(ftime.gmtime():{time.gmtime()})
print(ftime.localtime():{time.localtime()})
print(ftime.strftime(%Y/%m/%d/ %H:%M:%S, time.localtime()):{time.strftime(%Y/%m/%d/ %H:%M:%S, time.localtime())})
print(ftime.asctime:{time.asctime()})# 结果
time.time():1702135670.389908
time.gmtime():time.struct_time(tm_year2023, tm_mon12, tm_mday9, tm_hour15, tm_min27, tm_sec50, tm_wday5, tm_yday343, tm_isdst0)
time.localtime():time.struct_time(tm_year2023, tm_mon12, tm_mday9, tm_hour23, tm_min27, tm_sec50, tm_wday5, tm_yday343, tm_isdst0)
time.strftime(%Y/%m/%d/ %H:%M:%S, time.localtime()):2023/12/09/ 23:27:50
time.asctime:Sat Dec 9 23:27:50 2023datetime 模块
python datetime time 时间模块 搞定Python时区的N种姿势
datetime模块中包含如下类
类名功能说明date日期对象,常用的属性有year, month, daytime时间对象datetime日期时间对象,常用的属性有hour, minute, second, microseconddatetime_CAPI日期时间对象C语言接口timedelta时间间隔即两个时间点之间的长度tzinfo时区信息对象
datetime模块中包含的常量
datetime.MAXYEAR # 返回能表示的最大年份9999
datetime.MINYEAR # 返回能表示的最小年份1常见接口
from datetime import datetime, timedelta, tzinfo# 获取当前utc-0时区的时间
datetime.utcnow()
# 由时间戳返回本地时间datetime
datetime.fromtimestamp
# 由时间戳返回utc时间datetime
datetime.utcfromtimestampcalendar 模块
calendar – 日历相关 Python-标准库calendar的使用 提供日历功能
常见接口
import calendarif __name__ __main__:cc calendar.Calendar(0)one_weekday []for index, dt in enumerate(cc.itermonthdates(2023, 12)):if index ! 0 and index % 7 0:print(.join(one_weekday))one_weekday.clear()one_weekday.append(f{dt} )# 结果
2023-11-27 2023-11-28 2023-11-29 2023-11-30 2023-12-01 2023-12-02 2023-12-03
2023-12-04 2023-12-05 2023-12-06 2023-12-07 2023-12-08 2023-12-09 2023-12-10
2023-12-11 2023-12-12 2023-12-13 2023-12-14 2023-12-15 2023-12-16 2023-12-17
2023-12-18 2023-12-19 2023-12-20 2023-12-21 2023-12-22 2023-12-23 2023-12-24示例
一些常见的时间转换函数封装
# -*- coding: utf-8 -*-
import time
from datetime import datetime, timezone, timedeltaTIME_FORMAT_STR %Y/%m/%d/ %H:%M:%S
DAY_SECOND 86400
HOUR_SECOND 3600
MINUTE_SECOND 60def datetime_to_str(datetime_obj: datetime, format_strTIME_FORMAT_STR) - str:return datetime_obj.strftime(format_str)def str_to_datetime(time_str: str, format_strTIME_FORMAT_STR) - datetime:return datetime.strptime(time_str, format_str)def datetime_to_timestamp(datetime_obj: datetime) - float:return time.mktime(datetime_obj.timetuple())def timestamp_to_str(timestamp: float, format_strTIME_FORMAT_STR) - str:return time.strftime(format_str, time.localtime(timestamp))def str_to_timestamp(time_str: str, format_strTIME_FORMAT_STR) - float:time.gmtime([secs]): 将一个时间戳转换为UTC-0时区的struct_time。time.localtime([secs]): 将一个时间戳转换为当前时区的struct_time。secs参数未提供则以当前时间为准time.mktime: localtime()的反函数,将一个struct_time转化为时间戳,注意参数是使用本地时区的_TimeTuple | struct_time而不是UTC-0时区datetime_obj: datetime str_to_datetime(time_str, format_str)return time.mktime(datetime_obj.timetuple())def get_local_date_str(timestamp: float) - str:将一个时间戳转换为当前本地时区的日期字符串# 将一个时间戳转换为当前时区的struct_timetime_struct: time.struct_time time.localtime(int(timestamp))ret: str time.strftime(TIME_FORMAT_STR, time_struct)return retdef get_local_date_str_custom1(timestamp: float, HMS_FMT%H:%M:%S) - str:time_struct: time.struct_time time.localtime(int(timestamp))input_day int(timestamp) / DAY_SECONDcur_day int(time.time()) / DAY_SECONDif input_day cur_day:return time.strftime(HMS_FMT, time_struct)else:return time.strftime(TIME_FORMAT_STR, time_struct)def get_now_time_str(is_local: bool True, timezone_num: int 0) - str:if is_local:# 当前本地时区datetime_obj: datetime datetime.now()else:# utc-0 时区# utc_0_datetime: datetime datetime.utcnow()# timezone_num0: utc-0 时区; timezone_num2: utc-2 时区datetime_obj: datetime datetime.now(timezone(timedelta(hourstimezone_num)))return datetime_to_str(datetime_obj)def timestamp_to_utc_str(timestamp: float, format_strTIME_FORMAT_STR) - str:时间戳转utc-0时区的时间datetime_obj: datetime datetime.utcfromtimestamp(timestamp)return datetime_obj.strftime(format_str)def timestamp_to_local_str(timestamp: float, format_strTIME_FORMAT_STR) - str:时间戳转当前本地时区的时间datetime_obj: datetime datetime.fromtimestamp(timestamp)return datetime_obj.strftime(format_str)def get_local_offst() - timedelta:获取当前时区相对UTC-0的偏移timestamp: float time.time()local_datetime_obj: datetime datetime.fromtimestamp(timestamp)utc0_datetime_obj: datetime datetime.utcfromtimestamp(timestamp)offset: timedelta local_datetime_obj - utc0_datetime_objreturn offsetdef seconds_to_left_str(input_seconds: float) - str:秒数seconds转剩余倒计时:XX时XX分XX秒https://www.runoob.com/python/att-string-format.htmlhours 0minutes 0seconds 0input_seconds int(input_seconds)if input_seconds 0:hours int(input_seconds / HOUR_SECOND)minutes int(input_seconds % HOUR_SECOND / MINUTE_SECOND)seconds int(input_seconds % MINUTE_SECOND)# 例如(100, 1, 10) 100时01分10秒; (0, 0, 0) 0时00分00秒return {:0d}时{:02d}分{:02d}秒.format(hours, minutes, seconds)if __name__ __main__:print(futc-0 time_str: {timestamp_to_utc_str(time.time())})print(futc-local time_str: {timestamp_to_local_str(time.time())})print(futc-0 time_str: {get_now_time_str(False, 0)})print(futc-local time_str: {get_now_time_str()})print(futc-local time_str:{get_local_date_str(time.time())})print(futc-local offset:{get_local_offst()})input_seconds 6000print(f{input_seconds} seconds{seconds_to_left_str(input_seconds)})