8.1 datetime-Basic date and time types
日期时间-基本的日期时间类型
Source code: Lib/datetime.py
源码:Lib/datetime.py
The datetime module supplies classes for manipulating dates and times in both simple and complex ways.While date and time arithmetic is supported,the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.For related functionality,see also the time and calendar modules.
日期时间模块提供了简单和复杂两种操作日期和时间的类。当日期和时间支持运算时,实现的重点(焦点)就放在了对有效属性的提取和操作上。和日期时间相关的模块还可以看看time模块和calendar模块。
There are two kinds of date and timeobjects: "naive" and "aware"
有两种类型的日期和时间对象:naive和aware
An aware object has sufficient knowledge of applicable algorithmic and political time adjustments,such as time zone and daylight saving time information,to locate itself relative to other aware objects.An aware object is used to represent a specific moment in time that is not open to interpretation.
占位
A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects.Whether a naive object represents Coordinated Universal Time(UTC),local time,or time in some other timezone is purely up to the program,just like it is up to the program whether a particular number represents metres,miles,or mass.Naive objects are easy to understand and to work with,at the cost of ignoring some aspects of reality.
相对于其它的日期/时间对象来说,一个naive对象不能容纳足够的信息来清楚地定位它自己。一个naive对象表示的是UTC、本地时间,还是其它时区的时间,完全由程序来决定,就像naive对象由程序决定一个特别的数是表示米、英里还是质量一样。在以忽略某些真实的方面时,Naive对象很容易理解,并且很容易使用
For applications requiring aware objects,datetime and time objects have an optional time zone information attribute,tzinfo,that can be set to an instance of a subclass of abstract tzinfo class.These tzinfo objects capture information about the offset from UTC time,the time zone name,and whether Daylight Saving Time is in effect.Note that only one concrete tzinfo class,the timezone class,is supplied by the datetime module.The timezone class can represent simple timezones with fixed offset from UTC,such as UTC itself or North American EST and EDT timezones.Supporting timezones at deeper levels of detail is up to the application.The rules for time adjustment across the world are more political than rational,change frequently,and there is no standard suitable for every application aside from UTC.
对于需要aware对象的应用来说,日期时间和时间对象有一个可选的时间区域信息属性tzinfo,可以设置为一个抽象tzinfo类子类的实例,这些tzinfo对象捕获关于UTC时间的偏移、时间区域名、夏时制是否是有效的信息。需要注意的是仅仅一个具体的tzinfo类(timezone类)被datetime模块支持。timezone类可以表示与UTC时间固定偏移的简单时区,例如UTC自身时区、北美洲东部标准时间时区(EST)和东部夏季时间时区(DST)。在更深层次的时区支持细节被应用控制。世界各地的时间调整规则与合理性(或译为理性)相比更多的是政治,时区规则频敏改变,除了UTC时区外,没有一种标准对每一个应用程序都合适。
The datetime module export the following constants:
时期时间模块输出下列常数:
datetime.MINYEAR
日期时间类中的最小年常数变量(MINYEAR)
The smallest year number allowed in a date or datetime object.MINYEAR is 1.
在日期或日期时间对象中允许的最小年数。MINYEAR常量是1。
datetime.MAXYEAR
The largest year number allowed in a date or datetime object.MAXYEAR is 9999.
在日期或日期时间对象中允许的最大年数。MAXYEAR是9999。
----------------------------------------------------------------------------------
See also:
也可以看一下
Module calendar
日历模块
General calendar related funtions.
一般的日历相关函数
Module time
时间模块
Time access and conversions.
时间的使用和转换
-------------------------------------------------------------------------------------
8.1.1 Available Types
有效的(可用的)类型
class datetime.date
日期时间模块中的date类
An idealized naive date,assuming the current Gregorian calendar always was,and always will be,in effect.Attributes:year,month and day.
一个理想化的naive日期,假设当前一直是格林威治时间,实际上一直都会是。属性:年,月和日
class datetime.time
日期时间模块中的time类
An idealized time,independent of any particular day,assuming that every day has exactly 24*60*60 seconds (there is no notion of "leap seconds" here).
一个理想化的时间,独立于任何特定的一天,假设每天都有精确的24*60*60秒(这里没有"闰秒"的概念)
Attributes:hour,minute,second,microsecond,and tzinfo.
属性:小时,分钟,秒,微秒,tzinfo.
class datetime.datetime
日期时间模块中的日期时间类
A combination of a date and a time.Attributes:year,month,day,hour,minute,second,microsecond,and tzinfo.
日期和时间的结合。属性:年,月,日,时,分,秒,微秒,tzinfo。
class datetime.timedelta
日期时间模块中的时间间隔类
A duration expressing the difference between two date,time,or datetime instances to microsecond resolution.
用微秒的间隔来表达两个日期、时间或是日期时间实例的不同
class datetime.tzinfo
日期时间模块中的tzinfo类
An abstract base class for time zone information objects.These are used by the datetime and time classes to provide a customizable notion of time adjustment(for example,to account for time zone and/or daylight saving time).
时区信息对象的一个抽象基类。这些被日期时间类和时间类使用来提供一个时间调整的可定制的概念(例如,解释时区和/或夏时制)
class datetime.timezone
日期时间模块中的时区类
A class that implements the tzinfo abstract base class as a fixed offset from the UTC.
一个实现tzinfo抽象基类作为一个与UTC时区固定偏移的类
tzinfo和timezone解释的不是很清楚,自己也不是很理解
New in version 3.2.
在3.2版本中新增的
Objects of these types are immutable.
这些类型的对象是不可变的
Objects of the date type are always naive.
日期类型的对象总是naive。
An object of type time or datetime may be naive or aware.A datetime object d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None.If d.tzinfo is None,or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None,d is naive.A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None.Otherwise,t is naive.