详解python中处理时间的模块 (2)

time.mktime(t) 执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数。如果输入的值不是一个合法的时间,将触发 OverflowError 或 ValueError。其中 t — 结构化的时间或者完整的9位元组元素。

time.mktime(time.localtime()) 1545033603.0 time.mktime((2018, 12, 17, 16, 0, 0, 0, 351, 0)) 1545033600.0 datatime模块

datatime模块重新封装了time模块,提供更多接口,提供的类有:time,date,datetime,timedelta,tzinfo。

time类

time类表示时间值,属性有hour minute second microsecond tzinfo

>>> import datetime >>> t = datetime.time(11, 12, 13) >>> print(t) 11:12:13 >>> print(t.hour) 11 >>> print(t.minute) 12 >>> print(t.second) 13 >>> print(t.microsecond) 0 >>> print(t.tzinfo) None date类

日期值用date 类表示。实例具有属性year,month和 day。

>>> today = datetime.date.today() >>> print(today) 2018-12-17 >>> today.year 2018 >>> today.month 12 >>> today.day 17 >>> t = today.timetuple() >>> print(t) time.struct_time(tm_year=2018, tm_mon=12, tm_mday=17, tm_hour=0, tm_min=0, tm_se c=0, tm_wday=0, tm_yday=351, tm_isdst=-1) >>> d1 = datetime.date(2018,10,1) >>> print(d1) 2018-10-01 >>> d2 = d1.replace(month=11) >>> print(d2) 2018-11-01 timedelta类

用于时间的加减

>>> print(datetime.timedelta(microseconds=1)) 0:00:00.000001 >>> print(datetime.timedelta(milliseconds=1)) 0:00:00.001000 >>> print(datetime.timedelta(seconds=1)) 0:00:01 >>> print(datetime.timedelta(minutes=1)) 0:01:00 >>> print(datetime.timedelta(hours=1)) 1:00:00 >>> print(datetime.timedelta(days=1)) 1 day, 0:00:00 >>> print(datetime.timedelta(weeks=1)) 7 days, 0:00:00 >>> today = datetime.date.today() >>> print(today) 2018-12-17 >>> one_day = datetime.timedelta(days=1) >>> print(one_day) 1 day, 0:00:00 >>> yesterday = today - one_day >>> print(yesterday) 2018-12-16 >>> tomorrow = today + one_day >>> print(tomorrow) 2018-12-18 >>> print(tomorrow - yesterday) 2 days, 0:00:00 >>> print(yesterday - tomorrow) -2 days, 0:00:00 datetime类

datetime相当于date和time结合起来。其属性有year, month, day, hour , minute , second , microsecond , tzinfo

>>> today = datetime.datetime.today() >>> print(today) 2018-12-17 21:43:12.993074 >>> t = today.strftime("%a %b %d %H:%M:%S %Y") >>> print(t) Mon Dec 17 21:43:12 2018 tzinfo类

tzinfo类表示时区,但由于是抽象类,不能直接实现

calendar

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

>>> import calendar >>> cal = calendar.month(2018, 12) >>> print(cal) December 2018 Mo Tu We Th Fr Sa Su 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 31

基本方法:

#设置每周以周三开始算 >>> calendar.setfirstweekday(2) #判断是否是闰年 >>> print(calendar.isleap(2018)) False #返回某月的第一天是星期几和这个月的天数 >>> print(calendar.monthrange(2018, 12)) (5, 31) #返回某月每一周的列表集合 >>> print(calendar.monthcalendar(2018, 12)) [[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, 31, 0]] >>> calendar.setfirstweekday(0) >>> print(calendar.monthcalendar(2018, 12)) [[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], [31, 0, 0, 0, 0, 0, 0]]

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyxzwj.html