python3时间模块【官方参数解释】:
python有两个标准的时间表现形式:
1.自从【the Epoch, in UTC (a.k.a. GMT)】【1970 1 1 08:00:00 至现在时刻】的时间秒数:整型或者浮点型
2.包含9个整型的元组去表现本地时间:
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.
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
time.struct_time时间元组参数【struct_time是一个类】:
(年[xx],月[1-12],日[1-31],时[0-23],分[0-59],秒[0-59],星期[0-6],天[1-366],是否夏令[1-夏令、0-非夏、-1[默认未知]])
时间format格式参数:
%y:两位年份【0-99】
%Y:四位年份【2019】
%m:月份【01-12】
%d:天【0-31】
%H:时【0-23】
%I:时【01-12】
%M:分【00-59】
%S:秒【00-59】
%a:星期简写【Mon--->Monday】
%A:星期全称【Monday】
%b:月份简称【Jun--->June】
%B:月份简称【June】
%c:本地适当的日期和时间代表
%p:本地等价与AM或者PM
%z:来自UTC的时间空间
函数分类:
1.所有秒数:
形如:1559034874.5289233
seconds = 【 time.time()、time.mktime(tuple)、calendar.timegm(tuple)】
2.返回时间【一个类】元组:
形如:time.struct_time(tm_year=2019, tm_mon=5, tm_mday=28, tm_hour=16, tm_min=59, tm_sec=55, tm_wday=1, tm_yday=148, tm_isdst=-1)
tuple = 【 time.localtime(seconds)、time.gmtime(seconds)、time.strptime(string,format)】
3.返回格式化字符串:
形如:Tue May 28 16:59:55 2019 、2019-05-28 17:14:34
time.ctime(seconds)、time.asctime(tuple)、time.strftime(format,tuple)
4.CPU时间:
形如:0.3223298026283661
flocat_seconds = time.clock()
5.其它时间:
形如:浮点型
time.sleep(flocat_seconds)
为什么返回的是一个带元组名的元组?为什么返回的时间元组名是struct_time这个类名,参数应该自动获取并返回的?。目前不懂
time()计算秒数:
def time(): # real signature unknown; restored from __doc__
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.
'''
return 0.0 #表示浮点数,不是0.0
import time
time_total_seconds = time.time() # 到现在时刻的所有秒数
print("time()返回结果类型:%s" % (type(time_total_seconds))) # 类型:浮点型
print("[1970 1 1 08:00:00]到现在时刻历经%s秒" % time_total_seconds)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
time()返回结果类型:
[1970 1 1 08:00:00]到现在时刻历经1562056577.7521956秒
Process finished with exit code 0
localtime(seconds)以秒数为参数【可忽略】格式化输出时间元组,localtime(time.time())
struct_time:【年[xx],月[1-12],日[1-31],时[0-23],分[0-59],秒[0-59],星期[0-6],天[第几天],是否夏令[1-夏令、0-非夏、-1[默认未知]]】
def localtime(seconds=None): # real signature unknown; restored from __doc__
'''
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
tm_sec,tm_wday,tm_yday,tm_isdst) #入参为秒数【time()】,返回格式化元组【类名(参数)】
Convert seconds since the Epoch to a time tuple expressing local time.
When 'seconds' is not passed in, convert the current time instead.
'''
pass
import time
time_tuple = time.localtime(time.time()) # time.localtime()
print("localtime(seconds)返回结果类型:%s" % type(time_tuple)) #
print(time_tuple)
for x in range(len(time_tuple)): # for x in time_tuple:print(x,end=" ")
print(time_tuple[x], end=" ")
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
localtime(seconds)返回结果类型:
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=2, tm_hour=16, tm_min=38, tm_sec=22, tm_wday=1, tm_yday=183, tm_isdst=0)
2019 7 2 16 38 22 1 183 0
Process finished with exit code 0
gmtime(seconds)以秒数为参数【可忽略】格式化输出时间元组,gmtime(time.time())
struct_time:【年[xx],月[1-12],日[1-31],时[0-23],分[0-59],秒[0-59],星期[0-6],天[第几天],是否夏令[1-夏令、0-非夏、-1[默认未知]]】
def gmtime(seconds=None): # real signature unknown; restored from __doc__
'''
gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,
tm_sec, tm_wday, tm_yday, tm_isdst)#入参为秒数【time()】,返回格式化元组【类名(参数)】
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.
'''
pass
import time
gmtime_tuple = time.gmtime(time.time()) # time.gmtime()
print("gmtime(seconds)返回类型为:%s" % type(gmtime_tuple))
print(gmtime_tuple)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
gmtime(seconds)返回类型为:
time.struct_time(tm_year=2019, tm_mon=7, tm_mday=2, tm_hour=8, tm_min=39, tm_sec=46, tm_wday=1, tm_yday=183, tm_isdst=0)
Process finished with exit code 0
strptime(string,format):以strftime()返回的字符串为参数按照format格式化为时间元组:time.struct_time【,,,】
def strptime(string, format): # real signature unknown; restored from __doc__
'''
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.
'''
return struct_time #返回时间元组
import time
strptime_tuple = time.strptime("Tue May 28 16:59:55 2019", "%a %b %d %H:%M:%S %Y")
print("strptime(string,format)返回结果类型为:%s" % type(strptime_tuple))
print(strptime_tuple)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
strptime(string,format)返回结果类型为:
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=28, tm_hour=16, tm_min=59, tm_sec=55, tm_wday=1, tm_yday=148, tm_isdst=-1)
Process finished with exit code 0
ctime(seconds)以秒数为参数【可忽略】格式化输出,【 ctime(time.time())】
def ctime(seconds=None): # known case of time.ctime
'''
ctime(seconds) -> string #入参为秒数【time()】,返回格式化字符串
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.
'''
return "" #表示字符串
import time
ctime_begin_time_str = time.ctime(0) # 开始计时时刻【Thu Jan 1 08:00:00 1970】
ctime_current_time_str = time.ctime(time.time()) # time.ctime()
print("ctime(seconds)返回结果类型:%s" % (type(ctime_current_time_str))) #
print("开始时间:%s" % ctime_begin_time_str)
print("当前时间:%s" % ctime_current_time_str)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
ctime(seconds)返回结果类型:
开始时间:Thu Jan 1 08:00:00 1970
当前时间:Tue Jul 2 16:45:02 2019
Process finished with exit code 0
asctime(tuple)以localtime函数的返回值【时间元组】为参数【可忽略】,格式如ctime(seconds)
def asctime(p_tuple=None): # real signature unknown; restored from __doc__
'''
asctime([tuple]) -> string #入参为localtime()的返回值,返回格式化字符串,形如ctime()
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.
'''
return "" #表示字符串
import time
asctime_str = time.asctime(
time.localtime(time.time())) # time.asctime()、time.asctime(time.gmtime())、time.asctime(time.strptime())
print("asctime(tuple)返回结果类型:%s" % type(asctime_str))
print(asctime_str)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
asctime(tuple)返回结果类型:
Tue Jul 2 16:45:59 2019
Process finished with exit code 0
strftime(format,tuple):以时间元组为参数按照format格式化字符串
def strftime(format, p_tuple=None): # real signature unknown; restored from __doc__
'''
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.
'''
return "" #字符串
import time
strftime_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 或传入其它获取时间元组的函数的返回值
strftime_str1 = time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
print("strftime(format,tuple)返回结果类型:%s" % type(strftime_str))
print(strftime_str)
print(strftime_str1)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
strftime(format,tuple)返回结果类型:
2019-07-02 16:47:07
Tue Jul 02 16:47:07 2019
Process finished with exit code 0
mktime(tuple):入参为时间元组,返回值为浮点型的时间戳
def mktime(p_tuple): # real signature unknown; restored from __doc__
'''
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.
'''
return 0.0 #浮点型
import time
mktime_float_seconds = time.mktime(time.localtime(time.time())) # 或传入其它获取时间元组的函数的返回值
print("mktime(tuple)返回结果类型为:%s" % type(mktime_float_seconds))
print(mktime_float_seconds)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
mktime(tuple)返回结果类型为:
1562057294.0
Process finished with exit code 0
timegm(tuple):入参为时间元组,返回值为整型的时间戳【来自calendar模块】
def timegm(tuple):
'''Unrelated but handy function to calculate Unix timestamp from GMT.'''
year, month, day, hour, minute, second = tuple[:6]
days = datetime.date(year, month, 1).toordinal() - _EPOCH_ORD + day - 1
hours = days*24 + hour
minutes = hours*60 + minute
seconds = minutes*60 + second
return seconds
import time
import calendar
timegm_int_seconds = calendar.timegm(time.localtime()) # 或传入其它获取时间元组的函数的返回值
print("timegm(tuple)返回类型为:%s" % type(timegm_int_seconds))
print(timegm_int_seconds)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
timegm(tuple)返回类型为:
1562086181
Process finished with exit code 0
sleep(seconds):俗语-程序等待时间?参数seconds为浮点数
def sleep(seconds): # real signature unknown; restored from __doc__
'''
sleep(seconds) #参数seconds为浮点数
Delay execution for a given number of seconds. The argument may be
a floating point number for subsecond precision.
'''
pass
import time
sleep_before_seconds = time.time()
time.sleep(5.123)
sleep_after_seconds = time.time()
print("sleep(seconds)返回结果类型为:")
print(sleep_before_seconds)
print(sleep_after_seconds)
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
sleep(seconds)返回结果类型为:
1562057459.3410735
1562057464.464871
Process finished with exit code 0
clock():无入参返回浮点数,返回【从程序开始到调用】或者【首次调用到再次调用】的真实时间或者CPU时间
Deprecated since version 3.3, will be removed in version 3.8: The behaviour of this function depends on the platform:
use perf_counter() or process_time() instead
def clock(): # real signature unknown; restored from __doc__
'''
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.
'''
return 0.0
import time
def myfunc():
a = 1
for i in range(1000000):
a += i
time_begin_seconds = time.time() # 程序此刻的系统时间
clock_begin_seconds = time.clock() # 程序此刻已使用的CPU时间
myfunc()
time_end_seconds = time.time() # 程序此刻的系统时间
clock_end_seconds = time.clock() # 程序此刻已使用的CPU时间
print("clock()返回结果类型为:%s" % type(clock_begin_seconds))
print("CPU运行时间用clock计算为:%s" % (clock_end_seconds - clock_begin_seconds))
print("系统运行时间用time计算为:%s" % (time_end_seconds - time_begin_seconds))
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
clock()返回结果类型为:
CPU运行时间用clock计算为:0.06896580085867973
系统运行时间用time计算为:0.06252360343933105
Process finished with exit code 0
perf_counter()
def perf_counter(): # real signature unknown; restored from __doc__
'''
perf_counter() -> float
Performance counter for benchmarking.
'''
return 0.0
import time
def myfunc1():
a = 1
for i in range(1000000):
a += i
time_begin_seconds1 = time.time() # 程序此刻的系统时间
per_counter_begin_seconds = time.perf_counter()
myfunc1()
time_end_seconds2 = time.time() # 程序此刻的系统时间
per_counter_end_seconds = time.perf_counter()
print("per_counter()的返回结果类型为:%s" % type(per_counter_begin_seconds))
print("CPU运行时间用perf_counter计算为:%s" % (per_counter_end_seconds - per_counter_begin_seconds))
print("系统运行时间用time计算为:%s" % (time_end_seconds2 - time_begin_seconds1))
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
per_counter()的返回结果类型为:
CPU运行时间用perf_counter计算为:0.07038768074221513
系统运行时间用time计算为:0.07810616493225098
Process finished with exit code 0
process_time()
def process_time(): # real signature unknown; restored from __doc__
'''
process_time() -> float
Process time for profiling: sum of the kernel and user-space CPU time.
'''
return 0.0
import time
def myfunc2():
a = 1
for i in range(1000000):
a += i
time_begin_seconds3 = time.time() # 程序此刻的系统时间
process_time_begin_seconds = time.process_time()
myfunc2()
time_end_seconds4 = time.time() # 程序此刻的系统时间
process_time_end_seconds = time.process_time()
print("process_time()的返回结果类型为:%s" % type(process_time_begin_seconds))
print("CPU运行时间用perf_counter计算为:%s" % (process_time_end_seconds - process_time_begin_seconds))
print("系统运行时间用time计算为:%s" % (time_end_seconds4 - time_begin_seconds3))
D:\mypy\venv\Scripts\python.exe D:/mypy/tmp.py
process_time()的返回结果类型为:
CPU运行时间用perf_counter计算为:0.0625
系统运行时间用time计算为:0.06248664855957031
Process finished with exit code 0