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.
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.New in version 3.7.
Type-check macros:
- int
PyDate_Check(PyObject *ob)¶ Return true ifob is of type
PyDateTime_DateTypeor a subtype ofPyDateTime_DateType.ob must not beNULL. This function alwayssucceeds.
- int
PyDate_CheckExact(PyObject *ob)¶ Return true ifob is of type
PyDateTime_DateType.ob must not beNULL. This function always succeeds.
- int
PyDateTime_Check(PyObject *ob)¶ Return true ifob is of type
PyDateTime_DateTimeTypeor a subtype ofPyDateTime_DateTimeType.ob must not beNULL. This function alwayssucceeds.
- int
PyDateTime_CheckExact(PyObject *ob)¶ Return true ifob is of type
PyDateTime_DateTimeType.ob must notbeNULL. This function always succeeds.
- int
PyTime_Check(PyObject *ob)¶ Return true ifob is of type
PyDateTime_TimeTypeor a subtype ofPyDateTime_TimeType.ob must not beNULL. This function alwayssucceeds.
- int
PyTime_CheckExact(PyObject *ob)¶ Return true ifob is of type
PyDateTime_TimeType.ob must not beNULL. This function always succeeds.
- int
PyDelta_Check(PyObject *ob)¶ Return true ifob is of type
PyDateTime_DeltaTypeor a subtype ofPyDateTime_DeltaType.ob must not beNULL. This function alwayssucceeds.
- int
PyDelta_CheckExact(PyObject *ob)¶ Return true ifob is of type
PyDateTime_DeltaType.ob must not beNULL. This function always succeeds.
- int
PyTZInfo_Check(PyObject *ob)¶ Return true ifob is of type
PyDateTime_TZInfoTypeor a subtype ofPyDateTime_TZInfoType.ob must not beNULL. This function alwayssucceeds.
- int
PyTZInfo_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.New 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.New 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(PyDateTime_DeltaType *offset)¶ - Return value: New reference.
Return a
datetime.timezoneobject with an unnamed fixed offsetrepresented by theoffset argument.New in version 3.7.
- PyObject *
PyTimeZone_FromOffsetAndName(PyDateTime_DeltaType *offset, PyUnicode *name)¶ - Return value: New reference.
Return a
datetime.timezoneobject with a fixed offset representedby theoffset argument and with tznamename.New 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:
- int
PyDateTime_GET_YEAR(PyDateTime_Date *o)¶ Return the year, as a positive int.
- int
PyDateTime_GET_MONTH(PyDateTime_Date *o)¶ Return the month, as an int from 1 through 12.
- int
PyDateTime_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:
- int
PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)¶ Return the hour, as an int from 0 through 23.
- int
PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)¶ Return the minute, as an int from 0 through 59.
- int
PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)¶ Return the second, as an int from 0 through 59.
- int
PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)¶ Return the microsecond, as an int from 0 through 999999.
- int
PyDateTime_DATE_GET_FOLD(PyDateTime_DateTime *o)¶ Return the fold, as an int from 0 through 1.
New in version 3.6.
- PyObject *
PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime *o)¶ Return the tzinfo (which may be
None).New 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:
- int
PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)¶ Return the hour, as an int from 0 through 23.
- int
PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)¶ Return the minute, as an int from 0 through 59.
- int
PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)¶ Return the second, as an int from 0 through 59.
- int
PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)¶ Return the microsecond, as an int from 0 through 999999.
- int
PyDateTime_TIME_GET_FOLD(PyDateTime_Time *o)¶ Return the fold, as an int from 0 through 1.
New in version 3.6.
- PyObject *
PyDateTime_TIME_GET_TZINFO(PyDateTime_Time *o)¶ Return the tzinfo (which may be
None).New 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:
- int
PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o)¶ Return the number of days, as an int from -999999999 to 999999999.
New in version 3.3.
- int
PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)¶ Return the number of seconds, as an int from 0 through 86399.
New in version 3.3.
- int
PyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta *o)¶ Return the number of microseconds, as an int from 0 through 999999.
New 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().