datetime
模組提供各種日期和時間物件。在使用任何這些函式之前,必須將標頭檔 datetime.h
引入於原始碼中(請注意,Python.h
並無引入該標頭檔),且巨集 PyDateTime_IMPORT
必須被叫用,而這通常作為模組初始化函式的一部分。該巨集將指向 C 結構的指標放入靜態變數 PyDateTimeAPI
中,該變數會被以下巨集使用。
This subtype of PyObject
represents the difference between two datetime values.
This instance of PyTypeObject
represents the Python date type;
it is the same object as datetime.date
in the Python layer.
This instance of PyTypeObject
represents the Python datetime type;
it is the same object as datetime.datetime
in the Python layer.
This instance of PyTypeObject
represents the Python time type;
it is the same object as datetime.time
in the Python layer.
This instance of PyTypeObject
represents Python type for
the difference between two datetime values;
it is the same object as datetime.timedelta
in the Python layer.
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) 的巨集:
回傳表示 UTC 的時區單例,是與 datetime.timezone.utc
相同的物件。
在 3.7 版被加入.
型別檢查巨集:
如果 ob 的型別為 PyDateTime_DateType
或 PyDateTime_DateType
的子型別,則回傳 true。 ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_DateType
,則回傳 true。 ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_DateTimeType
或 PyDateTime_DateTimeType
的子型別,則回傳 true。ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_DateTimeType
,則回傳 true。ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_TimeType
或 PyDateTime_TimeType
的子型別,則回傳 true。ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_TimeType
,則回傳 true。ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_DeltaType
或 PyDateTime_DeltaType
的子型別,則回傳 true。ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_DeltaType
,則回傳 true。ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_TZInfoType
或 PyDateTime_TZInfoType
的子型別,則回傳 true。ob 不得為 NULL
。這個函式一定會執行成功。
如果 ob 的型別為 PyDateTime_TZInfoType
,則回傳 true。 ob 不得為 NULL
。這個函式一定會執行成功。
建立物件的巨集:
回傳一個有特定年、月、日的物件 datetime.date
。
回傳一個有特定年、月、日、時、分、秒、微秒的物件 datetime.datetime
。
回傳一個有特定年、月、日、時、分、秒、微秒與 fold(時間折疊)的物件 datetime.datetime
。
在 3.6 版被加入.
回傳一個有特定時、分、秒、微秒的物件 datetime.date
。
回傳一個有特定時、分、秒、微秒與 fold(時間折疊)的物件 datetime.time
。
在 3.6 版被加入.
回傳一個 datetime.timedelta
物件,表示給定的天數、秒數和微秒數。執行標準化 (normalization) 以便生成的微秒數和秒數位於 datetime.timedelta
物件記錄的範圍內。
回傳一個 datetime.timezone
物件,其未命名的固定偏移量由 offset 引數表示。
在 3.7 版被加入.
回傳一個 datetime.timezone
物件,其固定偏移量由 offset 引數表示,並帶有 tzname name。
在 3.7 版被加入.
從 date 物件中提取欄位的巨集。引數必須是個 PyDateTime_Date
的實例,包括子類別(例如 PyDateTime_DateTime
)。引數不得為 NULL
,並且不會檢查型別:
回傳年份,為正整數。
回傳月份,為正整數,從 1 到 12。
回傳日期,為正整數,從 1 到 31。
從 datetime 物件中提取欄位的巨集。引數必須是個 PyDateTime_DateTime
的實例,包括子類別。引數不得為 NULL
,並且不會檢查型別:
回傳小時,為正整數,從 0 到 23。
回傳分鐘,為正整數,從 0 到 59。
回傳秒,為正整數,從0 到59。
回傳微秒,為正整數,從 0 到 999999。
回傳 fold,為 0 或 1 的正整數。
在 3.6 版被加入.
回傳 tzinfo(可能是 None
)。
在 3.10 版被加入.
從 time 物件中提取欄位的巨集。引數必須是個 PyDateTime_Time
的實例,包括子類別。引數不得為 NULL
,並且不會檢查型別:
回傳小時,為正整數,從 0 到 23。
回傳分鐘,為正整數,從 0 到 59。
回傳秒,為正整數,從0 到59。
回傳微秒,為正整數,從 0 到 999999。
回傳 fold,為 0 或 1 的正整數。
在 3.6 版被加入.
回傳 tzinfo(可能是 None
)。
在 3.10 版被加入.
從 time delta 物件中提取欄位的巨集。引數必須是個 PyDateTime_Delta
的實例,包括子類別。引數不能為 NULL
,並且不會檢查型別:
以 -999999999 到 999999999 之間的整數形式回傳天數。
在 3.3 版被加入.
以 0 到 86399 之間的整數形式回傳秒數。
在 3.3 版被加入.
以 0 到 999999 之間的整數形式回傳微秒數。
在 3.3 版被加入.
為了方便模組實作 DB API 的巨集:
給定一個適合傳遞給 datetime.datetime.fromtimestamp()
的引數元組,建立並回傳一個新的 datetime.datetime
物件。
給定一個適合傳遞給 datetime.date.fromtimestamp()
的引數元組,建立並回傳一個新的 datetime.date
物件。