DateTime 对象¶
各种日期和时间对象由 datetime 模块提供。在使用任何这些函数之前,必须在源代码中包含头文件 datetime.h(注意,Python.h 中不包含此文件),并且必须调用宏 PyDateTime_IMPORT,通常作为模块初始化函数的一部分。该宏将指向 C 结构的指针放入静态变量 PyDateTimeAPI 中,该变量由以下宏使用。
-
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_DateType或PyDateTime_DateType的子类型,则返回 true。*ob* 不能为NULL。此函数始终成功。
-
int PyDate_CheckExact(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_DateType,则返回 true。*ob* 不能为NULL。此函数始终成功。
-
int PyDateTime_Check(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_DateTimeType或PyDateTime_DateTimeType的子类型,则返回 true。*ob* 不能为NULL。此函数始终成功。
-
int PyDateTime_CheckExact(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_DateTimeType,则返回 true。 *ob* 不能为NULL。此函数始终成功。
-
int PyTime_Check(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_TimeType或PyDateTime_TimeType的子类型,则返回 true。 *ob* 不能为NULL。此函数始终成功。
-
int PyTime_CheckExact(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_TimeType,则返回 true。 *ob* 不能为NULL。此函数始终成功。
-
int PyDelta_Check(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_DeltaType或PyDateTime_DeltaType的子类型,则返回 true。 *ob* 不能为NULL。此函数始终成功。
-
int PyDelta_CheckExact(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_DeltaType,则返回 true。 *ob* 不能为NULL。此函数始终成功。
-
int PyTZInfo_Check(PyObject *ob)¶
如果 *ob* 的类型为
PyDateTime_TZInfoType或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)¶
- 返回值:新引用。
返回指定年份、月份、日期、小时、分钟、秒、微秒和折叠的
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* 参数表示的固定偏移量和时区名称 *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)¶
返回 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)¶
返回折叠,为 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)¶
以 int 类型返回天数,范围为 -999999999 到 999999999。
3.3 版新增。
-
int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)¶
以 int 类型返回秒数,范围为 0 到 86399。
3.3 版新增。
-
int PyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta *o)¶
以 int 类型返回微秒数,范围为 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对象。