DateTime 物件

Various date and time objects are supplied by the datetime module. Before using any of these functions, the header file datetime.h must be included in your source (note that this is not included by Python.h), and the macro PyDateTime_IMPORT must be invoked, usually as part of the module initialisation function. The macro puts a pointer to a C structure into a static variable, PyDateTimeAPI, that is used by the following macros.

PyDateTime_IMPORT()

Import the datetime C API.

On success, populate the PyDateTimeAPI pointer. On failure, set PyDateTimeAPI to NULL and set an exception. The caller must check if an error occurred via PyErr_Occurred():

PyDateTime_IMPORT;
if (PyErr_Occurred()) { /* cleanup */ }

警告

This is not compatible with subinterpreters.

type PyDateTime_CAPI

Structure containing the fields for the datetime C API.

The fields of this structure are private and subject to change.

Do not use this directly; prefer PyDateTime_* APIs instead.

PyDateTime_CAPI *PyDateTimeAPI

Dynamically allocated object containing the datetime C API.

This variable is only available once PyDateTime_IMPORT succeeds.

type PyDateTime_Date

This subtype of PyObject represents a Python date object.

type PyDateTime_DateTime

This subtype of PyObject represents a Python datetime object.

type PyDateTime_Time

This subtype of PyObject represents a Python time object.

type PyDateTime_Delta

This subtype of PyObject represents the difference between two datetime values.

PyTypeObject PyDateTime_DateType

This instance of PyTypeObject represents the Python date type; it is the same object as datetime.date in the Python layer.

PyTypeObject PyDateTime_DateTimeType

This instance of PyTypeObject represents the Python datetime type; it is the same object as datetime.datetime in the Python layer.

PyTypeObject PyDateTime_TimeType

This instance of PyTypeObject represents the Python time type; it is the same object as datetime.time in the Python layer.

PyTypeObject PyDateTime_DeltaType

This instance of PyTypeObject represents the Python type for the difference between two datetime values; it is the same object as datetime.timedelta in the Python layer.

PyTypeObject PyDateTime_TZInfoType

This instance of PyTypeObject represents the Python time zone info type; it is the same object as datetime.tzinfo in the Python layer.

用於存取 UTC 單例 (singleton) 的巨集:

PyObject *PyDateTime_TimeZone_UTC

回傳表示 UTC 的時區單例,是與 datetime.timezone.utc 相同的物件。

在 3.7 版被加入.

型別檢查巨集:

int PyDate_Check(PyObject *ob)

如果 ob 的型別為 PyDateTime_DateTypePyDateTime_DateType 的子型別,則回傳 true。 ob 不得為 NULL。這個函式一定會執行成功。

int PyDate_CheckExact(PyObject *ob)

如果 ob 的型別為 PyDateTime_DateType,則回傳 true。 ob 不得為 NULL。這個函式一定會執行成功。

int PyDateTime_Check(PyObject *ob)

如果 ob 的型別為 PyDateTime_DateTimeTypePyDateTime_DateTimeType 的子型別,則回傳 true。ob 不得為 NULL。這個函式一定會執行成功。

int PyDateTime_CheckExact(PyObject *ob)

如果 ob 的型別為 PyDateTime_DateTimeType,則回傳 true。ob 不得為 NULL。這個函式一定會執行成功。

int PyTime_Check(PyObject *ob)

如果 ob 的型別為 PyDateTime_TimeTypePyDateTime_TimeType 的子型別,則回傳 true。ob 不得為 NULL。這個函式一定會執行成功。

int PyTime_CheckExact(PyObject *ob)

如果 ob 的型別為 PyDateTime_TimeType,則回傳 true。ob 不得為 NULL。這個函式一定會執行成功。

int PyDelta_Check(PyObject *ob)

如果 ob 的型別為 PyDateTime_DeltaTypePyDateTime_DeltaType 的子型別,則回傳 true。ob 不得為 NULL。這個函式一定會執行成功。

int PyDelta_CheckExact(PyObject *ob)

如果 ob 的型別為 PyDateTime_DeltaType,則回傳 true。ob 不得為 NULL。這個函式一定會執行成功。

int PyTZInfo_Check(PyObject *ob)

如果 ob 的型別為 PyDateTime_TZInfoTypePyDateTime_TZInfoType 的子型別,則回傳 true。ob 不得為 NULL。這個函式一定會執行成功。

int PyTZInfo_CheckExact(PyObject *ob)

如果 ob 的型別為 PyDateTime_TZInfoType,則回傳 true。 ob 不得為 NULL。這個函式一定會執行成功。

建立物件的巨集:

PyObject *PyDate_FromDate(int year, int month, int day)
回傳值:新的參照。

回傳一個有特定年、月、日的物件 datetime.date

PyObject *PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
回傳值:新的參照。

回傳一個有特定年、月、日、時、分、秒、微秒的物件 datetime.datetime

PyObject *PyDateTime_FromDateAndTimeAndFold(int year, int month, int day, int hour, int minute, int second, int usecond, int fold)
回傳值:新的參照。

回傳一個有特定年、月、日、時、分、秒、微秒與 fold(時間折疊)的物件 datetime.datetime

在 3.6 版被加入.

PyObject *PyTime_FromTime(int hour, int minute, int second, int usecond)
回傳值:新的參照。

回傳一個有特定時、分、秒、微秒的物件 datetime.date

PyObject *PyTime_FromTimeAndFold(int hour, int minute, int second, int usecond, int fold)
回傳值:新的參照。

回傳一個有特定時、分、秒、微秒與 fold(時間折疊)的物件 datetime.time

在 3.6 版被加入.

PyObject *PyDelta_FromDSU(int days, int seconds, int useconds)
回傳值:新的參照。

回傳一個 datetime.timedelta 物件,表示給定的天數、秒數和微秒數。執行標準化 (normalization) 以便生成的微秒數和秒數位於 datetime.timedelta 物件記錄的範圍內。

PyObject *PyTimeZone_FromOffset(PyObject *offset)
回傳值:新的參照。

回傳一個 datetime.timezone 物件,其未命名的固定偏移量由 offset 引數表示。

在 3.7 版被加入.

PyObject *PyTimeZone_FromOffsetAndName(PyObject *offset, PyObject *name)
回傳值:新的參照。

回傳一個 datetime.timezone 物件,其固定偏移量由 offset 引數表示,並帶有 tzname name

在 3.7 版被加入.

從 date 物件中提取欄位的巨集。引數必須是個 PyDateTime_Date 的實例,包括子類別(例如 PyDateTime_DateTime)。引數不得為 NULL,並且不會檢查型別:

int PyDateTime_GET_YEAR(PyDateTime_Date *o)

回傳年份,為正整數。

int PyDateTime_GET_MONTH(PyDateTime_Date *o)

回傳月份,為正整數,從 1 到 12。

int PyDateTime_GET_DAY(PyDateTime_Date *o)

回傳日期,為正整數,從 1 到 31。

從 datetime 物件中提取欄位的巨集。引數必須是個 PyDateTime_DateTime 的實例,包括子類別。引數不得為 NULL,並且不會檢查型別:

int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)

回傳小時,為正整數,從 0 到 23。

int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)

回傳分鐘,為正整數,從 0 到 59。

int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)

回傳秒,為正整數,從0 到59。

int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)

回傳微秒,為正整數,從 0 到 999999。

int PyDateTime_DATE_GET_FOLD(PyDateTime_DateTime *o)

回傳 fold,為 0 或 1 的正整數。

在 3.6 版被加入.

PyObject *PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime *o)

回傳 tzinfo(可能是 None)。

在 3.10 版被加入.

從 time 物件中提取欄位的巨集。引數必須是個 PyDateTime_Time 的實例,包括子類別。引數不得為 NULL,並且不會檢查型別:

int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)

回傳小時,為正整數,從 0 到 23。

int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)

回傳分鐘,為正整數,從 0 到 59。

int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)

回傳秒,為正整數,從0 到59。

int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)

回傳微秒,為正整數,從 0 到 999999。

int PyDateTime_TIME_GET_FOLD(PyDateTime_Time *o)

回傳 fold,為 0 或 1 的正整數。

在 3.6 版被加入.

PyObject *PyDateTime_TIME_GET_TZINFO(PyDateTime_Time *o)

回傳 tzinfo(可能是 None)。

在 3.10 版被加入.

從 time delta 物件中提取欄位的巨集。引數必須是個 PyDateTime_Delta 的實例,包括子類別。引數不能為 NULL,並且不會檢查型別:

int PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o)

以 -999999999 到 999999999 之間的整數形式回傳天數。

在 3.3 版被加入.

int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)

以 0 到 86399 之間的整數形式回傳秒數。

在 3.3 版被加入.

int PyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta *o)

以 0 到 999999 之間的整數形式回傳微秒數。

在 3.3 版被加入.

為了方便模組實作 DB API 的巨集:

PyObject *PyDateTime_FromTimestamp(PyObject *args)
回傳值:新的參照。

給定一個適合傳遞給 datetime.datetime.fromtimestamp() 的引數元組,建立並回傳一個新的 datetime.datetime 物件。

PyObject *PyDate_FromTimestamp(PyObject *args)
回傳值:新的參照。

給定一個適合傳遞給 datetime.date.fromtimestamp() 的引數元組,建立並回傳一個新的 datetime.date 物件。

Internal data

The following symbols are exposed by the C API but should be considered internal-only.

PyDateTime_CAPSULE_NAME

Name of the datetime capsule to pass to PyCapsule_Import().

Internal usage only. Use PyDateTime_IMPORT instead.