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.
-
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 asdatetime.date
in the Python layer.
-
PyTypeObject PyDateTime_DateTimeType¶
This instance of
PyTypeObject
represents the Python datetime type; it is the same object asdatetime.datetime
in the Python layer.
-
PyTypeObject PyDateTime_TimeType¶
This instance of
PyTypeObject
represents the Python time type; it is the same object asdatetime.time
in the Python layer.
-
PyTypeObject PyDateTime_DeltaType¶
This instance of
PyTypeObject
represents Python type for the difference between two datetime values; it is the same object asdatetime.timedelta
in the Python layer.
-
PyTypeObject PyDateTime_TZInfoType¶
This instance of
PyTypeObject
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 版新加入.
型別檢查巨集:
-
int PyDate_Check(PyObject *ob)¶
Return true if ob is of type
PyDateTime_DateType
or a subtype ofPyDateTime_DateType
. ob must not beNULL
. This function always succeeds.
-
int PyDate_CheckExact(PyObject *ob)¶
如果 ob 的型別為
PyDateTime_DateType
,則回傳 true。 ob 不得為NULL
。這個函式一定會執行成功。
-
int PyDateTime_Check(PyObject *ob)¶
Return true if ob is of type
PyDateTime_DateTimeType
or a subtype ofPyDateTime_DateTimeType
. ob must not beNULL
. This function always succeeds.
-
int PyDateTime_CheckExact(PyObject *ob)¶
如果 ob 的型別為
PyDateTime_DateTimeType
,則回傳 true。ob 不得為NULL
。這個函式一定會執行成功。
-
int PyTime_Check(PyObject *ob)¶
Return true if ob is of type
PyDateTime_TimeType
or a subtype ofPyDateTime_TimeType
. ob must not beNULL
. This function always succeeds.
-
int PyTime_CheckExact(PyObject *ob)¶
如果 ob 的型別為
PyDateTime_TimeType
,則回傳 true。ob 不得為NULL
。這個函式一定會執行成功。
-
int PyDelta_Check(PyObject *ob)¶
Return true if ob is of type
PyDateTime_DeltaType
or a subtype ofPyDateTime_DeltaType
. ob must not beNULL
. This function always succeeds.
-
int PyDelta_CheckExact(PyObject *ob)¶
如果 ob 的型別為
PyDateTime_DeltaType
,則回傳 true。ob 不得為NULL
。這個函式一定會執行成功。
-
int PyTZInfo_Check(PyObject *ob)¶
Return true if ob is of type
PyDateTime_TZInfoType
or a subtype ofPyDateTime_TZInfoType
. ob must not beNULL
. This function always succeeds.
-
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 版新加入.
Macros to extract fields from date objects. The argument must be an instance of
PyDateTime_Date
, including subclasses (such as
PyDateTime_DateTime
). The argument must not be NULL
, and the type is
not checked:
-
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。
Macros to extract fields from datetime objects. The argument must be an
instance of PyDateTime_DateTime
, including subclasses. The argument
must not be NULL
, and the type is not checked:
-
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 版新加入.
Macros to extract fields from time objects. The argument must be an instance of
PyDateTime_Time
, including subclasses. The argument must not be NULL
,
and the type is not checked:
-
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 版新加入.
Macros to extract fields from time delta objects. The argument must be an
instance of PyDateTime_Delta
, including subclasses. The argument must
not be NULL
, and the type is not checked:
-
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
物件。