DateTime Objects¶
Various date and time objects are supplied by thedatetime module.Before using any of these functions, the header filedatetime.h must beincluded in your source (note that this is not included byPython.h),and the macroPyDateTime_IMPORT must be invoked, usually as part ofthe module initialisation function. The macro puts a pointer to a C structureinto a static variable,PyDateTimeAPI, that is used by the followingmacros.
- PyDateTime_IMPORT()¶
Import the datetime C API.
On success, populate the
PyDateTimeAPIpointer.On failure, setPyDateTimeAPItoNULLand set an exception.The caller must check if an error occurred viaPyErr_Occurred():PyDateTime_IMPORT;if(PyErr_Occurred()){/* cleanup */}
Warning
This is not compatible with subinterpreters.
- typePyDateTime_CAPI¶
Structure containing the fields for the datetime C API.
The fields of this structure are private and subject to change.
Do not use this directly; prefer
PyDateTime_*APIs instead.
- PyDateTime_CAPI*PyDateTimeAPI¶
Dynamically allocated object containing the datetime C API.
This variable is only available once
PyDateTime_IMPORTsucceeds.
- PyTypeObjectPyDateTime_DateType¶
This instance of
PyTypeObjectrepresents the Python date type;it is the same object asdatetime.datein the Python layer.
- PyTypeObjectPyDateTime_DateTimeType¶
This instance of
PyTypeObjectrepresents the Python datetime type;it is the same object asdatetime.datetimein the Python layer.
- PyTypeObjectPyDateTime_TimeType¶
This instance of
PyTypeObjectrepresents the Python time type;it is the same object asdatetime.timein the Python layer.
- PyTypeObjectPyDateTime_DeltaType¶
This instance of
PyTypeObjectrepresents the Python type forthe difference between two datetime values;it is the same object asdatetime.timedeltain the Python layer.
- PyTypeObjectPyDateTime_TZInfoType¶
This instance of
PyTypeObjectrepresents the Python time zone info type;it is the same object asdatetime.tzinfoin the Python layer.
Macro for access to the UTC singleton:
- PyObject*PyDateTime_TimeZone_UTC¶
Returns the time zone singleton representing UTC, the same object as
datetime.timezone.utc.Added in version 3.7.
Type-check macros:
- intPyDate_Check(PyObject*ob)¶
Return true ifob is of type
PyDateTime_DateTypeor a subtype ofPyDateTime_DateType.ob must not beNULL. This function alwayssucceeds.
- intPyDate_CheckExact(PyObject*ob)¶
Return true ifob is of type
PyDateTime_DateType.ob must not beNULL. This function always succeeds.
- intPyDateTime_Check(PyObject*ob)¶
Return true ifob is of type
PyDateTime_DateTimeTypeor a subtype ofPyDateTime_DateTimeType.ob must not beNULL. This function alwayssucceeds.
- intPyDateTime_CheckExact(PyObject*ob)¶
Return true ifob is of type
PyDateTime_DateTimeType.ob must notbeNULL. This function always succeeds.
- intPyTime_Check(PyObject*ob)¶
Return true ifob is of type
PyDateTime_TimeTypeor a subtype ofPyDateTime_TimeType.ob must not beNULL. This function alwayssucceeds.
- intPyTime_CheckExact(PyObject*ob)¶
Return true ifob is of type
PyDateTime_TimeType.ob must not beNULL. This function always succeeds.
- intPyDelta_Check(PyObject*ob)¶
Return true ifob is of type
PyDateTime_DeltaTypeor a subtype ofPyDateTime_DeltaType.ob must not beNULL. This function alwayssucceeds.
- intPyDelta_CheckExact(PyObject*ob)¶
Return true ifob is of type
PyDateTime_DeltaType.ob must not beNULL. This function always succeeds.
- intPyTZInfo_Check(PyObject*ob)¶
Return true ifob is of type
PyDateTime_TZInfoTypeor a subtype ofPyDateTime_TZInfoType.ob must not beNULL. This function alwayssucceeds.
- intPyTZInfo_CheckExact(PyObject*ob)¶
Return true ifob is of type
PyDateTime_TZInfoType.ob must not beNULL. This function always succeeds.
Macros to create objects:
- PyObject*PyDate_FromDate(intyear,intmonth,intday)¶
- Return value: New reference.
Return a
datetime.dateobject with the specified year, month and day.
- PyObject*PyDateTime_FromDateAndTime(intyear,intmonth,intday,inthour,intminute,intsecond,intusecond)¶
- Return value: New reference.
Return a
datetime.datetimeobject with the specified year, month, day, hour,minute, second and microsecond.
- PyObject*PyDateTime_FromDateAndTimeAndFold(intyear,intmonth,intday,inthour,intminute,intsecond,intusecond,intfold)¶
- Return value: New reference.
Return a
datetime.datetimeobject with the specified year, month, day, hour,minute, second, microsecond and fold.Added in version 3.6.
- PyObject*PyTime_FromTime(inthour,intminute,intsecond,intusecond)¶
- Return value: New reference.
Return a
datetime.timeobject with the specified hour, minute, second andmicrosecond.
- PyObject*PyTime_FromTimeAndFold(inthour,intminute,intsecond,intusecond,intfold)¶
- Return value: New reference.
Return a
datetime.timeobject with the specified hour, minute, second,microsecond and fold.Added in version 3.6.
- PyObject*PyDelta_FromDSU(intdays,intseconds,intuseconds)¶
- Return value: New reference.
Return a
datetime.timedeltaobject representing the given numberof days, seconds and microseconds. Normalization is performed so that theresulting number of microseconds and seconds lie in the ranges documented fordatetime.timedeltaobjects.
- PyObject*PyTimeZone_FromOffset(PyObject*offset)¶
- Return value: New reference.
Return a
datetime.timezoneobject with an unnamed fixed offsetrepresented by theoffset argument.Added in version 3.7.
- PyObject*PyTimeZone_FromOffsetAndName(PyObject*offset,PyObject*name)¶
- Return value: New reference.
Return a
datetime.timezoneobject with a fixed offset representedby theoffset argument and with tznamename.Added in version 3.7.
Macros to extract fields from date objects. The argument must be an instance ofPyDateTime_Date, including subclasses (such asPyDateTime_DateTime). The argument must not beNULL, and the type isnot checked:
- intPyDateTime_GET_YEAR(PyDateTime_Date*o)¶
Return the year, as a positive int.
- intPyDateTime_GET_MONTH(PyDateTime_Date*o)¶
Return the month, as an int from 1 through 12.
- intPyDateTime_GET_DAY(PyDateTime_Date*o)¶
Return the day, as an int from 1 through 31.
Macros to extract fields from datetime objects. The argument must be aninstance ofPyDateTime_DateTime, including subclasses. The argumentmust not beNULL, and the type is not checked:
- intPyDateTime_DATE_GET_HOUR(PyDateTime_DateTime*o)¶
Return the hour, as an int from 0 through 23.
- intPyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime*o)¶
Return the minute, as an int from 0 through 59.
- intPyDateTime_DATE_GET_SECOND(PyDateTime_DateTime*o)¶
Return the second, as an int from 0 through 59.
- intPyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime*o)¶
Return the microsecond, as an int from 0 through 999999.
- intPyDateTime_DATE_GET_FOLD(PyDateTime_DateTime*o)¶
Return the fold, as an int from 0 through 1.
Added in version 3.6.
- PyObject*PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime*o)¶
Return the tzinfo (which may be
None).Added in version 3.10.
Macros to extract fields from time objects. The argument must be an instance ofPyDateTime_Time, including subclasses. The argument must not beNULL,and the type is not checked:
- intPyDateTime_TIME_GET_HOUR(PyDateTime_Time*o)¶
Return the hour, as an int from 0 through 23.
- intPyDateTime_TIME_GET_MINUTE(PyDateTime_Time*o)¶
Return the minute, as an int from 0 through 59.
- intPyDateTime_TIME_GET_SECOND(PyDateTime_Time*o)¶
Return the second, as an int from 0 through 59.
- intPyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time*o)¶
Return the microsecond, as an int from 0 through 999999.
- intPyDateTime_TIME_GET_FOLD(PyDateTime_Time*o)¶
Return the fold, as an int from 0 through 1.
Added in version 3.6.
- PyObject*PyDateTime_TIME_GET_TZINFO(PyDateTime_Time*o)¶
Return the tzinfo (which may be
None).Added in version 3.10.
Macros to extract fields from time delta objects. The argument must be aninstance ofPyDateTime_Delta, including subclasses. The argument mustnot beNULL, and the type is not checked:
- intPyDateTime_DELTA_GET_DAYS(PyDateTime_Delta*o)¶
Return the number of days, as an int from -999999999 to 999999999.
Added in version 3.3.
- intPyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta*o)¶
Return the number of seconds, as an int from 0 through 86399.
Added in version 3.3.
- intPyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta*o)¶
Return the number of microseconds, as an int from 0 through 999999.
Added in version 3.3.
Macros for the convenience of modules implementing the DB API:
- PyObject*PyDateTime_FromTimestamp(PyObject*args)¶
- Return value: New reference.
Create and return a new
datetime.datetimeobject given an argumenttuple suitable for passing todatetime.datetime.fromtimestamp().
- PyObject*PyDate_FromTimestamp(PyObject*args)¶
- Return value: New reference.
Create and return a new
datetime.dateobject given an argumenttuple suitable for passing todatetime.date.fromtimestamp().
Internal data¶
The following symbols are exposed by the C API but should be consideredinternal-only.
- PyDateTime_CAPSULE_NAME¶
Name of the datetime capsule to pass to
PyCapsule_Import().Internal usage only. Use
PyDateTime_IMPORTinstead.