日期时间对象¶
datetime
模块提供了各种日期和时间对象。在使用这些函数之前,必须在源代码中包含头文件 datetime.h
(请注意,此文件不包含在 Python.h
中),并且必须调用宏 PyDateTime_IMPORT
,通常作为模块初始化函数的一部分。该宏将指向 C 结构体的指针放入静态变量 PyDateTimeAPI
中,供以下宏使用。
-
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()
的参数元组。