1.时间戳 timestamp 是从1970年1月1日0时0分0秒开始的秒数
2.struct_time 包含9个元素的tuple
3.format time 已经格式化好便于阅读的时间
使用时间需要使用time模块
import time引入time模块
time.time()方法获取当前的时间,以timestamp的形式
>>>time.time()
1576372527.424447
time.localtime()方法:以struct_time的形式获取当前的当地时间
>>>time.localtime()
time.struct_time(tm_year=2019, tm_mon=12, tm_mday=14,
tm_hour=20, tm_min=15, tm_sec=49, tm_wday=5, tm_yday=348, tm_isdst=0)
time.gmtime()方法:以struct_time的形式获取当前的格林尼治时间
从struct_time中获取具体的年月日:
ctime.tm_year ctime.tm_mon .....
ttm_tm_isdst = 1来告知mktime()现在处于夏令时,明确使用ttm.tm_isdst = 0来告知未处于夏令时
不同时间表示法的转换
struct_time转timestamp: time.mktime(<struct_time>)
timestamp转struct_time: time.localtime(time.time())
就是先获取当前的时间戳,然后按格式输出时间。使用方式如下:1、打开终端,2、输入:python,3、输入:importtime,4、输入:time.localtime([可以放入时间戳]),5、发现输出了一个struct_time结构,6、输入:time.localtime().tm_hour,7、获得了当前的小时值。
提取时间的正则表达式主要介绍了Python使用re模块正则提取字符串中括号内的内容。
python中要把字符串转换成日期格式需要使用time模块中的strptime函数,例子如下:import
timet
=
time.strptime('2016-05-09
21:09:30',
'%Y-%m-%d
%H:%M:%S')print(t)执行结果如下:
time.struct_time(tm_year=2016,
tm_mon=5,
tm_mday=9,