time模块
Python中,通常有这几种方式来表示时间:
1、时间戳(timestamp)通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
2、格式化的时间字符串
3、元组(struct_time)struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)
time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
tzset() -- change the local timezone
""" time.strftime(format[, tuple]) # 返回日期字符串, tuple:struct_time对象, strftime:string format time 格式化的时间字符串 %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数(0-23) %I 12小时制小时数(01-12) %M 分钟数(00=59) %S 秒(00-59) %a 本地简化星期名称 %A 本地完整星期名称 %b 本地简化的月份名称 %B 本地完整的月份名称 %c 本地相应的日期表示和时间表示 %j 年内的一天(001-366) %p 本地A.M.或P.M.的等价符 %U 一年中的星期数(00-53)星期天为星期的开始 %w 星期(0-6),星期天为星期的开始 %W 一年中的星期数(00-53)星期一为星期的开始 %x 本地相应的日期表示 %X 本地相应的时间表示 %Z 当前时区的名称 %% %号本身 """
import time # time()返回当前时间的时间戳 t = time.time() # 1534383683.471927 print(t) # localtime(seconds=None)将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。 t = time.localtime() # time.struct_time(tm_year=2018, tm_mon=8, tm_mday=16, tm_hour=9, tm_min=44, tm_sec=28, tm_wday=3, tm_yday=228, tm_isdst=0) # t = time.localtime(1534383683.471927) print(t) # gmtime(seconds=None)将一个时间戳转换为UTC时区(0时区)的struct_time。 t = time.gmtime(1534383683.471927) print(t) # mktime(p_tuple)将一个struct_time转化为时间戳。 t = time.mktime(time.localtime()) # 1534384261.0 print(t) # asctime(p_tuple=None) 把一个表示时间的元组或者struct_time表示为这种形式:'Sat Jun 06 16:26:11 1998'。 # 如果没有参数,将会将time.localtime()作为参数传入 t = time.asctime(time.localtime()) # Thu Aug 16 09:53:09 2018 print(t) # ctime(seconds=None) 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为 # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。 t = time.ctime() # Thu Aug 16 09:54:40 2018 t = time.ctime(1534384261) # Thu Aug 16 09:51:01 2018 print(t) # strftime(format, p_tuple=None)把一个代表时间的元组或者struct_time(如由time.localtime()和 # time.gmtime()返回)转化为格式化的时间字符串。如果p_tuple未指定,将传入time.localtime()。如果元组中任何一个 # 元素越界,ValueError的错误将会被抛出 t = time.strftime("%Y-%m-%d %X", time.localtime()) # 2018-08-16 09:58:10 print(t) # strptime(string, format)把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。format默认为:"%a %b %d %H:%M:%S %Y"。 t = time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X') # time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1) print(t) # sleep(seconds) 线程推迟指定的时间运行,单位为秒 t = time.sleep(5)

日历
import calendar cal = calendar.month(2016, 1) print("以下输出2016年1月份的日历:") print(cal) # Mo Tu We Th Fr Sa Su # 1 2 3 # 4 5 6 7 8 9 10 # 11 12 13 14 15 16 17 # 18 19 20 21 22 23 24 # 25 26 27 28 29 30 31
浙公网安备 33010602011771号