| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c | 
 | 2 |  | 
 | 3 | .. _datetimeobjects: | 
 | 4 |  | 
 | 5 | DateTime Objects | 
 | 6 | ---------------- | 
 | 7 |  | 
 | 8 | Various date and time objects are supplied by the :mod:`datetime` module. | 
 | 9 | Before using any of these functions, the header file :file:`datetime.h` must be | 
 | 10 | included in your source (note that this is not included by :file:`Python.h`), | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 11 | and the macro :c:macro:`PyDateTime_IMPORT` must be invoked, usually as part of | 
| Alexander Belopolsky | 56303e0 | 2010-06-26 02:15:07 +0000 | [diff] [blame] | 12 | the module initialisation function.  The macro puts a pointer to a C structure | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 13 | into a static variable, :c:data:`PyDateTimeAPI`, that is used by the following | 
| Alexander Belopolsky | 1341f57 | 2010-06-26 18:57:02 +0000 | [diff] [blame] | 14 | macros. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 15 |  | 
 | 16 | Type-check macros: | 
 | 17 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 | .. c:function:: int PyDate_Check(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 20 |    Return true if *ob* is of type :c:data:`PyDateTime_DateType` or a subtype of | 
 | 21 |    :c:data:`PyDateTime_DateType`.  *ob* must not be *NULL*. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 |  | 
 | 23 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 24 | .. c:function:: int PyDate_CheckExact(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 25 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 26 |    Return true if *ob* is of type :c:data:`PyDateTime_DateType`. *ob* must not be | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 |    *NULL*. | 
 | 28 |  | 
 | 29 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 30 | .. c:function:: int PyDateTime_Check(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 31 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 32 |    Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType` or a subtype of | 
 | 33 |    :c:data:`PyDateTime_DateTimeType`.  *ob* must not be *NULL*. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 34 |  | 
 | 35 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 36 | .. c:function:: int PyDateTime_CheckExact(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 37 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 38 |    Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType`. *ob* must not | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 39 |    be *NULL*. | 
 | 40 |  | 
 | 41 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 42 | .. c:function:: int PyTime_Check(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 43 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 |    Return true if *ob* is of type :c:data:`PyDateTime_TimeType` or a subtype of | 
 | 45 |    :c:data:`PyDateTime_TimeType`.  *ob* must not be *NULL*. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 46 |  | 
 | 47 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 48 | .. c:function:: int PyTime_CheckExact(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 49 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 50 |    Return true if *ob* is of type :c:data:`PyDateTime_TimeType`. *ob* must not be | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 51 |    *NULL*. | 
 | 52 |  | 
 | 53 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 54 | .. c:function:: int PyDelta_Check(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 55 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 56 |    Return true if *ob* is of type :c:data:`PyDateTime_DeltaType` or a subtype of | 
 | 57 |    :c:data:`PyDateTime_DeltaType`.  *ob* must not be *NULL*. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 |  | 
 | 59 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 60 | .. c:function:: int PyDelta_CheckExact(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 61 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 62 |    Return true if *ob* is of type :c:data:`PyDateTime_DeltaType`. *ob* must not be | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 63 |    *NULL*. | 
 | 64 |  | 
 | 65 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 66 | .. c:function:: int PyTZInfo_Check(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 67 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 68 |    Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType` or a subtype of | 
 | 69 |    :c:data:`PyDateTime_TZInfoType`.  *ob* must not be *NULL*. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 70 |  | 
 | 71 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 72 | .. c:function:: int PyTZInfo_CheckExact(PyObject *ob) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 73 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 74 |    Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType`. *ob* must not be | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 |    *NULL*. | 
 | 76 |  | 
 | 77 |  | 
 | 78 | Macros to create objects: | 
 | 79 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | .. c:function:: PyObject* PyDate_FromDate(int year, int month, int day) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 81 |  | 
 | 82 |    Return a ``datetime.date`` object with the specified year, month and day. | 
 | 83 |  | 
 | 84 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 85 | .. c:function:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 |  | 
 | 87 |    Return a ``datetime.datetime`` object with the specified year, month, day, hour, | 
 | 88 |    minute, second and microsecond. | 
 | 89 |  | 
 | 90 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 91 | .. c:function:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 92 |  | 
 | 93 |    Return a ``datetime.time`` object with the specified hour, minute, second and | 
 | 94 |    microsecond. | 
 | 95 |  | 
 | 96 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 97 | .. c:function:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 98 |  | 
 | 99 |    Return a ``datetime.timedelta`` object representing the given number of days, | 
 | 100 |    seconds and microseconds.  Normalization is performed so that the resulting | 
 | 101 |    number of microseconds and seconds lie in the ranges documented for | 
 | 102 |    ``datetime.timedelta`` objects. | 
 | 103 |  | 
 | 104 |  | 
 | 105 | Macros to extract fields from date objects.  The argument must be an instance of | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 106 | :c:data:`PyDateTime_Date`, including subclasses (such as | 
 | 107 | :c:data:`PyDateTime_DateTime`).  The argument must not be *NULL*, and the type is | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 108 | not checked: | 
 | 109 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 110 | .. c:function:: int PyDateTime_GET_YEAR(PyDateTime_Date *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 111 |  | 
 | 112 |    Return the year, as a positive int. | 
 | 113 |  | 
 | 114 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 115 | .. c:function:: int PyDateTime_GET_MONTH(PyDateTime_Date *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 116 |  | 
 | 117 |    Return the month, as an int from 1 through 12. | 
 | 118 |  | 
 | 119 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 120 | .. c:function:: int PyDateTime_GET_DAY(PyDateTime_Date *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 121 |  | 
 | 122 |    Return the day, as an int from 1 through 31. | 
 | 123 |  | 
 | 124 |  | 
 | 125 | Macros to extract fields from datetime objects.  The argument must be an | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 126 | instance of :c:data:`PyDateTime_DateTime`, including subclasses. The argument | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 127 | must not be *NULL*, and the type is not checked: | 
 | 128 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 129 | .. c:function:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 130 |  | 
 | 131 |    Return the hour, as an int from 0 through 23. | 
 | 132 |  | 
 | 133 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 134 | .. c:function:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 135 |  | 
 | 136 |    Return the minute, as an int from 0 through 59. | 
 | 137 |  | 
 | 138 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 139 | .. c:function:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 140 |  | 
 | 141 |    Return the second, as an int from 0 through 59. | 
 | 142 |  | 
 | 143 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 144 | .. c:function:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 145 |  | 
 | 146 |    Return the microsecond, as an int from 0 through 999999. | 
 | 147 |  | 
 | 148 |  | 
 | 149 | Macros to extract fields from time objects.  The argument must be an instance of | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 150 | :c:data:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*, | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 151 | and the type is not checked: | 
 | 152 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 153 | .. c:function:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 154 |  | 
 | 155 |    Return the hour, as an int from 0 through 23. | 
 | 156 |  | 
 | 157 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 158 | .. c:function:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 159 |  | 
 | 160 |    Return the minute, as an int from 0 through 59. | 
 | 161 |  | 
 | 162 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 163 | .. c:function:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 164 |  | 
 | 165 |    Return the second, as an int from 0 through 59. | 
 | 166 |  | 
 | 167 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 168 | .. c:function:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 169 |  | 
 | 170 |    Return the microsecond, as an int from 0 through 999999. | 
 | 171 |  | 
 | 172 |  | 
| Amaury Forgeot d'Arc | 5e8260b | 2012-01-17 21:31:50 +0100 | [diff] [blame] | 173 | Macros to extract fields from time delta objects.  The argument must be an | 
 | 174 | instance of :c:data:`PyDateTime_Delta`, including subclasses. The argument must | 
 | 175 | not be *NULL*, and the type is not checked: | 
 | 176 |  | 
 | 177 | .. c:function:: int PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o) | 
 | 178 |  | 
 | 179 |    Return the number of days, as an int from -999999999 to 999999999. | 
 | 180 |  | 
 | 181 |    .. versionadded:: 3.3 | 
 | 182 |  | 
 | 183 |  | 
 | 184 | .. c:function:: int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o) | 
 | 185 |  | 
 | 186 |    Return the number of seconds, as an int from 0 through 86399. | 
 | 187 |  | 
 | 188 |    .. versionadded:: 3.3 | 
 | 189 |  | 
 | 190 |  | 
 | 191 | .. c:function:: int PyDateTime_DELTA_GET_MICROSECOND(PyDateTime_Delta *o) | 
 | 192 |  | 
 | 193 |    Return the number of microseconds, as an int from 0 through 999999. | 
 | 194 |  | 
 | 195 |    .. versionadded:: 3.3 | 
 | 196 |  | 
 | 197 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 198 | Macros for the convenience of modules implementing the DB API: | 
 | 199 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 200 | .. c:function:: PyObject* PyDateTime_FromTimestamp(PyObject *args) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 201 |  | 
 | 202 |    Create and return a new ``datetime.datetime`` object given an argument tuple | 
 | 203 |    suitable for passing to ``datetime.datetime.fromtimestamp()``. | 
 | 204 |  | 
 | 205 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 206 | .. c:function:: PyObject* PyDate_FromTimestamp(PyObject *args) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 207 |  | 
 | 208 |    Create and return a new ``datetime.date`` object given an argument tuple | 
 | 209 |    suitable for passing to ``datetime.date.fromtimestamp()``. |