Python---time、datatime、calendar模块

Time模块


时间的表示形式:
1.时间戳

以整数或浮点型表示的是一个秒为单位的时间间隔,这个时间的基础值1970.1.1的零点开始算起

2.元组格式

采用python的数据结构表示,这个元组有9个整型内容,分别表示不同含义

year  month   day  hours  minutes  seconds  weekday  Julia day   flag[1 夏令时  -1 根据当前时间判断   0 正常表示]

3.格式化字符串


返回当前时间的时间戳,浮点数形式,不需要参数
>>> time1 = time.time()
>>> time1
1522159406.3535008

时间戳转换为UTC时间
>>> gm = time.gmtime(time1)
>>> gm
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=27, tm_hour=14, tm_min=3, tm_sec=26, tm_wday=1, tm_yday=86, tm_isdst=0)

时间戳转为本地时间[把时间戳转为元组]
>>> lt = time.localtime(time1)
>>> lt
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=27, tm_hour=22, tm_min=3, tm_sec=26, tm_wday=1, tm_yday=86, tm_isdst=0)

本地时间元组转为时间戳
>>> mt = time.mktime(lt)
>>> mt
1522159406.0

时间元组格式转为字符串
>>> st = time.asctime(lt)
>>> st
'Tue Mar 27 22:03:26 2018'

将时间戳转换为字符串
>>> ct = time.ctime(time1)
>>> ct
'Tue Mar 27 22:03:26 2018'

将本地时间元组转为指定的格式的字符串
>>> strf = time.strftime("%Y-%m-%d %H:%M:%S", lt)
>>> strf
'2018-03-27 22:03:26'

将指定时间格式的字符串,转为时间元组
>>> strp = time.strptime('2018-03-27 22:03:26','%Y-%m-%d %X')
>>> strp
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=27, tm_hour=22, tm_min=3, tm_sec=26, tm_wday=1, tm_yday=86, tm_isdst=-1)

以浮点数计算的秒数返回当前cpu的时间,用来衡量不同的程序的耗时,常用
>>> time.clock()

休眠 单位:s
>>> time.sleep(2)

格式    含义    备注
%a    本地(locale)简化星期名称    
%A    本地完整星期名称    
%b    本地简化月份名称    
%B    本地完整月份名称    
%c    本地相应的日期和时间表示    
%d    一个月中的第几天(01 - 31)    
%H    一天中的第几个小时(24小时制,00 - 23)    
%I    第几个小时(12小时制,01 - 12)    
%j    一年中的第几天(001 - 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    时区的名字(如果不存在为空字符)    
%%    ‘%'字符

datetime模块


datetime比time高级了不少,可以理解为datetime基于time进行了封装,提供了更多的实用的函数,datetime的接口更加的直观,更容易调用。


模块中的类
datetime:同时有时间与日期
timedelta:表示时间间隔,即两个时间点的间隔:主要用于计算时间的跨度
tzinfo: 时区相关的信息
date : 只关注日期

获取系统当前时间
>>> datetime.datetime.now()
datetime.datetime(2018, 3, 28, 21, 59, 7, 95015)

获取指定时间
>>> time2 = datetime.datetime(2018, 3, 28, 21, 59, 7, 95015)
>>> print(time2)
2018-03-28 22:47:11.006712

将时间转为字符串       
>>> time3 = time1.strftime("%Y-%m-%d")
>>> time3
'2018-03-28'  

时间相减,返回一个时间间隔的对象
>>> time4 = datetime.datetime.now()
>>> time4
datetime.datetime(2018, 3, 28, 22, 53, 24, 709216)
>>> time5 = time4 -time1
>>> print(time5)
0:06:13.702504
>>> type(time5)
<class 'datetime.timedelta'>

间隔天数
>>> print(time5.days)

间隔天数之外的时间转为秒
>>> print(time5.seconds)
373

 

Calendar 模块


calendar模块有很广泛的方法用来处理年历和月历

返回指定的某月
>>>>>> calendar.month(2018, 4)

返回指定某年的日历
>>> calendar.calendar(2018)

返回当前每周起始日期的设置
>>> calendar.firstweekday()
0

判断某一年是否为闰年,如果是返回True,否则返回False
>>> calendar.isleap(2018)
False

返回在y1,y2之间闰年的总数
>>> calendar.leapdays(2018, 2080)
15

返回某年某月的weekday的第一天和这个月所有的天数   
>>> calendar.monthrange(2018,4)
(6, 30)

返回某个月以每一周为元素的序列
>>> calendar.monthcalendar(2018, 4)
[[0, 0, 0, 0, 0, 0, 1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 0, 0, 0, 0, 0, 0]]

返回给定日期的日期码---0(周一)到 6(周日)
>>> calendar.weekday(2018,4,2)
6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值