time模块(python)

一.sleep休眠

[root@rhel8 day04]# vim demo01_time.py
import time
def banzhuan():
    print("搬砖")
    time.sleep(3.5)  #让程序休眠3.5秒
    print("结束")

banzhuan()

[root@rhel8 day04]# python3 demo01_time.py 
搬砖
结束

运行时,会发现程序中间暂停了3.5秒

二.time.time时间戳

从1970年1月1日00:00:00开始,到现在的秒数

[root@rhel8 day04]# vim demo01_time.py
print(time.time())
[root@rhel8 day04]# python3 demo01_time.py 
1702115925.7090266

可以利用上面自定义的函数+时间戳,验证sleep的效果,用程序执行后的时间戳减去程序执行前的时间戳,查看得到的秒数

[root@rhel8 day04]# vim demo01_time.py
import time
def banzhuan():
    print("搬砖")
    time.sleep(3.5)  #让程序休眠3.5秒
    print("结束")



time1 = time.time()
banzhuan()
time2 = time.time()
print("spend:",time2-time1)
[root@rhel8 day04]# python3 demo01_time.py 
搬砖
结束
spend: 3.5039751529693604

结果显示,中间暂停了3.5秒。

三.结构化时间

将时间戳转换为UTC时区的结构化时间

[root@rhel8 day04]# vim demo01_time.py
import time

print(time.gmtime(time.time()))
[root@rhel8 day04]# python3 demo01_time.py 
time.struct_time(tm_year=2023, tm_mon=12, tm_mday=9, tm_hour=10, tm_min=3, tm_sec=49, tm_wday=5, tm_yday=343, tm_isdst=0)

time.struct_time(tm_year=2023, tm_mon=12, tm_mday=9, tm_hour=10, tm_min=3, tm_sec=49, tm_wday=5, tm_yday=343, tm_isdst=0) 

可以得到九个字段,每个字段分别代表:

tm_year代表年份
tm_mon代表月份
tm_mday代表当月的第几天
tm_hourUTC时区的时间,换算成北京时间需要加八个小时
tm_min小时的第几分钟
tm_sec分钟的第几秒
tm_wday一周的第几天,按照0,1,2,3,4,5,6计算,今天周六,就显示5
tm_yday一年的第多少天
tm_isdst是否为夏令时
可以用索引方式取值
import time

data = time.gmtime(time.time())
print(data)
print(data[0])
print(data.tm_year,data.tm_hour,data.tm_min,data.tm_sec)
[root@rhel8 day04]# python3 demo01_time.py 
time.struct_time(tm_year=2023, tm_mon=12, tm_mday=9, tm_hour=10, tm_min=14, tm_sec=46, tm_wday=5, tm_yday=343, tm_isdst=0)
2023
2023 10 14 46

四.localtime()

time.localtime()表示当前时区的结构化时间

[root@rhel8 day04]# vim demo01_time.py 
import time

data = time.localtime()
print(data)
[root@rhel8 day04]# python3 demo01_time.py 
time.struct_time(tm_year=2023, tm_mon=12, tm_mday=9, tm_hour=18, tm_min=24, tm_sec=14, tm_wday=5, tm_yday=343, tm_isdst=0)

五.结构化时间转换为时间戳

[root@rhel8 day04]# vim demo01_time.py 
import time

data = time.mktime(time.localtime())
print(data)
[root@rhel8 day04]# python3 demo01_time.py 
1702117553.0

六.结构化时间--->时间字符串

[root@rhel8 day04]# vim demo01_time.py 
import time
#%Y 年 %m 月 %d 日 %H 小时 %M 分钟 %S 秒
data = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
print(data)
[root@rhel8 day04]# python3 demo01_time.py 
2023-12-09 19:19:04

七.时间字符串--->结构化时间

[root@rhel8 day04]# vim demo01_time.py 
import time
#%Y 年 %m 月 %d 日 %H 小时 %M 分钟 %S 秒
data = time.strptime("2029-11-01 19:56:47","%Y-%m-%d %H:%M:%S")
print(data)

[root@rhel8 day04]# python3 demo01_time.py 
time.struct_time(tm_year=2029, tm_mon=11, tm_mday=1, tm_hour=19, tm_min=56, tm_sec=47, tm_wday=3, tm_yday=305, tm_isdst=-1)

八.总结

练习

有一个日志文件内容如下,要求取出9点--12点的内容

[root@rhel8 day04]# cat /opt/myweb.log 
2030-01-02 08:01:43 aaaaaaaaaaaaaaaa
2030-01-02 08:34:23 bbbbbbbbbbbbbbbb
2030-01-02 09:23:12 cccccccccccccccccccc
2030-01-02 10:56:13 dddddddddddddddddddddd
2030-01-02 11:38:19 eeeeeeeeeeeeeeeeeeeee
2030-01-02 12:02:28 ffffffffffffffffffff

 

使用readlines函数方式如下

[root@rhel8 day04]# vim test02_time.py
import time
t9 = time.strptime("2030-01-02 09:00:00","%Y-%m-%d %H:%M:%S")
t12 = time.strptime("2030-01-02 12:00:00","%Y-%m-%d %H:%M:%S")

with open("/opt/myweb.log",mode="r") as f:
    for line in f.readlines():
        t = time.strptime(line[:19],"%Y-%m-%d %H:%M:%S")
        if t9 <= t <= t12:
            print(line,end="")
[root@rhel8 day04]# python3 test02_time.py 
2030-01-02 09:23:12 cccccccccccccccccccc
2030-01-02 10:56:13 dddddddddddddddddddddd
2030-01-02 11:38:19 eeeeeeeeeeeeeeeeeeeee

 使用readlines函数方式如下

[root@rhel8 day04]# vim test02_time.py 
import time
t9 = time.strptime("2030-01-02 09:00:00","%Y-%m-%d %H:%M:%S")
t12 = time.strptime("2030-01-02 12:00:00","%Y-%m-%d %H:%M:%S")

with open("/opt/myweb.log",mode="r") as f:
    while True:
        data = f.readline()
        if len(data) == 0:
            break
        else:
            t = time.strptime(data[:19],"%Y-%m-%d %H:%M:%S")
            if t9 <= t <= t12:
                print(data,end="")
[root@rhel8 day04]# python3 test02_time.py 
2030-01-02 09:23:12 cccccccccccccccccccc
2030-01-02 10:56:13 dddddddddddddddddddddd
2030-01-02 11:38:19 eeeeeeeeeeeeeeeeeeeee

 

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值