Python 日期时间处理实战:time 与 calendar 模块完全指南
什么是 Tick?
时间间隔以秒为单位的浮点数。特定时间点表示为自 1970 年 1 月 1 日 00:00:00 (纪元) 以来的秒数。
Python 提供流行的 time 模块处理时间及转换。函数 time.time() 返回自纪元以来的当前系统时间(tick)。
示例
在线演示#!/usr/bin/python import time ticks = time.time() print "Number of ticks since 12:00am, January 1, 1970:", ticks
输出类似:
Number of ticks since 12:00am, January 1, 1970: 7186862.73399
Tick 便于日期运算。但纪元前日期无法此形式表示。远未来日期也无法表示——UNIX 和 Windows 截止点约在 2038 年。
什么是 TimeTuple?
许多 Python 时间函数将时间作为 9 元素元组处理:
| 索引 | 字段 | 值 |
|---|---|---|
| 0 | 4位年份 | 2008 |
| 1 | 月份 | 1-12 |
| 2 | 日期 | 1-31 |
| 3 | 小时 | 0-23 |
| 4 | 分钟 | 0-59 |
| 5 | 秒 | 0-61(60或61为闰秒) |
| 6 | 星期几 | 0-6(0为周一) |
| 7 | 一年中的天数 | 1-366(儒略日) |
| 8 | 夏令时 | -1,0,1,-1指库确定DST |
上述元组等价于 struct_time 结构,属性如下:
| 索引 | 属性 | 值 |
|---|---|---|
| 0 | tm_year | 2008 |
| 1 | tm_mon | 1-12 |
| 2 | tm_mday | 1-31 |
| 3 | tm_hour | 0-23 |
| 4 | tm_min | 0-59 |
| 5 | tm_sec | 0-61(60或61为闰秒) |
| 6 | tm_wday | 0-6(0为周一) |
| 7 | tm_yday | 1-366(儒略日) |
| 8 | tm_isdst | -1,0,1,-1指库确定DST |
获取当前时间
将秒数浮点值转为时间元组,将浮点值传给函数(如 localtime),返回含九项有效值的时间元组:
在线演示#!/usr/bin/python import time localtime = time.localtime(time.time()) print "Local current time :", localtime
输出类似(可格式化为其他形式):
Local current time : time.struct_time(tm_year=2013, tm_mon=7, tm_mday=17, tm_hour=21, tm_min=26, tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)
获取格式化时间
可按需格式化时间,简单方法用 asctime():
在线演示#!/usr/bin/python import time localtime = time.asctime(time.localtime(time.time())) print "Local current time :", localtime
输出结果:
Local current time : Tue Jan 13 10:17:09 2009
获取月历
calendar 模块提供丰富方法操作年/月历。打印指定月历(2008年1月):
在线演示#!/usr/bin/python import calendar cal = calendar.month(2008, 1) print "Here is the calendar:" print cal
输出结果:
Here is the calendar: January 2008 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
time 模块
time 模块提供处理时间及转换的函数。所有可用方法:
| 序号 | 函数与说明 |
|---|---|
| 1 | time.altzone - 本地DST时区偏移UTC的秒数(西为正),若本地DST时区在UTC东(如西欧含UK)则为负。仅当daylight非零时使用 |
| 2 | time.asctime([tupletime]) - 接受时间元组,返回可读24字符串如'Tue Dec 11 18:07:14 2008' |
| 3 | time.clock() - 返回当前CPU时间浮点秒数。衡量不同方法计算成本比time.time()更有用 |
| 4 | time.ctime([secs]) - 类似asctime(localtime(secs)),无参数类似asctime() |
| 5 | time.gmtime([secs]) - 接受纪元秒数,返回UTC时间元组t。注意:t.tm_isdst始终为0 |
| 6 | time.localtime([secs]) - 接受纪元秒数,返回本地时间元组t(t.tm_isdst为0或1,视本地规则DST是否适用) |
| 7 | time.mktime(tupletime) - 接受本地时间元组,返回纪元秒数浮点值 |
| 8 | time.sleep(secs) - 挂起调用线程secs秒 |
| 9 | time.strftime(fmt[,tupletime]) - 接受本地时间元组,按字符串fmt格式返回时间字符串 |
| 10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') - 按格式字符串fmt解析str,返回时间元组格式的瞬间 |
| 11 | time.time() - 返回当前时间瞬间,纪元以来浮点秒数 |
| 12 | time.tzset() - 重置库例程使用的时间转换规则。环境变量TZ指定如何进行 |
time 模块两个重要属性:
| 序号 | 属性与说明 |
|---|---|
| 1 | time.timezone - 本地时区(无DST)偏移UTC的秒数(美洲>0;欧洲/亚洲/非洲大部分<=0) |
| 2 | time.tzname - 一对依赖区域设置的字符串,分别为无DST和有DST时的本地时区名 |
calendar 模块
calendar 模块提供日历相关函数,含打印指定月/年文本日历功能。
默认 calendar 以周一为周首日、周日为周末。可调用 calendar.setfirstweekday() 更改。
calendar 模块可用函数:
| 序号 | 函数与说明 |
|---|---|
| 1 | calendar.calendar(year,w=2,l=1,c=6) - 返回多行字符串,year年日历格式化为三列,列间c空格。w为各日期字符宽;每行长21*w+18+2*c。l为每周行数 |
| 2 | calendar.firstweekday() - 返回每周起始周码设置。默认导入calendar时为0(周一) |
| 3 | calendar.isleap(year) - year为闰年返回True,否则False |
| 4 | calendar.leapdays(y1,y2) - 返回range(y1,y2)年份范围内闰日总数 |
| 5 | calendar.month(year,month,w=2,l=1) - 返回多行字符串,year年month月日历,每周一行加两行表头。w为日期字符宽;每行长7*w+6。l为每周行数 |
| 6 | calendar.monthcalendar(year,month) - 返回整数列表列表。每子列表为一周。year年month月外的天设为0;月内天设为日期数1及以上 |
| 7 | calendar.monthrange(year,month) - 返回两整数。第一为year年month月首日周码;第二为月天数。周码0(周一)-6(周日);月份1-12 |
| 8 | calendar.prcal(year,w=2,l=1,c=6) - 类似print calendar.calendar(year,w,l,c) |
| 9 | calendar.prmonth(year,month,w=2,l=1) - 类似print calendar.month(year,month,w,l) |
| 10 | calendar.setfirstweekday(weekday) - 设置每周首日周码。周码0(周一)-6(周日) |
| 11 | calendar.timegm(tupletime) - time.gmtime逆函数:接受时间元组形式瞬间,返回纪元以来浮点秒数 |
| 12 | calendar.weekday(year,month,day) - 返回给定日期周码。周码0(周一)-6(周日);月份1(1月)-12(12月) |
其他模块与函数
感兴趣可查阅以下重要模块和函数处理日期时间:
- datetime 模块
- pytz 模块
- dateutil 模块
Python
- C++日期时间完全指南:ctime库、tm结构体与格式化输出
- 精通Python数据类型:实战指南
- 精通Python运算符:全面指南
- 精通Python Pass语句:实战指南
- 精通Python函数参数:位置、关键字与默认参数
- 精通 Python 字典:创建、操作与进阶技巧
- 精通 Python datetime:日期、时间与时区实战指南
- Python 获取当前日期和时间:实用指南
- Python 轻松获取当前时间与时区数据
- 精通 Python time 模块:函数、结构体与实战示例
- 精通 Python time.sleep():最佳实践、多线程与实战案例
- 掌握 Java 日期时间:构造器、方法与最佳实践