Python的时间模块


Pthon的time,datetime,calendar模块提供了和时间,日期,日历相关的功能

一、 time模块

《Python3从入门到实战》及大拿老师讲的Python课的笔记

#时间模块的属性
#timezong:当前时区和UTC时间相差的秒数
#在没有夏令时情况下的间隔
#altzone:与上差别:有夏令时的情况
#检查当前是否是夏令时时间状态,0表示是
print(time.timezone)
print(time.altzone)
print(time.daylight)
'''
-28800
-32400
0
'''
#UTC又称世界协调时间,以英国的格林尼治天文所所在地区的时间作为参考的时间,也叫做世界标准时间,中国时间是UTC+8区


    import time
    print(time.time())#返回的是时间戳,表示从1970年1月1日开始的毫秒数
    t = time.time()
    print(time.ctime(t))#将时间戳转为字符串
    print(time.ctime())#也可以直接得到当前时间
    print(time.localtime(t))#将时间戳转换为struct_time类型的本地时间

	'''
	运行结果如下:
	Thu Dec 31 15:27:39 2020
	Thu Dec 31 15:27:39 2020
	time.struct_time(tm_year=2020, tm_mon=12, tm_mday=31, tm_hour=15, tm_min=27, tm_sec=39, tm_wday=3, tm_yday=366, tm_isdst=0)
	tm_year是年,tm_mon是月,tm_mday是一个月中的第几天,tm_sec是秒,tm_wday表示一个星期中的第几天,tm_yday表示一年中的第几天,tm_isdst表示是否为夏令时(取值为0/1/-1)
	'''

通过函数time.ascytime(t)将元组形式或time.struct_time类型对象的时间转换为一个字符串


    print(time.asctime(time.localtime(time.time())))
    '''
    Thu Dec 31 15:32:58 2020
    '''
'''
mktime()使用时间元祖获取对应的时间戳
格式 time.mktime(时间元组)
返回值 浮点数时间戳
'''
lt = time.localtime()
ts = time.mktime(lt)
print(ts)

在这里插入图片描述
在这里插入图片描述

	#strftime
    t = time.localtime()
    ft = time.strftime("%Y年%m月%d日 %H:%M:%S",t)
    print(ft)
    '''
    2021年01月12日 08:59:29
    '''

二、datetime模块

datetime模块可以用于处理日期和时间信息的函数和类,如对如期和时间解析,格式化和算术运算

datetime.date:用于提供与时间无关的日期
datetime.time:用于独立于日期的时间
datetime.datetime:用于具有日期和时间的对象
datetime.timedelta:用于表示日期或日期时间之间的差异,如果用一个日期时间减另一个日期时间,结果还将是timedelta
datetime.timezone:表示时区调整为UTC的偏移量,该类是datetime.tzinfo的子类,不应直接使用.

datetime的一些方法
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


    import datetime
    t = datetime.time(1,2,3,23)#指定的时间
    print(t)
    print(t.hour,t.minute,t.second,t.microsecond,t.tzinfo)
    '''
    运行结果如下:
    01:02:03.000023
    1 2 3 23 None
    '''

min和max属性是一天中时间的最小值和最大值


    import datetime
    print(datetime.time.max,datetime.time.min)
    '''
    运行结果
    23:59:59.999999 00:00:00
    '''

datetime.date类 表示具有年,月,日属性的类,使用today()类方法可返回当前日期的date对象

	import datetime
    today = datetime.date.today()#今天的日期
    print(today)
    print(today.year,today.month,today.day)
    '''
    运行结果如下:
    2020-12-31
    2020 12 31
    '''
print(datetime.date(2019,7,30))#指定的日期

可以用datetime的ctime()方法返回一个日期的字符串表示


    import datetime
    t = datetime.date.today()
    print(t.ctime())
    
    #可以用datetime的timetuple方法返回一个time.struct_time对象
    import datetime
    t = datetime.date.today()
    tt = t.timetuple()
    print(tt)
    
    '''
    运行结果如下
    Thu Dec 31 00:00:00 2020
    time.struct_time(tm_year=2020, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=366, tm_isdst=-1)
    
    '''

所能表示的最大最小时间

	import datetime
    t = datetime.date.today()
    print(datetime.date.min,datetime.date.max)
    '''
    0001-01-01 9999-12-31
    '''

通过构造函数或replace()方法可以创建一个新的日期实例


    import datetime
    d1 = datetime.date(2020,10,21)
    print(d1.ctime())
    print(d1)
    #通过replace方法可以创建一个新的日期实例
    d2 = d1.replace(year=2018)
    print(d2)
    '''
    运行结果如下:
    Wed Oct 21 00:00:00 2020
    2020-10-21
    2018-10-21
    
    '''

datetime.date类的fromtimesamp()方法可以将一个时间戳转换为一个date类对象


    import time
    import datetime
    t = datetime.date.fromtimestamp(time.time())
    print(t)
    
    '''
    运行结果
    2020-12-31
    '''

#datetime.datetime类表示时间和日期的组合,可以用combine()函数组合日期和时间,也可以用datetime()函数构造一个包含日期和时间的datetime对象


    import datetime
    import time
    t = datetime.time(1,2,3)
    print(t)
    d = datetime.date.today()
    now = datetime.datetime.combine(d,t)
    long_ago = datetime.datetime(1999,3,14,12,30,58)
    print(now)
    print(long_ago)
    print(now.strftime("%a,%d %B %Y"))
    
    #datetime.datetime类的fromtimestamp()方法可以将一个时间戳转换为一个datetime对象
    tt = datetime.datetime.fromtimestamp(time.time())
    print(tt)
    df = now - long_ago#两个时间对象是可以相减的
    print(df)
    '''
    01:02:03
    2020-12-31 01:02:03
    1999-03-14 12:30:58
    Thu,31 December 2020
    2020-12-31 15:47:22.319659
    7962 days, 12:31:05
    '''
	#timedelta的使用
    t = datetime.datetime.now()#现在的时间
    print(t.strftime("%Y年%m月%d日 %H:%M:%S"))
    
    td = datetime.timedelta(hours=1)
    print((td+t).strftime("%Y年%m月%d日 %H:%M:%S"))    
    '''
    2021年01月12日 09:19:44
    2021年01月12日 10:19:44
    '''
	#timeit-时间测量工具
	t1 = timeit.timeit(stmt='[i for i in range(1000)]',number=1000)
    print(t1)
    '''
    0.03512760000012349
    '''
def f():
    print("我是一个函数")
#也可以用来测量一个函数的时间    
print(timeit.timeit(stmt=f,number=100))

三、calendar模块

	import calendar
    #calendar:跟日历相关的模块
    '''
    calendar“获取一年的日历字符串
    参数:
    w = 每个日期之间的间隔字符数
    l = 每周所占用的行数
    c = 每个月之间的间隔字符数
    '''
    cal = calendar.calendar(2020)#2020的日历
    print(cal)
    
    call = calendar.calendar(2020,w = 2,l=2,c=2)
    print(call)
	#isleap:判断某一年是不是闰年
    print(calendar.isleap(2020))
	#leapdays:获取指定年份之间闰年的个数
	print(calendar.leapdays(2000,2020))
#month()获取某个月的日历字符串
#格式:calendar.month(年,月)
#返回值:月日历的字符串
print(calendar.month(2020,7))
#monthrange()获取一个月的周几开始和天数
#格式:calendar.monthrange(年,月)
#返回值:元组(周几开始,总天数)
#注意 周默认从0——6表示周一到周日
print(calendar.monthrange(2020,8))
w ,t = calendar.monthrange(2020,7)
print(w)
print(t)
'''
monthcalendar()返回一个月每天的矩阵列表
格式:calendar.monthcalendar(年,月)
返回值:二级列表
注意:矩阵中没有天数用0表示
'''
m = calendar.monthcalendar(2018,3)
print(m)
'''
运行结果如下:
[[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]]
'''
#prcal:直接打印日历
print(calendar.prcal(2018))

'''
prmonth()打印整个月的日历
格式:calendar.prmonth(年,月)
返回值:无
'''
print(calendar.prmonth(2020,8))
'''
    August 2020
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
None
'''
'''
weekday()获取周几
格式:calendar.weekday(年,月,日)
返回值:周几对应的数字
'''
print(calendar.weekday(2020,8,6))
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值