datetime — 基本日期和时间类型

源代码: Lib/datetime.py


The datetime 模块提供用于操纵日期和时间的类。

While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.

Tip

Skip to the format codes .

另请参阅

模块 calendar

一般日历相关功能。

模块 time

时间的访问和转换。

模块 zoneinfo

Concrete time zones representing the IANA time zone database.

dateutil

Third-party library with expanded time zone and parsing support.

DateType

Third-party library that introduces distinct static types to e.g. allow static type checkers to differentiate between naive and aware datetimes.

感知和单纯对象

Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include time zone information.

With sufficient knowledge of applicable algorithmic and political time adjustments, such as time zone and daylight saving time information, an aware object can locate itself relative to other aware objects. An aware object represents a specific moment in time that is not open to interpretation. [ 1 ]

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 time zone 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.

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 the 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.

Only one concrete tzinfo 类, timezone class, is supplied by the datetime 模块。 timezone class can represent simple time zones with fixed offsets from UTC, such as UTC itself or North American EST and EDT time zones. Supporting time zones 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.

常量

The datetime 模块导出下列常量:

datetime. MINYEAR

最小允许年数在 date or datetime 对象。 MINYEAR is 1.

datetime. MAXYEAR

最大允许年数在 date or datetime 对象。 MAXYEAR is 9999.

datetime. UTC

Alias for the UTC time zone singleton datetime.timezone.utc .

Added in version 3.11.