help(time)之后可以知道time有2种时间表示形式:
1、时间戳表示法:即以整型或浮点型表示的是一个以秒为单位的时间间隔。这个时间的基础值是从1970年的1月1号零点开始算起。
2、元组格式表示法:即一种Python的数据结构表示。这个元组有9个整型内容(不能少),分别表示不同的时间含义。
常用功能:

名词解释:
UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。
DST(Daylight Saving Time)即夏令时。是一种为节约能源而人为规定地方时间的制度,一般在天亮早的夏季人为将时间提前一小时。
包含的变量:
timezone --当地时间与标准UTC时间的误差,以秒计
altzone --当地夏令时时间与标准UTC时间的误差,以秒计
daylight --当地时间是否反映夏令时,默认为0
zname --关于(标准时区名称,夏令时时区名称)的元组
1、time.localtime([secs]):将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。

2、time.gmtime([secs]):和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。

3、time.time():返回当前时间的时间戳

4、time.mktime(t):将一个struct_time转化为时间戳

5、time.sleep(secs):线程推迟指定的时间运行。单位为秒
6、time.clock():这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。(实际上是以WIN32上QueryPerformanceCounter()为基础,它比毫秒表示更为精确)


7、time.asctime([t]):把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。如果没有参数,将会将time.localtime()作为参数传入

8、time.ctime([secs]):把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()即当前时间戳为参数。它的作用相当于time.asctime(time.localtime(secs))。

9、time.strftime(format[,t]):把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。

10、time.strptime(string[,format]):把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

总结:根据之前描述,在Python中共有三种表达方式:1)timestamp 2)tuple或者struct_time 3)格式化字符串。它们之间的转化如图所示:



格式化字符串常用格式:
备注:
“%p”只有与“%I”配合使用才有效果。
文档中强调确实是0 - 61,而不是59,闰年秒占两秒(汗一个)。
当使用strptime()函数时,只有当在这年中的周数和天数被确定的时候%U和%W才会被计算。
网友评论