第三方库 -- time 库
There are two standard representations of time.
One is the number of seconds since the Epoch, in UTC (a.k.a. GMT). It may be an integer or a floating point number (to represent fractions of seconds).
The actual value can be retrieved by calling gmtime(0).
The other representation is a tuple of 9 integers giving local time.
The tuple items are:
year (including century, e.g. 1998)
month (1-12)
day (1-31)
hours (0-23)
minutes (0-59)
seconds (0-59)
weekday (0-6, Monday is 0)
Julian day (day in the year, 1-366)
DST (Daylight Savings Time) flag (-1, 0 or 1)
If the DST flag is 0, the time is given in the regular time zone;
if it is 1, the time is given in the DST time zone;
if it is -1, mktime() should guess based on the date and time.
FUNCTIONS
asctime(...)
asctime([tuple]) -> string
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used.
>>> import time
>>> t=time.localtime()
>>> time.asctime(t)
'Sat Apr 18 19:18:38 2020'
>>>
ctime(...)
ctime(seconds) -> string
Convert a time in seconds since the Epoch (The Epoch is system-defined; on Unix, it is generally January 1st, 1970)to a string in local time.
This is equivalent to asctime(localtime(seconds)). When the time tuple is
not present, current time as returned by localtime() is used.
get_clock_info(...)
get_clock_info(name: str) -> dict
Get information of the specified clock.
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(...)
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.
mktime(...)
mktime(tuple) -> floating point number
Convert a time tuple in local time to seconds since the Epoch.
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.
monotonic(...)
monotonic() -> float
Monotonic clock, cannot go backward.
monotonic_ns(...)
monotonic_ns() -> int
Monotonic clock, cannot go backward, as nanoseconds.
perf_counter(...)
perf_counter() -> float
Performance counter for benchmarking.
perf_counter_ns(...)
perf_counter_ns() -> int
Performance counter for benchmarking as nanoseconds.
process_time(...)
process_time() -> float
Process time for profiling: sum of the kernel and user-space CPU time.
process_time_ns(...)
process_time() -> int
Process time for profiling as nanoseconds:
sum of the kernel and user-space CPU time.
sleep(...)
sleep(seconds)
Delay execution for a given number of seconds. The argument may be
a floating point number for subsecond precision.
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.
Commonly used format codes:
%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.
Other codes may be available on your platform. See documentation for
the C library strftime function.
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()).
Commonly used format codes:
%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.
Other codes may be available on your platform. See documentation for
the C library strftime function.
thread_time(...)
thread_time() -> float
Thread time for profiling: sum of the kernel and user-space CPU time.
thread_time_ns(...)
thread_time() -> int
Thread time for profiling as nanoseconds:
sum of the kernel and user-space CPU time.
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.
DATA
altzone = -32400
daylight = 0
timezone = -28800
tzname = ('中国标准时间', '中国夏令时')
time库 import.time 时间获取: time.time() 获取当前时间戳,即计算及内部的时间值,浮点数 time.ctime() 获取当前时间并以易读的方式表示时间,返回字符串 time.gmtime() 获取当前时间,表示为计算机可处理的时间格式 时间格式化: time.strftime(tpl,ts) tpl 是格式化模板字符串 ts 用来定义输出效果 >>>t=time.gmtime() >>>time.strftime("%Y-%m-%d %H:%M:%S",t) # str可以理解为字符串 f '2018-01-26 12:55:20' time.strptime() >>>timestr='2018-01-26 12:55:20' >>>time.strptime(timestr,"%Y-%m-%d %H:%M:%S") 输出t=time.gmtime()的结果 程序计时: time.perf_counter() 返回一个CPU级别的精确时间计算值,单位为秒,由于这个计数起点不确定,连续调用取差值才有意义 time.sleep(s) s是休眠时间,单位是秒,可以是浮点数