当前位置:网站首页>time标准库
time标准库
2022-07-07 14:24:00 【努力卷】
传送门:零基础Python手把手学编程课程2020最新Python零基础入门课程(完结)想学Python这部视频就够了_哔哩哔哩_bilibili
time模块中三种时间表示方式
- 时间戳
- 结构化时间对象
- 格式化时间字符串
时间戳
import time
时间戳 1970.1.1 到指定时间的间隔 单位是秒
time.time() 生成当前时间的时间戳
time.time()-3600 一个小时之前的时间戳
结构化时间对象
st = time.localtime()
print(type(st))
print(st)
输出结果:
<class 'time.struct_time'>
time.struct_time(tm_year=2022, tm_mon=6, tm_mday=17, tm_hour=16, tm_min=15, tm_sec=12, tm_wday=4, tm_yday=168, tm_isdst=0)
st 本质上是一个元组
print('今天是{}-{:02d}-{}'.format(st[0],st[1],st[2]))
print('今天是 星期{}'.format(st.tm_wday+1))
输出结果:
今天是2022-06-17
今天是 星期5
对象的属性是只读的 不能改
格式化时间字符串
print(time.ctime())
输出结果:
Fri Jun 17 16:20:20 2022
strftime(时间格式)'%Y-%m-%d %H:%M:%S'
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y年%m月%d日 %H时%M分%S秒'))
输出结果:
2022-06-17 16:25:44
2022年06月17日 16时25分44秒
如果报错:
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error
就需要在代码中添加:
import locale
locale.setlocale(locale.LC_CTYPE,'chinese')
sleep:
t1 = time.time()
print('sleep begin...')
time.sleep(1.23)
print('sleep end')
t2 = time.time()
print("执行了{:.3f}秒".format(t2-t1))
三种格式之间的转换:
时间戳 转换为 结构化对象
# UTC时间
time.gmtime(time.time())
time.gmtime()
# local
print(time.localtime())
print(time.localtime(time.time()))
结构化对象 转换为 时间戳
# mktime(st)
print(time.time())
print(time.mktime(time.localtime()))
结构化对象 转换为 格式化时间字符串
# strftime(format,st)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
print(time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime()))
格式化字符串 转换为 结构化时间对象
# strptime(str, format)
strtime = '2020-07-25 13:23:18'
print(time.strptime(strtime,'%Y-%m-%d %H:%M:%S'))
边栏推荐
- OpenGL personal notes
- 如何快速检查钢网开口面积比是否符合 IPC7525
- 华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
- Sqlserver2014+: create indexes while creating tables
- Set the route and optimize the URL in thinkphp3.2.3
- PHP实现微信小程序人脸识别刷脸登录功能
- 作为Android开发程序员,android高级面试
- You Yuxi, coming!
- Horizontal and vertical centering method and compatibility
- 01tire+链式前向星+dfs+贪心练习题.1
猜你喜欢
Three. JS series (2): API structure diagram-2
作为Android开发程序员,android高级面试
As an Android Developer programmer, Android advanced interview
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Continuous creation depends on it!
Personal notes of graphics (2)
Leetcode-231-2的幂
3000 words speak through HTTP cache
[Android -- data storage] use SQLite to store data
二叉搜索树(基操篇)
随机推荐
PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
Opportunity interview experience summary
【DesignMode】代理模式(proxy pattern)
1亿单身男女“在线相亲”,撑起130亿IPO
Odoo integrated plausible embedded code monitoring platform
Imitate the choice of enterprise wechat conference room
【PHP】PHP接口继承及接口多继承原理与实现方法
AutoLISP series (2): function function 2
How can laravel get the public path
IP地址和物理地址有什么区别
Communication mode between application program and MATLAB
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
【DesignMode】享元模式(Flyweight Pattern)
Odoo集成Plausible埋码监控平台
Detailed explanation of several ideas for implementing timed tasks in PHP
prometheus api删除某个指定job的所有数据
How to determine whether the checkbox in JS is selected
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
iptables只允许指定ip地址访问指定端口