built-in moduletime
1. time() time_ns()
从1970-1-1 0:00到现在的秒数、纳秒数。返回值类型分别为float和int,两者的值相差10^9倍。
time(...) time() -> floating point number Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them. time_ns(...) time_ns() -> int Return the current time in nanoseconds since the Epoch.
>>> import time >>> time.time <built-in function time> >>> time.time() 1620908340.6910295 >>> time.time_ns() 1620908347245421300 >>> type(time.time()) <class 'float'> >>> type(time.time_ns()) <class 'int'> >>> time.time()==time.time_ns()/10**9 True >>>
2. time.struct_time
时间的结构,本质是一个元组tuple,由9个整数组成的序列,分别是:年月日、时分秒、星期、该天在一年中的序号、夏令时标识;函数gmtime()、localtime()和strptime() 的返回值就是时间元组,函数asctime()、mktime()和strftime()的参数也是时间元组。
class struct_time(builtins.tuple) struct_time(iterable=(), /) The time value as returned by gmtime(), localtime(), and strptime(), and accepted by asctime(), mktime() and strftime(). May be considered as a sequence of 9 integers. Note that several fields' values are not the same as those defined by the C language standard for struct tm. For example, the value of the field tm_year is the actual year, not year - 1900. See individual fields' descriptions for details.
>>> import time >>> time.struct_time((0,0,0,0,0,0,0,0,0)) time.struct_time(tm_year=0, tm_mon=0, tm_mday=0, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=0, tm_isdst=0) >>> >>> type(time.struct_time((1,1,1,1,1,1,1,1,1))) <class 'time.struct_time'> >>> ''' struct_time: int tm_year; /* 年份,实际年份[1,9999] */ int tm_mon; /* 月份 [0,11] 0代表一月 */ int tm_mday; /* 日期[1,31] */ int tm_hour; /* 时 [0,23] */ int tm_min; /* 分 [0,59] */ int tm_sec; /* 秒 [0,59] */ int tm_wday; /* 星期 [0,6] 0代表星期一 */ int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的时候,tm_isdst为0;不了解情况时,tm_isdst()为负。 '''
3. gmtime() localtime()
gmtime([seconds]) 将秒数转换为表示UTC(格林威治标准)的时间结构。
gmtime(...) gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst) Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a. GMT). When 'seconds' is not passed in, convert the current time instead. If the platform supports the tm_gmtoff and tm_zone, they are available as attributes only.
localtime([seconds]) 将秒数转换为表示本地(北京时间)的时间结构。
localtime(...) localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min, tm_sec,tm_wday,tm_yday,tm_isdst) Convert seconds since the Epoch to a time tuple expressing local time. When 'seconds' is not passed in, convert the current time instead.
注:北京时间是东八时区UTC+8,两者相差8小时;默认是转换time()返回的秒数。
>>> import time >>> a,b=time.gmtime(),time.localtime() >>> [t1==t2 for i,t1 in enumerate(a) for j,t2 in enumerate(b) if i==j] [True, True, True, False, True, True, True, True, True] >>> [t2-t1 for i,t1 in enumerate(a) for j,t2 in enumerate(b) if i==j] [0, 0, 0, 8, 0, 0, 0, 0, 0] >>> time.gmtime()==time.gmtime(time.time()) True >>> time.localtime()==time.localtime(time.time()) True >>>
4. mktime()
把时间元组或时间结构转成秒数。功能正好与gmtime() localtime()相反。
mktime(...) mktime(tuple) -> floating point number Convert a time tuple in local time to seconds since the Epoch. Tuple or struct_time argument required. Note that mktime(gmtime(0)) will not generally return zero for most time zones; instead the returned value will either be equal to that of the timezone or altzone attributes on the time module.
5. strftime() strptime()
strftime(tuple) 时间元组转字符串。
strftime(...) strftime(format[, tuple]) -> string Convert a time tuple to a string according to a format specification. See the library reference manual for formatting codes. When the time tuple is not present, current time as returned by localtime() is used.
strptime(string, format) 时间格式串转换成时间结构元组。
strptime(...) strptime(string, format) -> struct_time Parse a string to a time tuple according to a format specification. See the library reference manual for formatting codes (same as strftime()).
时间串格式:
%Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM.