Getting Started with Python's built-in module - time module
1, time module
time translation is the time, this module is a time-dependent module
import time # 内置模块 -- 标准库
(1) time.time () timestamp
(Time stamp indicates the GMT from January 1970 00:00:00 1st offset press in seconds)
print(time.time()) # 时间戳 浮点数 秒
(2) time.sleep () sleep
time.sleep(3) # 秒
(3) time.localtime () to convert into a structured time stamp
print(time.localtime(time.time())) # 命名元组
print(time.localtime(time.time())[0])
print(time.localtime(time.time()).tm_year)
# 结构化时间:元组(struct_time) struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
tm_year, tm_mon, tm_mday,tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst
# 如:'2017-07-24 13-55-04'
time.localtime()
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24,
tm_hour=13, tm_min=59, tm_sec=37,
tm_wday=0, tm_yday=205, tm_isdst=0)
(4) time.strftime () converts the character string into a structured time
time_g = time.localtime()
print(time.strftime("%Y-%m-%d %H:%M:%S",time_g))
(5) time.strptime () converts the character string into a structured time
str_time = "2018-10-1 10:11:12"
time_g = time.strptime(str_time,"%Y-%m-%d %H:%M:%S")
(6) time.mktime () to convert the time stamp of the structure
print(time.mktime(time_g))
str_time = "2016-10-1 10:11:12"
time_int = time.time() - time.mktime(time.strptime(str_time,"%Y-%m-%d %H:%M:%S"))
print(time.localtime(time_int)
Summary: The computer is able to identify the timestamp of the time; time string is one can read time; tuple is used to operate the time
(7) python time date formatting symbols:
%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)
%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 当前时区的名称
%% %号本身