1、常用函数
- time.time():返回当前的时间戳
- time.localtime([secs]):默认将当前时间戳转换成当前时区的struct_time
- time.sleep(secs):计时器
- time.strftime(format[,t]):把一个struct_time转换成格式化的时间字符串。此函数支持的格式符号如下表所示:
| 格式 | 含义 |
|---|---|
| %a | 本地(local)简化星期名称 |
| %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 | 一年中的星期数 |
| %w | 一个星期中的第几天(0~6,0是星期天) |
| %W | 和%w基本相同,区别在于%W是以星期一为一星期的开始 |
| %x | 本地相应日期 |
| %X | 本地相应时间 |
| %y | 简化的年份(00~99) |
| %Y | 完整的年份 |
| %Z | 时区的名字(若不存在则为空字符) |
| %% | %字符 |
在使用strftime()函数时,%p和%I配合使用才有效。%S中的秒是0~61,闰年中的秒占两秒。在使用strftime函数时,只有当前中的周数和天数被确定时,%U和%W才被计算。
[root@aliyun ~]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.time()
1647493346.2583184
>>> time.localtime()
time.struct_time(tm_year=2022, tm_mon=3, tm_mday=17, tm_hour=13, tm_min=2, tm_sec=35, tm_wday=3, tm_yday=76, tm_isdst=0)
>>> time.sleep(1)
>>> for i in range(5):
... time.sleep(1)
... print(i)
...
0
1
2
3
4
>>> time.strftime("%Y-%m-%d %X",time.localtime())
'2022-03-17 13:04:23'

浙公网安备 33010602011771号