日期時間物件

datetime 模組提供了各種日期和時間物件。在使用這些函式之前,必須在原始碼中包含標頭檔案 datetime.h(請注意,此檔案不包含在 Python.h 中),並且必須呼叫宏 PyDateTime_IMPORT,通常作為模組初始化函式的一部分。該宏將指向 C 結構體的指標放入靜態變數 PyDateTimeAPI 中,供以下宏使用。

型別 PyDateTime_Date

PyObject 的子型別表示 Python 日期物件。

型別 PyDateTime_DateTime

PyObject 的子型別表示 Python 日期時間物件。

型別 PyDateTime_Time

PyObject 的子型別表示 Python 時間物件。

型別 PyDateTime_Delta

PyObject 的子型別表示兩個日期時間值之間的差。

PyTypeObject PyDateTime_DateType

PyTypeObject 例項表示 Python 日期型別;它與 Python 層中的 datetime.date 物件相同。

PyTypeObject PyDateTime_DateTimeType

PyTypeObject 例項表示 Python 日期時間型別;它與 Python 層中的 datetime.datetime 物件相同。

PyTypeObject PyDateTime_TimeType

PyTypeObject 例項表示 Python 時間型別;它與 Python 層中的 datetime.time 物件相同。

PyTypeObject PyDateTime_DeltaType

PyTypeObject 例項表示兩個日期時間值之間差的 Python 型別;它與 Python 層中的 datetime.timedelta 物件相同。

PyTypeObject PyDateTime_TZInfoType

PyTypeObject 例項表示 Python 時區資訊型別;它與 Python 層中的 datetime.tzinfo 物件相同。

訪問 UTC 單例的宏

PyObject *PyDateTime_TimeZone_UTC

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

在 3.7 版本加入。

型別檢查宏

int PyDate_Check(PyObject *ob)

如果 *ob* 是 PyDateTime_DateType 型別或其子型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyDate_CheckExact(PyObject *ob)

如果 *ob* 是 PyDateTime_DateType 型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyDateTime_Check(PyObject *ob)

如果 *ob* 是 PyDateTime_DateTimeType 型別或其子型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyDateTime_CheckExact(PyObject *ob)

如果 *ob* 是 PyDateTime_DateTimeType 型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyTime_Check(PyObject *ob)

如果 *ob* 是 PyDateTime_TimeType 型別或其子型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyTime_CheckExact(PyObject *ob)

如果 *ob* 是 PyDateTime_TimeType 型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyDelta_Check(PyObject *ob)

如果 *ob* 是 PyDateTime_DeltaType 型別或其子型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyDelta_CheckExact(PyObject *ob)

如果 *ob* 是 PyDateTime_DeltaType 型別,則返回 true。*ob* 不能為 NULL。此函式總是成功。

int PyTZInfo_Check(PyObject *ob)

如果 *ob* 是 PyDateTime_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.time 物件。

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 物件,表示給定天數、秒數和微秒數。將執行規範化,使得結果微秒數和秒數位於 datetime.timedelta 物件文件中規定的範圍內。

PyObject *PyTimeZone_FromOffset(PyObject *offset)
返回值:新引用。

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

在 3.7 版本加入。

PyObject *PyTimeZone_FromOffsetAndName(PyObject *offset, PyObject *name)
返回值:新引用。

返回一個 datetime.timezone 物件,其具有由 *offset* 引數表示的固定偏移量和時區名稱 *name*。

在 3.7 版本加入。

用於從日期物件中提取欄位的宏。引數必須是 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 之間的整數。

用於從日期時間物件中提取欄位的宏。引數必須是 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)

返回摺疊,作為 0 到 1 之間的整數。

在 3.6 版本加入。

PyObject *PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime *o)

返回時區資訊(可能為 None)。

在 3.10 版本加入。

用於從時間物件中提取欄位的宏。引數必須是 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)

返回摺疊,作為 0 到 1 之間的整數。

在 3.6 版本加入。

PyObject *PyDateTime_TIME_GET_TZINFO(PyDateTime_Time *o)

返回時區資訊(可能為 None)。

在 3.10 版本加入。

用於從時間差物件中提取欄位的宏。引數必須是 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 物件並返回它,給定適合傳遞給 datetime.datetime.fromtimestamp() 的引數元組。

PyObject *PyDate_FromTimestamp(PyObject *args)
返回值:新引用。

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