DateTime 对象

datetime 模块提供了各种日期和时间对象。 在使用这些函数之前,必须在你的源代码中包含头文件 datetime.h (请注意此文件并未包括在 Python.h 中),并且 PyDateTime_IMPORT 必须被唤起,通常是作为模块初始化函数的一部分。 这个宏会将指向特定 C 结构体的指针放入一个静态变量 PyDateTimeAPI 中,它将被下列的宏所使用。

  • type PyDateTime_Date
  • PyObject 的这个子类型表示 Python 日期对象。
  • type PyDateTime_DateTime
  • PyObject 的这个子类型表示 Python 日期时间对象。
  • type PyDateTime_Time
  • PyObject 的这个子类型表示 Python 时间对象。
  • type PyDateTime_Delta
  • PyObject 的这个子类型表示两个日期时间值之间的差值。
  • 这个 PyTypeObject 的实例是代表两个日期时间值之间差值的 Python 类型;它与 Python 层面的 datetime.timedelta 对象相同。

宏访问UTC单例:

Added in version 3.7.

类型检查宏:

  • 如果 ob 为 PyDateTime_DateType 类型或 PyDateTime_DateType 的某个子类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_DateType 类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_DateTimeType 类型或 PyDateTime_DateTimeType 的某个子类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • int PyDateTime_CheckExact(PyObject *ob)
  • 如果 ob 为 PyDateTime_DateTimeType 类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_TimeType 类型或 PyDateTime_TimeType 的某个子类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_TimeType 类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_DeltaType 类型或 PyDateTime_DeltaType 的某个子类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_DeltaType 类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_TZInfoType 类型或 PyDateTime_TZInfoType 的某个子类型则返回真值。 ob 不能为 NULL。 此函数总是会成功执行。
  • 如果 ob 为 PyDateTime_TZInfoType 类型则返回真值。 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)
  • 返回值:新的引用。 返回具有指定 year, month, day, hour, minute, second 和 microsecond 属性的 datetime.datetime 对象。
  • PyObject *PyDateTime_FromDateAndTimeAndFold(int year, int month, int day, int hour, int minute, int second, int usecond, int fold)
  • 返回值:新的引用。 返回具有指定 year, month, day, hour, minute, second, microsecond 和 fold 属性的 datetime.datetime 对象。

Added in version 3.6.

  • PyObject *PyTime_FromTime(int hour, int minute, int second, int usecond)
  • 返回值:新的引用。 返回具有指定 hour, minute, second and microsecond 属性的 datetime.time 对象。
  • PyObject *PyTime_FromTimeAndFold(int hour, int minute, int second, int usecond, int fold)
  • 返回值:新的引用。 返回具有指定 hour, minute, second, microsecond 和 fold 属性的 datetime.time 对象。

Added in version 3.6.

  • PyObject *PyDelta_FromDSU(int days, int seconds, int useconds)
  • 返回值:新的引用。 返回代表给定天、秒和微秒数的 datetime.timedelta 对象。 将执行正规化操作以使最终的微秒和秒数处在 datetime.timedelta 对象的文档指明的区间之内。
  • 返回值:新的引用。 返回一个 datetime.timezone 对象,该对象具有以 offset 参数表示 的未命名固定时差。

Added in version 3.7.

  • 返回值:新的引用。 返回一个 datetime.timezone 对象,该对象具有以 offset 参数表示的固定时差和时区名称 name。

Added in version 3.7.

一些用来从日期对象中提取字段的宏。 参数必须是 PyDateTime_Date 包括其子类 (如 PyDateTime_DateTime) 的实例。 参数不能为 NULL,且不会检查类型:

  • 以正整数的形式返回年份值。
  • 返回月,从0到12的整数。
  • 返回日期,从0到31的整数。

一些用来从日期时间对象中提取字段的宏。 参数必须是 PyDateTime_DateTime 包括其子类的实例。 参数不能为 NULL,并且不会检查类型:

  • 返回小时,从0到23的整数。
  • 返回分钟,从0到59的整数。
  • 返回秒,从0到59的整数。
  • 返回微秒,从0到999999的整数。
  • 返回折叠值,为整数 0 或 1。

Added in version 3.6.

  • 返回 tzinfo (可以为 None)。

Added in version 3.10.

一些用来从时间对象中提取字段的宏。 参数必须是 PyDateTime_Time 包括其子类的实例。 参数不能为 NULL,且不会检查类型:

  • 返回小时,从0到23的整数。
  • 返回分钟,从0到59的整数。
  • 返回秒,从0到59的整数。
  • 返回微秒,从0到999999的整数。
  • 返回折叠值,为整数 0 或 1。

Added in version 3.6.

  • 返回 tzinfo (可以为 None)。

Added in version 3.10.

一些用来从时间差对象中提取字段的宏。 参数必须是 PyDateTime_Delta 包括其子类的实例。参数不能为 NULL,并且不会检查类型:

  • 返回天数,从-999999999到999999999的整数。

Added in version 3.3.

  • 返回秒数,从0到86399的整数。

Added in version 3.3.

  • 返回微秒数,从0到999999的整数。

Added in version 3.3.

一些便于模块实现 DB API 的宏: