time 模块
相关方法
asctime(...)
asctime([tuple])->字符串
将时间元组转换为字符串,例如 'Sat Jun 06 16:26:11 1998'。 '%a %b %d %H:%M:%S %Y'
如果不存在时间元组,则使用localtime()返回的当前时间。
clock(...)
clock()->浮点数
返回自进程开始或自第一次调用clock()以来的CPU时间或实时时间。
这与系统记录一样精确。
ctime(...)
ctime(seconds)->字符串
将自纪元以来的时间(以秒为单位)转换为本地时间的字符串。
这等效于asctime(localtime(seconds))。
如果不存在时间元组,则使用localtime()返回的当前时间。
gmtime(...)
gmtime([seconds])->(tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
将自大纪元以来的秒数转换为表示UTC的时间元组(又称GMT)。
如果未传递“秒”,请改为转换当前时间。
localtime(...)
localtime([seconds])->(tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst)
将自大纪元以来的秒数转换为表示本地时间的时间元组。
如果未传递“秒”,请改为转换当前时间。
mktime(...)
mktime(tuple)->浮点数 如:time.mktime((2020, 9, 5, 8, 0, 55, 5, 249, 0))
将本地时间的时间元组转换为自大纪元以来的秒数。
sleep(...)
sleep(seconds)
将执行延迟指定的秒数。
该参数可以是浮点数,用于亚秒级精度。
strftime(...)
strftime(format [,tuple])-> string 如:time.strftime("%Y-%m-%d %H:%M:%S")
根据格式说明将时间元组转换为字符串。
有关格式代码,请参见库参考手册。
如果不存在时间元组,则使用localtime()返回的当前时间。
strptime(...)
strptime(string,format)-> struct_time 如:time.strptime('Sat Jun 06 16:26:11 1998')
根据格式说明将字符串解析为时间元组。
请参阅库参考手册以获取格式代码(与strftime()相同)。
time(...)
time()->浮点数
返回自纪元以来的当前时间(以秒为单位)。
如果系统时钟提供了小数秒,则可能会出现。
DATA
accept2dyear = 1
altzone = -32400
daylight = 0
timezone = -28800
tzname = ('\xd6\xd0\xb9\xfa\xb1\xea\xd7\xbc\xca\xb1\xbc\xe4', '\xd6\xd...
名称解释
# 字符串string
Sat Jun 06 16:26:11 1998
# 浮点数floating point number
1463846400.0
# 时间元组time tuple & struct_time : <type 'time.struct_time'>
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=1, tm_hour=1, tm_min=28, tm_sec=59, tm_wday=3, tm_yday=32, tm_isdst=0)
时间元组(年、月、日、时、分、秒、一周的第几日、一年的第几日、夏令时)
一周的第几日: 0-6
一年的第几日: 1-366
夏令时: -1, 0, 1
时间日期格式化符号
%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)
%f 毫秒(0-1000)
%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 当前时区的名称 # 乱码
%% %号本身
time 模块
Help on built-in module time:
NAME
time - This module provides various functions to manipulate time values.
FILE
(built-in)
DESCRIPTION
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 Epoch is system-defined; on Unix, it is generally January 1st, 1970.
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 (four digits, 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.
Variables:
timezone -- difference in seconds between UTC and local standard time
altzone -- difference in seconds between UTC and local DST time
daylight -- whether local time should reflect DST
tzname -- tuple of (standard time zone name, DST time zone name)
Functions:
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
CLASSES
__builtin__.object
struct_time
class struct_time(__builtin__.object)
| 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.
|
| Methods defined here:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __contains__(...)
| x.__contains__(y) <==> y in x
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __getslice__(...)
| x.__getslice__(i, j) <==> x[i:j]
|
| Use of negative indices is not supported.
|
| __gt__(...)
| x.__gt__(y) <==> x>y
|
| __hash__(...)
| x.__hash__() <==> hash(x)
|
| __le__(...)
| x.__le__(y) <==> x<=y
|
| __len__(...)
| x.__len__() <==> len(x)
|
| __lt__(...)
| x.__lt__(y) <==> x<y
|
| __mul__(...)
| x.__mul__(n) <==> x*n
|
| __ne__(...)
| x.__ne__(y) <==> x!=y
|
| __reduce__(...)
|
| __repr__(...)
| x.__repr__() <==> repr(x)
|
| __rmul__(...)
| x.__rmul__(n) <==> n*x
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| tm_hour
| hours, range [0, 23]
|
| tm_isdst
| 1 if summer time is in effect, 0 if not, and -1 if unknown
|
| tm_mday
| day of month, range [1, 31]
|
| tm_min
| minutes, range [0, 59]
|
| tm_mon
| month of year, range [1, 12]
|
| tm_sec
| seconds, range [0, 61])
|
| tm_wday
| day of week, range [0, 6], Monday is 0
|
| tm_yday
| day of year, range [1, 366]
|
| tm_year
| year, for example, 1993
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __new__ = <built-in method __new__ of type object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
| n_fields = 9
|
| n_sequence_fields = 9
|
| n_unnamed_fields = 0
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.
clock(...)
clock() -> floating point number
Return the CPU time or real time since the start of the process or since the first call to clock().
This has as much precision as the system records.
ctime(...)
ctime(seconds) -> string
Convert a time in seconds since the Epoch 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.
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.
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.
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.
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()).
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.
DATA
accept2dyear = 1
altzone = -32400
daylight = 0
timezone = -28800
tzname = ('\xd6\xd0\xb9\xfa\xb1\xea\xd7\xbc\xca\xb1\xbc\xe4', '\xd6\xd...