time module
python representing time three ways: time stamp tuples (struct_time), time format string
Conversion between three formats:
1. timestamp
Is from at 0:00:00 on January 1, 1970 start offset calculated in seconds, the time stamp is given to the computer to identify the
import time t = time.time() print(t,type(t)) #1566992452.458001 <class 'float'>
Timestamp -> Structured time:
Import Time T = the time.time () Print (T) # 1,566,994,939.2002344 Print (time.gmtime (T)) # Returns GMT time.struct_time (tm_year = 2019, tm_mon = 8, tm_mday = 28, tm_hour = 12, = 22 is tm_min, tm_sec =. 19, tm_wday = 2, tm_yday = 240, the tm_isdst = 0) Print (time.localtime (T)) # returns Local time.struct_time (tm_year = 2019, tm_mon = 8, tm_mday = 28, tm_hour = 20, tm_min = 22, tm_sec = 19, tm_wday = 2, tm_yday = 240, tm_isdst = 0)
ctime (): timestamp -> format string time
Import Time # time.ctime (timestamp) is not returned if the parameter string directly returns the current time T = the time.time () Print (time.ctime (T)) # Wed 28-Aug 2019 21:18:09 Print (time.ctime ()) # Wed Aug 28 21:18:09 2019
2. Time String Format
After the output format can be converted adults are better able to read format
% y represents a two-digit year (00-99 ) % the Y represents a four-digit year (000-9999 ) % m (01-12 ) within% d month of day (0-31 of ) % H 24 hours number (manufactured by 0-23 hours ) % h 12 is the number of the I (manufactured by 01-12 hours ) % M number of minutes (00 = 59 ) % S seconds (00-59 ) % A simplified week local name % A full week of local name % b local simplified month name % B full month name local % C indicates the corresponding local date and time indicates one day (001-366% j years ) % P AM or PM local character equivalent % of U week of the year number (00-53 ) on Sunday as the start of the week % w week (0-6 ), Sunday to begin the week of % W week number of the year (00-53 ) Monday to start the week of % the X-local corresponding date It represents % X-indicates the corresponding local time % Name Z current time zone number %%% itself
usage:
import time t=time.strftime('%Y-%m-%d-%X') print(t,type(t)) #2019-08-28-19:59:44 <class 'str'> t=time.strftime('%Y/%m/%d %H/%M/%S') print(t,type(t)) #2019/08/28 20/04/33 <class 'str'>
Format String Time -> Time Structured
import time t = time.strptime('2019-7-5','%Y-%m-%d') print(t) #time.struct_time(tm_year=2019, tm_mon=7, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=186, tm_isdst=-1) t = time.strptime('2019-7-5 15:20:30','%Y-%m-%d %X') print(t) #time.struct_time(tm_year=2019, tm_mon=7, tm_mday=5, tm_hour=15, tm_min=20, tm_sec=30, tm_wday=4, tm_yday=186, tm_isdst=-1)
3. Ganso (struct_time)
Tuples are used to manipulate time
import time print(time.struct_time(time.localtime())) #time.struct_time(tm_year=2019, tm_mon=8, tm_mday=28, tm_hour=20, tm_min=11, tm_sec=15, tm_wday=2, tm_yday=240, tm_isdst=0)
Structured Time -> stamp
import time #time.mktime(结构化时间) t = time.time() time_tuple = time.localtime(t) print(time.mktime(time_tuple)) #1566996176.0
Structured Time -> format string time
import time #time.strftime('格式化时间','结构化时间') 结构化时间若不传则显示当前时间 t = time.time() print(time.strftime('%Y-%m-%d %X')) #2019-08-28 20:49:54 print(time.strftime('%Y-%m-%d %X',time.localtime(t))) #2019-08-28 20:49:54
time.asctime():结构化时间->格式化字符串时间
import time #time.asctime(结构化时间) 如果不传参数,直接返回当前时间的字符串 t = time.time() print(time.asctime(time.localtime(t))) #Wed Aug 28 21:11:09 2019 print(time.asctime()) #Wed Aug 28 21:11:09 2019
4.计算时间差
import time tuple_time1 = time.mktime(time.strptime('2018-7-30 15:20:30','%Y-%m-%d %H:%M:%S')) print(tuple_time1) #1532935230.0 tuple_time2 = time.time() time_dif = tuple_time2 - tuple_time1 print(time_dif) #34064346.31246209 struct_t = time.localtime(time_dif) print(struct_t) #time.struct_time(tm_year=1971, tm_mon=1, tm_mday=30, tm_hour=14, tm_min=19, tm_sec=6, tm_wday=5, tm_yday=30, tm_isdst=0) print('相差了{year}年,{month}月,{day}日,{hour}小时,{minute}分钟,{second}秒'.format(year=struct_t.tm_year-1970,month=struct_t.tm_mon-1,day=struct_t.tm_mday-1,hour=struct_t.tm_hour,minute=struct_t.tm_min,second=struct_t.tm_sec)) #相差了1年,0月,29日,14小时,19分钟,6秒
5.方法总结
1.time.allzone()
返回格林威治西部的夏令时地区的偏移秒数,如果该地区在格林威治东部会返回负值(如西欧,包括英国)
import time print(time.altzone) #-32400
2.time.asctime([tupletime])
接收一个时间元祖并返回一个可读形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串
import time print(time.asctime(time.localtime(time.time()))) #Wed Aug 28 22:13:30 2019 print(time.asctime(time.gmtime(time.time()))) #Wed Aug 28 14:13:30 2019
3.time.clock()
以浮点数计算的秒数返回当前的CPU时间
4.time.ctime([secs])
作用相当于asctime(localtime(secs)),未给参数相当于asctime()
import time print(time.ctime(time.time())) #Wed Aug 28 22:21:34 2019 print(time.ctime(777777777)) #Thu Aug 25 09:22:57 1994
5.time.gmtime([[secs]])
将一个时间戳转换为UTC时区(0时区)的struct_time,可选的参数sec表示从1970-1-1以来的秒数.其默认值为time.time(),函数返回time.struct_time类型的对象.(struct_time是在time模块中定义的表示时间的对象)
import time print(time.gmtime()) #time.struct_time(tm_year=2019, tm_mon=8, tm_mday=28, tm_hour=14, tm_min=27, tm_sec=25, tm_wday=2, tm_yday=240, tm_isdst=0) print(time.gmtime(777777777)) #time.struct_time(tm_year=1994, tm_mon=8, tm_mday=25, tm_hour=1, tm_min=22, tm_sec=57, tm_wday=3, tm_yday=237, tm_isdst=0)
6.time.localtime([secs])
接收时间戳(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时)
int tm_sec; /* 秒 – 取值区间为[0,59] */ int tm_min; /* 分 - 取值区间为[0,59] */ int tm_hour; /* 时 - 取值区间为[0,23] */ int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */ int tm_year; /* 年份,其值等于实际年份减去1900 */ int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。
import time print(time.localtime()) #time.struct_time(tm_year=2019, tm_mon=8, tm_mday=28, tm_hour=22, tm_min=33, tm_sec=31, tm_wday=2, tm_yday=240, tm_isdst=0)
7.time.mktime([tupletime])
接受时间元组并返回时间戳(1970纪元后经过的浮点秒数)
参数为结构化的时间或者完整的9位元组元素
import time t_tu = (1997,7,15,21,15,47,3,7,0) print(time.mktime(time.localtime(time.time()))) #1567003342.0 print(time.mktime(t_tu)) #868972547.0 print(time.asctime(t_tu)) #Thu Jul 15 21:15:47 1997
8.time.sleep([secs])
推迟调用线程的运行,secs指秒数
9.time.strftime(fmt[,tupletime])
结构化时间->格式化字符串时间
import time t = (2009, 2, 17, 17, 3, 38, 1, 48, 0) t = time.mktime(t) print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t))) #Feb 17 2009 09:03:38
10.time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')
格式化字符串时间->结构化时间
import time struct_time = time.strptime("30 Nov 00", "%d %b %y") print(struct_time) #time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
11.time.time()
返回当前时间的时间戳(1970纪元后经过的浮点秒数)
12.time.tzset()
根据环境变量TZ重新初始化时间相关设置
random模块