在Python中,和时间处理相关的模块有time,datatime,calendar(不常用)三个。
UTCC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。
在Python中时间的表示方式分三种:
- 时间戳(timestamp):时间戳表示从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
>>> time.time() 1521948761.733449 >>> type(time.time()) <class 'float'>
- 格式化的时间字符串:2014-11-11 11:11,即time.strftime('%Y-%m-%d')
>>> import time >>> time.strftime('%Y-%m-%d') '2018-03-25' - 元组(struct_time)共九个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()
>>> import time >>> time.localtime() time.struct_time( tm_year=2018, # 年 tm_mon=2, # 月 tm_mday=26, # 日 tm_hour=2, # 时 tm_min=47, # 分 tm_sec=49, # 秒 tm_wday=0, # 星期几(0代表星期日) tm_yday=57, # 一年中第几天 tm_isdst=0) # 是否夏令时,默认是-1
| time模块 |

time常用方法:
>>> time.time() # 返回当前时间的时间戳(按秒计算的浮点数),从1970到现在的秒数
1521948276.9173918
>>> time.localtime() # 打印本地时间(操作系统时间)
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=25, tm_hour=11, tm_min=22, tm_sec=27, tm_wday=6, tm_yday=84, tm_isdst=0)
>>> time.gmtime() # 打印格林威治时间(比北京时间晚8个小时)
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=25, tm_hour=3, tm_min=55, tm_sec=55, tm_wday=6, tm_yday=84, tm_isdst=0)
time.mktime() # 把一个时间对象转化为时间戳
>>> time.mktime(time.localtime())
1521950235.0
time.sleep() # 线程推迟指定的时间运行,单位为秒
time.asctime() # 把一个表示时间的元祖或struct time转换表示形式
>>> time.asctime() # 如果没有参数将time.localtime作为参数传入
'Mon Feb 26 10:59:10 2018'
time.ctime() # 把一个时间戳转化为time_asctime()形式,默认以time.time()为参数
>>> time.ctime() # 相当于time.asctime(time.localtime(secs))
'Mon Feb 26 11:06:29 2018'
>>> time.ctime(-231334422) # 参数可以为负
'Sun Sep 2 20:26:18 1962't
time.strftime(format,a) # 把一个代表时间的元祖或struct time转化为格式化的时间字符串
>>> time.strftime('%Y-%m-%d')
'2018-02-26'
>>> time.strftime('%Y-%m-%d %H:%M:%S')
'2018-02-26 11:19:47'
>>> time.strftime('%Y-%m-%d %H:%M:%S',a)
'2018-02-26 10:40:18'
>>> time.strftime('%Y-%m-%d %H:%M:%S %A',a)
'2018-02-26 10:40:18 Monday'
>>> time.strftime('%Y-%m-%d %H:%M:%S %p')
'2018-02-26 11:21:44 AM'
>>> time.strftime('%Y-%m-%d %H:%M:%S %U') # 今年的第几周
'2018-02-26 11:21:55 08'
>>> time.strftime('%Y-%m-%d %H:%M:%S %w') # 0-6,星期日是0
'2018-02-26 11:24:56 1'
time.strptime('string', format) # 把一个格式化时间字符串转化为struct_time,stftime的逆操作
>>> s = time.strftime('%Y %m-%d %H:%M:%S')
>>> s
'2018 02-26 11:42:16'
>>> time.strptime(s,'%Y %m-%d %H:%M:%S')
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=26, tm_hour=11, tm_min=42, \
tm_sec=16, tm_wday=0, tm_yday=57, tm_isdst=-1)
| datetime模块 |
datetime模块相比time模块,datetime模块的接口更直观,更容易调用。
datetime模块定义了下面几个类:
1、datetime.date:表示日期的类,常用的属性有year,month,day
2、datetime.time:表示时间的类,常用的属性有hour,minute,second,microsecond
3、datetime.datetime:表示日期时间
4、datetime.timedelta:表示时间间隔,即两个时间点之间的长度
5、datetime.tzinfo:与时区相关信息
>>> a = datetime.datetime.now()
datetime.datetime(2018, 2, 26, 12, 8, 18, 805166)
>>> import time
>>> d2 = datetime.date.fromtimestamp(time.time()) # 时间戳转化为年月日
datetime.date(2018, 2, 26)
>>> d2.timetuple() # 转化为时间对象
time.struct_time(tm_year=2018, tm_mon=2, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=57, tm_isdst=-1)
# 时间运算
>>> a = datetime.datetime.now()
>>> t1 = datetime.timedelta(1)
>>> a - t1
datetime.datetime(2018, 2, 25, 13, 37, 13, 812339)
>>> a - datetime.timedelta(days=1)
datetime.datetime(2018, 2, 25, 13, 37, 13, 812339)
>>> a - datetime.timedelta(days=3)
datetime.datetime(2018, 2, 23, 13, 37, 13, 812339)
# 还支持hours、minutes、secends的运算
>>> a - datetime.timedelta(hours=3)
datetime.datetime(2018, 2, 26, 10, 37, 13, 812339)
>>> a + datetime.timedelta(hours=3)
datetime.datetime(2018, 2, 26, 16, 37, 13, 812339)
# 时间替换
>>> n = datetime.datetime.now()
>>> n.replace(year=2016)
datetime.datetime(2016, 2, 26, 13, 45, 26, 863002)
>>> n.replace(year=2017,month=4)
datetime.datetime(2017, 4, 26, 13, 45, 26, 863002)
>>> n.replace(year=2017,month=4,day=13)
datetime.datetime(2017, 4, 13, 13, 45, 26, 863002)
# replace实现时间计算
>>> expire_year = time.localtime()[0] # 当前年份
>>> expire_date = datetime.datetime.now().replace(year=expire_year+10).strftime('%Y-%m-%d')
>>> expire_date
'2028-03-25'
来源:https://www.cnblogs.com/xiugeng/p/8643791.html