相关文章推荐
Python-datetime — 日期和时间值操作

Python-datetime — 日期和时间值操作

目前在Django开发的时候遇到了很多datetime库的使用,而网上大多教程都是描述datetime中几个函数如何使用的,没有全面的对datatime库中的函数进行讲解,但在 datetime — Date and Time Value Manipulation 中发现了较为全面的datetime库函数的讲解,由于原网站讲解为英文,故翻译一下,方面今后使用的时候进行查找,也可以分享给大家。


目录:(方便在浏览器中进行Ctrl+F查询操作)

  1. Times
  2. Dates
  3. timedeltas
  4. Date Arithmetic
  5. Comparing Values
  6. Combining Dates and Times
  7. Formatting and Parsing
  8. Time Zones


Datetime — 日期和时间值操作


Purpose: Datetime模块包括用于进行日期和时间解析,格式化和算术的函数和类。Datetime包含单独和一起处理日期和时间的函数和类。


Times(时间操作)

时间值用时间类表示。 时间实例具有小时,分钟,秒和微秒的属性,还可以包括时区信息。

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)

初始化时间实例的参数是可选的,但默认值为0不太可能是正确的。

$ python3 datetime_time.py
01:02:03
hour       : 1
minute     : 2
second     : 3
microsecond: 0
tzinfo     : None

时间实例只保留时间值,而不是与时间相关联的日期。

datetime_time_minmax.py
import datetime
print('Earliest  :', datetime.time.min)
print('Latest    :', datetime.time.max)
print('Resolution:', datetime.time.resolution)

最小和最大类属性反映了一天中有效的时间范围。

$ python3 datetime_time_minmax.py
Earliest  : 00:00:00
Latest    : 23:59:59.999999
Resolution: 0:00:00.000001

时间的分辨率限制在整个微秒。(也可以说是一个微秒,详见上面代码Resolution)

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


Dates(日期操作)

日历日期值用日期类表示。 实例具有年、月和日的属性。 使用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
2017-07-30
ctime  : Sun Jul 30 00:00:00 2017
tuple  : tm_year  = 2017
         tm_mon   = 7
         tm_mday  = 30
         tm_hour  = 0
         tm_min   = 0
         tm_sec   = 0
         tm_wday  = 6
         tm_yday  = 211
         tm_isdst = -1
ordinal: 736540
Year   : 2017
Mon    : 7
Day    : 30

还有一些类方法,用于从POSIX时间戳或整数表示公历中的日期值创建实例,其中1年1月1日为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               : 1501432275.190121
fromtimestamp(t): 2017-07-30

与时间一样,支持的日期值的范围可以使用最小和最大属性来确定。

datetime_date_minmax.py
import datetime
print('Earliest  :', datetime.date.min)
print('Latest    :', datetime.date.max)
print('Resolution:', datetime.date.resolution)

日期的精确度是整天(也可以说日期的精确度是一个‘天’单位)

(原文如下:The resolution for dates is whole days.)

$ python3 datetime_date_minmax.py
Earliest  : 0001-01-01
Latest    : 9999-12-31
Resolution: 1 day, 0:00:00$ python3 datetime_date_minmax.py
Earliest  : 0001-01-01
Latest    : 9999-12-31
Resolution: 1 day, 0:00:00$ python3 datetime_date_minmax.py
Earliest  : 0001-01-01
Latest    : 9999-12-31
Resolution: 1 day, 0:00:00

创建新日期实例的另一种方法为对现有日期的进行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以产生另一个日期。imedelta的内部值以天,秒和微秒存储。

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))

传递给构造函数的中间级别值将转换为天数,秒和微秒。

$ 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

timedelta的完整持续时间可以使用total_seconds()以秒的形式被检索。

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


Date Arithmetic(日期算数)

日期数学使用标准算术运算符。

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对象来计算新的日期,并减去日期实例以产生timedeltas(包括负的增量值)。

$ python3 datetime_date_math.py
Today    : 2017-07-30
One day  : 1 day, 0:00:00
Yesterday: 2017-07-29
Tomorrow : 2017-07-31
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)
# assume an hour for lunch
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


Comparing Values

可以使用比较运算符(>.<)来比较日期和时间值,以确定哪个早期或更晚。

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: 2017-07-30
  d2: 2017-07-31
  d1 > d2: False


Combining Dates and Times

使用datetime类来保存由日期和时间组成的值。与日期一样,有几种方便的类方法可以从其他常用值创建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实例具有日期和时间对象的所有属性。

$ python3 datetime_datetime.py
Now    : 2017-07-30 12:31:15.519675
Today  : 2017-07-30 12:31:15.519706
UTC Now: 2017-07-30 16:31:15.519715
year           : 2017
month          : 7
day            : 30
hour           : 12
minute         : 31
second         : 15
microsecond    : 519852

与日期一样,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()从一个日期和一个时间实例创建datetime实例。

$ python3 datetime_datetime_combine.py
t : 01:02:03
d : 2017-07-30
dt: 2017-07-30 01:02:03


Formatting and Parsing

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     : 2017-07-30 12:31:15.601198
strftime: Sun Jul 30 12:31:15 2017
strptime: Sun Jul 30 12:31:15 2017

相同的格式代码可以与Python字符串格式化的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))

每个日期时间格式代码仍然必须以%为前缀,后续冒号将被视为文本字符以包含在输出中。

$ python3 datetime_format.py
ISO     : 2017-07-30 12:31:15.666149
format(): Sun Jul 30 12:31:15 2017

下表显示了2016年1月13日下午5:00在美国/东部时区的所有格式代码。


Time Zones

在datetime中,时区由tzinfo的子类表示。 由于tzinfo是一个抽象的基类,应用程序需要定义一个子类,并为几个方法提供适当的实现,使其可以进行使用。

datetime在类时区中包含了一些naive的实现,它使用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))
# convert to the current system timezone
d_system = d.astimezone()
 
推荐文章