DateTime 物件

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

typePyDateTime_Date

This subtype ofPyObject represents a Python date object.

typePyDateTime_DateTime

This subtype ofPyObject represents a Python datetime object.

typePyDateTime_Time

This subtype ofPyObject represents a Python time object.

typePyDateTime_Delta

This subtype ofPyObject represents the difference between two datetime values.

PyTypeObjectPyDateTime_DateType

This instance ofPyTypeObject represents the Python date type;it is the same object asdatetime.date in the Python layer.

PyTypeObjectPyDateTime_DateTimeType

This instance ofPyTypeObject represents the Python datetime type;it is the same object asdatetime.datetime in the Python layer.

PyTypeObjectPyDateTime_TimeType

This instance ofPyTypeObject represents the Python time type;it is the same object asdatetime.time in the Python layer.

PyTypeObjectPyDateTime_DeltaType

This instance ofPyTypeObject represents Python type forthe difference between two datetime values;it is the same object asdatetime.timedelta in the Python layer.

PyTypeObjectPyDateTime_TZInfoType

This instance ofPyTypeObject represents the Python time zone info type;it is the same object asdatetime.tzinfo in the Python layer.

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

PyObject*PyDateTime_TimeZone_UTC

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

在 3.7 版被加入.

型別檢查巨集:

intPyDate_Check(PyObject*ob)

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

intPyDate_CheckExact(PyObject*ob)

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

intPyDateTime_Check(PyObject*ob)

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

intPyDateTime_CheckExact(PyObject*ob)

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

intPyTime_Check(PyObject*ob)

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

intPyTime_CheckExact(PyObject*ob)

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

intPyDelta_Check(PyObject*ob)

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

intPyDelta_CheckExact(PyObject*ob)

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

intPyTZInfo_Check(PyObject*ob)

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

intPyTZInfo_CheckExact(PyObject*ob)

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

建立物件的巨集:

PyObject*PyDate_FromDate(intyear,intmonth,intday)
回傳值:新的參照。

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

PyObject*PyDateTime_FromDateAndTime(intyear,intmonth,intday,inthour,intminute,intsecond,intusecond)
回傳值:新的參照。

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

PyObject*PyDateTime_FromDateAndTimeAndFold(intyear,intmonth,intday,inthour,intminute,intsecond,intusecond,intfold)
回傳值:新的參照。

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

在 3.6 版被加入.

PyObject*PyTime_FromTime(inthour,intminute,intsecond,intusecond)
回傳值:新的參照。

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

PyObject*PyTime_FromTimeAndFold(inthour,intminute,intsecond,intusecond,intfold)
回傳值:新的參照。

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

在 3.6 版被加入.

PyObject*PyDelta_FromDSU(intdays,intseconds,intuseconds)
回傳值:新的參照。

回傳一個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 引數表示,並帶有 tznamename

在 3.7 版被加入.

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

intPyDateTime_GET_YEAR(PyDateTime_Date*o)

回傳年份,為正整數。

intPyDateTime_GET_MONTH(PyDateTime_Date*o)

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

intPyDateTime_GET_DAY(PyDateTime_Date*o)

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

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

intPyDateTime_DATE_GET_HOUR(PyDateTime_DateTime*o)

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

intPyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime*o)

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

intPyDateTime_DATE_GET_SECOND(PyDateTime_DateTime*o)

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

intPyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime*o)

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

intPyDateTime_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,並且不會檢查型別:

intPyDateTime_TIME_GET_HOUR(PyDateTime_Time*o)

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

intPyDateTime_TIME_GET_MINUTE(PyDateTime_Time*o)

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

intPyDateTime_TIME_GET_SECOND(PyDateTime_Time*o)

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

intPyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time*o)

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

intPyDateTime_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,並且不會檢查型別:

intPyDateTime_DELTA_GET_DAYS(PyDateTime_Delta*o)

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

在 3.3 版被加入.

intPyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta*o)

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

在 3.3 版被加入.

intPyDateTime_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 物件。