版权声明:本文为博主原创文章,转载请注明原文链接。 https://blog.csdn.net/Frank_Abagnale/article/details/81909078
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#--------------------------------------------
# 简单介绍一下time模块
#
#---------------------------------------------
## time 模块
import time
if __name__ == "__main__":
print dir(time)
'''
['__doc__', '__name__', '__package__', 'accept2dyear', 'altzone', 'asctime', 'clock', 'ctime', \
'daylight', 'gmtime', 'localtime', 'mktime', 'sleep', 'strftime', 'strptime', 'struct_time', \
'time', 'timezone', 'tzname', 'tzset']
'''
#--------------------------------------------
# 时间戳:自历元(1970年1月1日00:00:00)到现在经过
# 的秒数。目前的范围是(1970~2038)
#--------------------------------------------
print "时间戳 t:",time.time() ## 时间戳 t: 1534837216.77
#--------------------------------------------
# 常用格式化时间表达
# time.localtime()
# %a 本地(local)简化星期名称
# %A 本地完整星期名称
# %b 本地简化月份名称
# %B 本地完整月份名称
# %c 本地相应的日期和时间表示
# %d 一个月中的第几天(01-31)
# %H 一天中的第几个小时(24小时制,00-23)
# %l 一天中的第几个小时(12小时制,01-12)
# %j 一年中的第几天(01-366)
# %m 月份(01-12)
# %M 分钟数(00-59)
# %p 本地am或者pm的相应符
# %S 秒(01-61)
# %U 一年中的星期数(00-53,星期天是一个星期的开始,第一个星期天之前的所有天数都放在第0周)
# %w 一个星期中的第几天(0-6,0是星期天)
# %W 和%U基本相同,不同的是%W以星期一为一个星期的开始
# %x 本地相应日期
# %X 本地相应时间
# %y 去掉世纪的年份(00-99)
# %Y 完整的年份
# %z 用+HHMM或者-HHMM表示距离格林威治的时区偏移(H代表十进制的小时数,M代表十进制的分钟数)
# %Z 时区的名字(如果不存在为空字符)
# %% %号本身
# %p只有与%I配合使用才有效果
# 当使用strptime()函数时,只有当在这年中的周数和天数被确定的时候%U和%W才会被计算
#--------------------------------------------
print "%Y-%m-%d %H:%M:%S",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
## %Y-%m-%d %H:%M:%S 2018-08-21 15:40:16
print "%b %d(%a) %Y %H:%M:%S ",time.strftime("%b %d(%a) %Y %H:%M:%S", time.localtime())
## %b %d(%a) %Y %H:%M:%S Aug 21(Tue) 2018 15:40:16
#--------------------------------------------
# 将格式字符串转换为时间戳
#--------------------------------------------
str_time = "2018-08-21 15:20:33"
## 转为时间戳:
stamp_time = time.mktime(time.strptime(str_time,"%Y-%m-%d %H:%M:%S"))
print "stamp:", stamp_time,"type:",type(stamp_time) ## stamp: 1534836033.0 type: <type 'float'>
## 时间戳转换格式:
newstr_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stamp_time))
print "new string:", newstr_time ## new string: 2018-08-21 15:20:33
## 字符串转元祖:
array_time = time.strptime(str_time, "%Y-%m-%d %H:%M:%S")
print "Array:",array_time ,"type:",type(array_time)
'''
Array: time.struct_time(tm_year=2018, tm_mon=8, tm_mday=21, tm_hour=15, tm_min=20,
tm_sec=33, tm_wday=1, tm_yday=233, tm_isdst=-1) type: <type 'time.struct_time'>
'''
newstr_time2 = time.strftime("%Y-%m-%d %H:%M:%S",array_time )
print "new string2:",newstr_time2 ## new string2: 2018-08-21 15:20:33
#--------------------------------------------
# 计算的秒数返回当前的CPU时间,用来衡量不同程序的耗时.
#--------------------------------------------
print "当前的CPU时间:", time.clock() ## 当前的CPU时间: 0.015586
上面介绍了python time模块的简单、常用的介绍。但是,作为python 一定还有更简单更方便使用的模块,例如arrow。
可以认为 Arrow 对象是一个增强版的 datetime 对象。
# pip install arrow
# idle
>>> import arrow
>>> arrow.now() # 当前本地时间
<Arrow [2018-08-24T07:09:03.468562+08:00]>
>>> arrow.utcnow() # 当前utc时间
<Arrow [2018-08-23T23:11:50.147585+00:00]>
>>> type(arrow.now().datetime)
<type 'datetime.datetime'>
>>> arrow.now().timestamp ## 时间戳
1535602673
>>> arrow.now().date()
datetime.date(2018, 8, 30)
>>> arrow.now().time()
datetime.time(12, 19, 52, 594274)
>>> a = arrow.now()
>>> a.year
2018
>>> a.month
8
>>> a.day
30
>>> a.hour
11
## shift 获取位移后的时间,例如获取上个月是几月:
>>> a.shift(months=-1)
<Arrow [2018-07-30T14:21:09.975506+08:00]>
>>> a.shift(months=-1).format("YYYYMM")
u'201807'
>>> a.shift(years=1).format("YYYYMM")
u'201908'
## 指定参数 months-1为向前推一个月,years=1为向后推一年。同理:
>>> a.shift(hours=-8).format("YYYYMMDD,hh:mm:ss")
u'20180830,06:21:09'
a.shift(weeks=1).format("YYYYMMDD,hh:mm:ss (d)")
u'20180906,02:21:09 (4)'
获取arrow对象的创建时间,并直接表达:
>>> a = arrow.now()
>>> a.humanize()
u'just now'
>>> a.humanize()
u'9 minutes ago'
## to 转换时区
>>> t=arrow.now()
>>> t
<Arrow [2018-08-30T15:02:44.008442+08:00]>
>>> t.format("YYYY-MM-DD HH:mm:ss (d)")
u'2018-08-30 15:02:44 (4)'
>>> t.to("utc").format("YYYY-MM-DD HH:mm:ss (d)")
u'2018-08-30 07:02:44 (4)'
>>> t.to("America/New_York").format("YYYY-MM-DD HH:mm:ss (d)")
u'2018-08-30 03:02:44 (4)'
## get创建对象(灵活转换时间戳和格式化时间)
t = arrow.get(1535613987) ## 接受时间戳
>>> t.format("YYYY-MM-DD HH:mm:ss")
u'2018-08-30 07:26:27'
import datetime
>>> arrow.get(datetime.date(2018,5,12)) ##接收一个时间对象
<Arrow [2018-05-12T00:00:00+00:00]>
>>> arrow.get(datetime.date(2018,5,12)).format("YYYY-MM-DD")
u'2018-05-12'
arrow.get("2017-05-19 23:59:59","YYYY-MM-DD HH:mm:ss") ##接受一个字符串参数
<Arrow [2017-05-19T23:59:59+00:00]>
>>> arrow.get("2017-05-19 23:59:59","YYYY-MM-DD HH:mm:ss") ## 将字符串转时间戳
<Arrow [2017-05-19T23:59:59+00:00]>
格式: