DateTime 物件

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

type PyDateTime_Date

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

type PyDateTime_DateTime

PyObject 的子型別表示 Python datetime 物件。

type PyDateTime_Time

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

type PyDateTime_Delta

PyObject 的子型別表示兩個 datetime 值之間的差值。

PyTypeObject PyDateTime_DateType

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

PyTypeObject PyDateTime_DateTimeType

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

PyTypeObject PyDateTime_TimeType

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

PyTypeObject PyDateTime_DeltaType

PyTypeObject 的例項表示 Python 中兩個 datetime 值之間的差值的型別;它與 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_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)
返回值:新引用。

返回一個具有指定年、月、日、時、分、秒、微秒和摺疊的 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)
返回值:新引用。

返回一個具有指定時、分、秒、微秒和摺疊的 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 引數表示的固定偏移量,並且 tzname 為 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)

返回 fold,為 0 到 1 的整數。

3.6 版本中新增。

PyObject *PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime *o)

返回 tzinfo(可能為 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)

返回 fold,為 0 到 1 的整數。

3.6 版本中新增。

PyObject *PyDateTime_TIME_GET_TZINFO(PyDateTime_Time *o)

返回 tzinfo(可能為 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.fromtimestamp() 的引數元組,建立並返回一個新的 datetime.datetime 物件。

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

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