5.3. datetime — 日期和时间处理
目的: datetime 模块包括很多函数和类,这些函数和类均用于日期和时间的解析、格式化以及计算。
datetime
包含用于处理日期与时间的函数和类。这些函数和类有的只是处理日期或时间的一种,有的可以日期时间一起处理。
时间
时间值由 time
类表示。 time
实例的属性包括 hour
、 minute
、 second
以及 microsecond
,并且还可以包括时区信息。
datetime_time.py
import datetime
t = datetime.time(1, 2, 3)
print(t)
print('hour :', t.hour)
print('minute :', t.minute)
print('second :', t.second)
print('microsecond:', t.microsecond)
print('tzinfo :', t.tzinfo)
初始化 time
实例的参数是可选的,但是默认情况下的 0
很有可能是不正确的。
$ python3 datetime_time.py
01:02:03
hour : 1
minute : 2
second : 3
microsecond: 0
tzinfo : None
time
实例只保存时间值,而不保存与时间相关的日期值。
datetime_time_minmax.py
import datetime
print('Earliest :', datetime.time.min)
print('Latest :', datetime.time.max)
print('Resolution:', datetime.time.resolution)
min
和 max
类的属性代表一天内的有效时间范围。
$ python3 datetime_time_minmax.py
Earliest : 00:00:00
Latest : 23:59:59.999999
Resolution: 0:00:00.000001
time
的精度(即 time
能够有效表示的最小时间间隔)被限制为 1 微秒。
datetime_time_resolution.py
import datetime
for m in [1, 0, 0.1, 0.6]:
try:
print('{:02.1f} :'.format(m),
datetime.time(0, 0, 0, microsecond=m))
except TypeError as err:
print('ERROR:', err)
微秒的浮点型数值会导致 TypeError
。
$ python3 datetime_time_resolution.py
1.0 : 00:00:00.000001
0.0 : 00:00:00
ERROR: integer argument expected, got float
ERROR: integer argument expected, got float
日期
日历日期可以用 date
类来描述。date
类实例化后有属性 year
, month
,和 day
。可以很容易的通过 date
类中的 today()
方法来创建一个表示当前时间的实例。
datetime_date.py
import datetime
today = datetime.date.today()
print(today)
print('ctime :', today.ctime())
tt = today.timetuple()
print('tuple : tm_year =', tt.tm_year)
print(' tm_mon =', tt.tm_mon)
print(' tm_mday =', tt.tm_mday)
print(' tm_hour =', tt.tm_hour)
print(' tm_min =', tt.tm_min)
print(' tm_sec =', tt.tm_sec)
print(' tm_wday =', tt.tm_wday)
print(' tm_yday =', tt.tm_yday)
print(' tm_isdst =', tt.tm_isdst)
print('ordinal:', today.toordinal())
print('Year :', today.year)
print('Mon :', today.month)
print('Day :', today.day)
这个例子通过特定格式输出了当前的日期:
$ python3 datetime_date.py
2018-03-18
ctime : Sun Mar 18 00:00:00 2018
tuple : tm_year = 2018
tm_mon = 3
tm_mday = 18
tm_hour = 0
tm_min = 0
tm_sec = 0
tm_wday = 6
tm_yday = 77
tm_isdst = -1
ordinal: 736771
Year : 2018
Mon : 3
Day : 18
还有一些类方法可以用来构造用 POSIX 时间戳或者使用整数表示公历日期的实例,其中第一年一月一日的值可以用 1 来表示,随后每过一天,该值递增1。
datetime_date_fromordinal.py
import datetime
import time
o = 733114
print('o :', o)
print('fromordinal(o) :', datetime.date.fromordinal(o))
t = time.time()
print('t :', t)
print('fromtimestamp(t):', datetime.date.fromtimestamp(t))
这个例子说明了使用fromordinal()
和 fromtimestamp()
的不同值类型。
$ python3 datetime_date_fromordinal.py
o : 733114
fromordinal(o) : 2008-03-13
t : 1521404434.262209
fromtimestamp(t): 2018-03-18
与 time
相同,可以使用 min
和 max
属性来确定支持的日期值范围。
datetime_date_minmax.py
import datetime
print('Earliest :', datetime.date.min)
print('Latest :', datetime.date.max)
print('Resolution:', datetime.date.resolution)
date 中表示时间的最小单位是天。
$ python3 datetime_date_minmax.py
Earliest : 0001-01-01
Latest : 9999-12-31
Resolution: 1 day, 0:00:00
另一种创建新 date
实例的方法是对现有的 date
实例进行replace()
操作。
datetime_date_replace.py
import datetime
d1 = datetime.date(2008, 3, 29)
print('d1:', d1.ctime())
d2 = d1.replace(year=2009)
print('d2:', d2.ctime())
这个例子中,改变了年的数值而保留了原实例中的日和月。
$ python3 datetime_date_replace.py
d1: Sat Mar 29 00:00:00 2008
d2: Sun Mar 29 00:00:00 2009
timedeltas (时间差)
可以使用两个 datetime
对象的基本运算来计算未来和过去的日期,或者是通过将一个 datetime
对象和一个 timedelta
对象相结合的方法来计算。不同的日期相减会产生一个 timedelta
,一个日期加上或者减去一个 timedelta
会产生另一个日期。 timedelta
的内部值以天、秒和微秒的形式存储。
datetime_timedelta.py
import datetime
print('microseconds:', datetime.timedelta(microseconds=1))
print('milliseconds:', datetime.timedelta(milliseconds=1))
print('seconds :', datetime.timedelta(seconds=1))
print('minutes :', datetime.timedelta(minutes=1))
print('hours :', datetime.timedelta(hours=1))
print('days :', datetime.timedelta(days=1))
print('weeks :', datetime.timedelta(weeks=1))
传递给构造函数的中间值(就是程序里面的那一堆 1 )被转换成了天数、秒数和微秒数。
$ python3 datetime_timedelta.py
microseconds: 0:00:00.000001
milliseconds: 0:00:00.001000
seconds : 0:00:01
minutes : 0:01:00
hours : 1:00:00
days : 1 day, 0:00:00
weeks : 7 days, 0:00:00
可以使用 total_seconds()
将 timedelta
的整个持续时间检索为秒数。
datetime_timedelta_total_seconds.py
import datetime
for delta in [datetime.timedelta(microseconds=1),
datetime.timedelta(milliseconds=1),
datetime.timedelta(seconds=1),
datetime.timedelta(minutes=1),
datetime.timedelta(hours=1),
datetime.timedelta(days=1),
datetime.timedelta(weeks=1),
]:
print('{:15} = {:8} seconds'.format(
str(delta), delta.total_seconds())
)
返回值是一个浮点数,以存储亚秒级(泛指比秒更短的时间单位)的持续时间。
$ python3 datetime_timedelta_total_seconds.py
0:00:00.000001 = 1e-06 seconds
0:00:00.001000 = 0.001 seconds
0:00:01 = 1.0 seconds
0:01:00 = 60.0 seconds
1:00:00 = 3600.0 seconds
1 day, 0:00:00 = 86400.0 seconds
7 days, 0:00:00 = 604800.0 seconds
日期算术运算
日期数学(可以理解为与日期相关的算术运算)使用标准算术运算符。
datetime_date_math.py
import datetime
today = datetime.date.today()
print('Today :', today)
one_day = datetime.timedelta(days=1)
print('One day :', one_day)
yesterday = today - one_day
print('Yesterday:', yesterday)
tomorrow = today + one_day
print('Tomorrow :', tomorrow)
print()
print('tomorrow - yesterday:', tomorrow - yesterday)
print('yesterday - tomorrow:', yesterday - tomorrow)
这个包含日期对象的例子演示了如何使用 timedelta
对象来计算新的日期,以及日期实例之间相减会产生时间差(包括负的差值)。
$ python3 datetime_date_math.py
Today : 2018-03-18
One day : 1 day, 0:00:00
Yesterday: 2018-03-17
Tomorrow : 2018-03-19
tomorrow - yesterday: 2 days, 0:00:00
yesterday - tomorrow: -2 days, 0:00:00
timedelta
对象还支持与整型数、浮点数和其它 timedelta
实例的算术运算。
datetime_timedelta_math.py
import datetime
one_day = datetime.timedelta(days=1)
print('1 day :', one_day)
print('5 days :', one_day * 5)
print('1.5 days :', one_day * 1.5)
print('1/4 day :', one_day / 4)
# 假设午饭时间为 1 小时
work_day = datetime.timedelta(hours=7)
meeting_length = datetime.timedelta(hours=1)
print('meetings per day :', work_day / meeting_length)
在这个例子中,我们用了几个不同的倍数去乘以单日,结果, timedelta
会根据不同的情况来保存适当的天数或小时数。最后的例子演示了如何用两个 timedelta
对象来计算日期数值。在这种情况下,计算结果是一个浮点数。
$ python3 datetime_timedelta_math.py
1 day : 1 day, 0:00:00
5 days : 5 days, 0:00:00
1.5 days : 1 day, 12:00:00
1/4 day : 6:00:00
meetings per day : 7.0
比较数值
日期值和时间值都可以使用标准比较运算符进行比较,以确定哪个值更早或更晚。
datetime_comparing.py
import datetime
import time
print('Times:')
t1 = datetime.time(12, 55, 0)
print(' t1:', t1)
t2 = datetime.time(13, 5, 0)
print(' t2:', t2)
print(' t1 < t2:', t1 < t2)
print()
print('Dates:')
d1 = datetime.date.today()
print(' d1:', d1)
d2 = datetime.date.today() + datetime.timedelta(days=1)
print(' d2:', d2)
print(' d1 > d2:', d1 > d2)
支持所有的比较运算符。
$ python3 datetime_comparing.py
Times:
t1: 12:55:00
t2: 13:05:00
t1 < t2: True
Dates:
d1: 2018-03-18
d2: 2018-03-19
d1 > d2: False
将日期和时间组合在一起
我们可以使用 datetime
类来保存由日期组件和时间组件组成的数值。与 date
一样,有一些很方便的类方法可以从其它常用值创建 datetime
实例。
datetime_datetime.py
import datetime
print('Now :', datetime.datetime.now())
print('Today :', datetime.datetime.today())
print('UTC Now:', datetime.datetime.utcnow())
print()
FIELDS = [
'year', 'month', 'day',
'hour', 'minute', 'second',
'microsecond',
]
d = datetime.datetime.now()
for attr in FIELDS:
print('{:15}: {}'.format(attr, getattr(d, attr)))
正如预期的那样, datetime
实例有 date
对象和 time
对象的所有属性。
$ python3 datetime_datetime.py
Now : 2018-03-18 16:20:34.811583
Today : 2018-03-18 16:20:34.811616
UTC Now: 2018-03-18 20:20:34.811627
year : 2018
month : 3
day : 18
hour : 16
minute : 20
second : 34
microsecond : 811817
正如 date
一样, datetime
提供了一些很方便的类方法,用于创建新的实例。此外, datetime
还拥有 fromordinal()
和 fromtimestamp()
。
datetime_datetime_combine.py
import datetime
t = datetime.time(1, 2, 3)
print('t :', t)
d = datetime.date.today()
print('d :', d)
dt = datetime.datetime.combine(d, t)
print('dt:', dt)
combine()
可以用一个 date
实例和一个 time
实例来创建一个 datetime
实例。
$ python3 datetime_datetime_combine.py
t : 01:02:03
d : 2018-03-18
dt: 2018-03-18 01:02:03
格式化和解析
datetime 对象默认的字符串表示法采用的是 ISO-8601 格式 (YYYY-MM-DDTHH:MM:SS.mmmmmm
)。当然也可以使用 strftime()
函数生成其它的格式。
datetime_datetime_strptime.py
import datetime
format = "%a %b %d %H:%M:%S %Y"
today = datetime.datetime.today()
print('ISO :', today)
s = today.strftime(format)
print('strftime:', s)
d = datetime.datetime.strptime(s, format)
print('strptime:', d.strftime(format))
使用 datetime.strptime()
将格式化的字符串转化为 datetime
实例。
$ python3 datetime_datetime_strptime.py
ISO : 2018-03-18 16:20:34.941204
strftime: Sun Mar 18 16:20:34 2018
strptime: Sun Mar 18 16:20:34 2018
相同的格式化代码可以与 Python 的 string formatting mini-language 一起使用,方法是将它们放在格式字符串的字段规范中的 :
后面。
datetime_format.py
import datetime
today = datetime.datetime.today()
print('ISO :', today)
print('format(): {:%a %b %d %H:%M:%S %Y}'.format(today))
每一个 datetime 格式代码均以 %
为前缀,并且后面的冒号被当作是要输出的字符。
$ python3 datetime_format.py
ISO : 2018-03-18 16:20:35.006116
format(): Sun Mar 18 16:20:35 2018
下表展示了美国/东部时区 2016 年 1 月 13 日下午 5 点的所有格式化代码。
strptime/strftime format codes
符号 | 含义 | 例子 |
---|---|---|
%a |
星期的简写 | 'Wed' |
%A |
星期的完整写法 | 'Wednesday' |
%w |
星期代号 -- 从0 (周日) 到 6 (周六) | '3' |
%d |
月的第几日(个位数时,十位补 0 ) | '13' |
%b |
月份的简写 | 'Jan' |
%B |
月份的完整写法 | 'January' |
%m |
月份 | '01' |
%y |
年份的缩写 | '16' |
%Y |
年份的完整写法 | '2016' |
%H |
小时( 24 小时制) | '17' |
%I |
小时( 12 小时制) | '05' |
%p |
AM/PM | 'PM' |
%M |
分钟 | '00' |
%S |
秒 | '00' |
%f |
微秒 | '000000' |
%z |
设置时区(适用于对时区敏感的类) | '-0500' |
%Z |
时区名 | 'EST' |
%j |
一年的第几天 | '013' |
%W |
一年的第几周 | '02' |
%c |
当地当前日期和时间 | 'Wed Jan 13 17:00:00 2016' |
%x |
当地当前日期 | '01/13/16' |
%X |
当地当前时间 | '17:00:00' |
%% |
一个 % |
'%' |
时区
在 datetime
中,时区由 tzinfo
的子类表示。由于 tzinfo
是一个抽象基类,因此应用程序需要定义一个子类。为了让这个类能用,应用程序还要为一些方法提供适当的实现。
在 timezone
类中, datetime
确实包含了一个略为简单的实现。它使用 UTC 的固定偏移量,并且不支持一年中不同日期的不同偏移量。比如,使用夏令时的地方或者 UTC 的偏移量会随时间变化的地方。
datetime_timezone.py
import datetime
min6 = datetime.timezone(datetime.timedelta(hours=-6))
plus6 = datetime.timezone(datetime.timedelta(hours=6))
d = datetime.datetime.now(min6)
print(min6, ':', d)
print(datetime.timezone.utc, ':',
d.astimezone(datetime.timezone.utc))
print(plus6, ':', d.astimezone(plus6))
# 转换为当前系统时间
d_system = d.astimezone()
print(d_system.tzinfo, ' :', d_system)
要想将 datetime 值从一个时区转换到另一个时区,可以使用 astimezone()
函数。在上面的例子中,在 UTC 的两边分别显示了 6小时的时区,并且还使用 datetime.timezone
中的 utc
实例作为参考。最后的输出行显示系统时区中的值,该值可以通过调用 astimezone()
函数获得,调用时不需要参数。
$ python3 datetime_timezone.py
UTC-06:00 : 2018-03-18 14:20:35.123594-06:00
UTC : 2018-03-18 20:20:35.123594+00:00
UTC+06:00 : 2018-03-19 02:20:35.123594+06:00
EDT : 2018-03-18 16:20:35.123594-04:00
注释
第三方模块 pytz 是时区更好的实现。它支持指定的时区,并且随着世界各地政治机构的变化,它能使偏移数据库能保持更新。
另请参阅
- datetime 的标准库文档
- [Python 2 到 3 关于 datetime 的迁移注释](pymotw.com/3/porting_notes.html# datetime 的迁移)
- [
calendar
](pymotw.com/3/calendar/index.html# calendar 模块「 calendar: 用于处理面向年、月、周和日的值的类。」) --calendar
模块。- [
time
](pymotw.com/3/time/index.html# time模块「 time: 时钟时间」) --time
模块。- dateutil -- Labix中的
dateutil
扩展了附加功能的datetime
模块。- pytz -- 用于创建
datetime
对象的世界时区数据库和类。- WikiPedia: Proleptic Gregorian calendar -- 公历系统的描述。
- ISO 8601 -- 日期和时间的数字表示标准。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。