| Georg Brandl | f684272 | 2008-01-19 22:08:21 +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`), | 
| Alexander Belopolsky | caf5db7 | 2010-06-26 02:05:19 +0000 | [diff] [blame] | 11 | and the macro :cmacro:`PyDateTime_IMPORT` must be invoked, usually as part of | 
|  | 12 | the module initialisation function.  The macro puts a pointer to a C structure | 
| Alexander Belopolsky | 21887df | 2010-06-26 18:49:10 +0000 | [diff] [blame] | 13 | into a static variable, :cdata:`PyDateTimeAPI`, that is used by the following | 
|  | 14 | macros. | 
| Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 15 |  | 
|  | 16 | Type-check macros: | 
|  | 17 |  | 
|  | 18 |  | 
|  | 19 | .. cfunction:: int PyDate_Check(PyObject *ob) | 
|  | 20 |  | 
|  | 21 | Return true if *ob* is of type :cdata:`PyDateTime_DateType` or a subtype of | 
|  | 22 | :cdata:`PyDateTime_DateType`.  *ob* must not be *NULL*. | 
|  | 23 |  | 
|  | 24 | .. versionadded:: 2.4 | 
|  | 25 |  | 
|  | 26 |  | 
|  | 27 | .. cfunction:: int PyDate_CheckExact(PyObject *ob) | 
|  | 28 |  | 
|  | 29 | Return true if *ob* is of type :cdata:`PyDateTime_DateType`. *ob* must not be | 
|  | 30 | *NULL*. | 
|  | 31 |  | 
|  | 32 | .. versionadded:: 2.4 | 
|  | 33 |  | 
|  | 34 |  | 
|  | 35 | .. cfunction:: int PyDateTime_Check(PyObject *ob) | 
|  | 36 |  | 
|  | 37 | Return true if *ob* is of type :cdata:`PyDateTime_DateTimeType` or a subtype of | 
|  | 38 | :cdata:`PyDateTime_DateTimeType`.  *ob* must not be *NULL*. | 
|  | 39 |  | 
|  | 40 | .. versionadded:: 2.4 | 
|  | 41 |  | 
|  | 42 |  | 
|  | 43 | .. cfunction:: int PyDateTime_CheckExact(PyObject *ob) | 
|  | 44 |  | 
|  | 45 | Return true if *ob* is of type :cdata:`PyDateTime_DateTimeType`. *ob* must not | 
|  | 46 | be *NULL*. | 
|  | 47 |  | 
|  | 48 | .. versionadded:: 2.4 | 
|  | 49 |  | 
|  | 50 |  | 
|  | 51 | .. cfunction:: int PyTime_Check(PyObject *ob) | 
|  | 52 |  | 
|  | 53 | Return true if *ob* is of type :cdata:`PyDateTime_TimeType` or a subtype of | 
|  | 54 | :cdata:`PyDateTime_TimeType`.  *ob* must not be *NULL*. | 
|  | 55 |  | 
|  | 56 | .. versionadded:: 2.4 | 
|  | 57 |  | 
|  | 58 |  | 
|  | 59 | .. cfunction:: int PyTime_CheckExact(PyObject *ob) | 
|  | 60 |  | 
|  | 61 | Return true if *ob* is of type :cdata:`PyDateTime_TimeType`. *ob* must not be | 
|  | 62 | *NULL*. | 
|  | 63 |  | 
|  | 64 | .. versionadded:: 2.4 | 
|  | 65 |  | 
|  | 66 |  | 
|  | 67 | .. cfunction:: int PyDelta_Check(PyObject *ob) | 
|  | 68 |  | 
|  | 69 | Return true if *ob* is of type :cdata:`PyDateTime_DeltaType` or a subtype of | 
|  | 70 | :cdata:`PyDateTime_DeltaType`.  *ob* must not be *NULL*. | 
|  | 71 |  | 
|  | 72 | .. versionadded:: 2.4 | 
|  | 73 |  | 
|  | 74 |  | 
|  | 75 | .. cfunction:: int PyDelta_CheckExact(PyObject *ob) | 
|  | 76 |  | 
|  | 77 | Return true if *ob* is of type :cdata:`PyDateTime_DeltaType`. *ob* must not be | 
|  | 78 | *NULL*. | 
|  | 79 |  | 
|  | 80 | .. versionadded:: 2.4 | 
|  | 81 |  | 
|  | 82 |  | 
|  | 83 | .. cfunction:: int PyTZInfo_Check(PyObject *ob) | 
|  | 84 |  | 
|  | 85 | Return true if *ob* is of type :cdata:`PyDateTime_TZInfoType` or a subtype of | 
|  | 86 | :cdata:`PyDateTime_TZInfoType`.  *ob* must not be *NULL*. | 
|  | 87 |  | 
|  | 88 | .. versionadded:: 2.4 | 
|  | 89 |  | 
|  | 90 |  | 
|  | 91 | .. cfunction:: int PyTZInfo_CheckExact(PyObject *ob) | 
|  | 92 |  | 
|  | 93 | Return true if *ob* is of type :cdata:`PyDateTime_TZInfoType`. *ob* must not be | 
|  | 94 | *NULL*. | 
|  | 95 |  | 
|  | 96 | .. versionadded:: 2.4 | 
|  | 97 |  | 
|  | 98 | Macros to create objects: | 
|  | 99 |  | 
|  | 100 |  | 
|  | 101 | .. cfunction:: PyObject* PyDate_FromDate(int year, int month, int day) | 
|  | 102 |  | 
|  | 103 | Return a ``datetime.date`` object with the specified year, month and day. | 
|  | 104 |  | 
|  | 105 | .. versionadded:: 2.4 | 
|  | 106 |  | 
|  | 107 |  | 
|  | 108 | .. cfunction:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond) | 
|  | 109 |  | 
|  | 110 | Return a ``datetime.datetime`` object with the specified year, month, day, hour, | 
|  | 111 | minute, second and microsecond. | 
|  | 112 |  | 
|  | 113 | .. versionadded:: 2.4 | 
|  | 114 |  | 
|  | 115 |  | 
|  | 116 | .. cfunction:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond) | 
|  | 117 |  | 
|  | 118 | Return a ``datetime.time`` object with the specified hour, minute, second and | 
|  | 119 | microsecond. | 
|  | 120 |  | 
|  | 121 | .. versionadded:: 2.4 | 
|  | 122 |  | 
|  | 123 |  | 
|  | 124 | .. cfunction:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds) | 
|  | 125 |  | 
|  | 126 | Return a ``datetime.timedelta`` object representing the given number of days, | 
|  | 127 | seconds and microseconds.  Normalization is performed so that the resulting | 
|  | 128 | number of microseconds and seconds lie in the ranges documented for | 
|  | 129 | ``datetime.timedelta`` objects. | 
|  | 130 |  | 
|  | 131 | .. versionadded:: 2.4 | 
|  | 132 |  | 
|  | 133 | Macros to extract fields from date objects.  The argument must be an instance of | 
|  | 134 | :cdata:`PyDateTime_Date`, including subclasses (such as | 
|  | 135 | :cdata:`PyDateTime_DateTime`).  The argument must not be *NULL*, and the type is | 
|  | 136 | not checked: | 
|  | 137 |  | 
|  | 138 |  | 
|  | 139 | .. cfunction:: int PyDateTime_GET_YEAR(PyDateTime_Date *o) | 
|  | 140 |  | 
|  | 141 | Return the year, as a positive int. | 
|  | 142 |  | 
|  | 143 | .. versionadded:: 2.4 | 
|  | 144 |  | 
|  | 145 |  | 
|  | 146 | .. cfunction:: int PyDateTime_GET_MONTH(PyDateTime_Date *o) | 
|  | 147 |  | 
|  | 148 | Return the month, as an int from 1 through 12. | 
|  | 149 |  | 
|  | 150 | .. versionadded:: 2.4 | 
|  | 151 |  | 
|  | 152 |  | 
|  | 153 | .. cfunction:: int PyDateTime_GET_DAY(PyDateTime_Date *o) | 
|  | 154 |  | 
|  | 155 | Return the day, as an int from 1 through 31. | 
|  | 156 |  | 
|  | 157 | .. versionadded:: 2.4 | 
|  | 158 |  | 
|  | 159 | Macros to extract fields from datetime objects.  The argument must be an | 
|  | 160 | instance of :cdata:`PyDateTime_DateTime`, including subclasses. The argument | 
|  | 161 | must not be *NULL*, and the type is not checked: | 
|  | 162 |  | 
|  | 163 |  | 
|  | 164 | .. cfunction:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o) | 
|  | 165 |  | 
|  | 166 | Return the hour, as an int from 0 through 23. | 
|  | 167 |  | 
|  | 168 | .. versionadded:: 2.4 | 
|  | 169 |  | 
|  | 170 |  | 
|  | 171 | .. cfunction:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o) | 
|  | 172 |  | 
|  | 173 | Return the minute, as an int from 0 through 59. | 
|  | 174 |  | 
|  | 175 | .. versionadded:: 2.4 | 
|  | 176 |  | 
|  | 177 |  | 
|  | 178 | .. cfunction:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o) | 
|  | 179 |  | 
|  | 180 | Return the second, as an int from 0 through 59. | 
|  | 181 |  | 
|  | 182 | .. versionadded:: 2.4 | 
|  | 183 |  | 
|  | 184 |  | 
|  | 185 | .. cfunction:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o) | 
|  | 186 |  | 
|  | 187 | Return the microsecond, as an int from 0 through 999999. | 
|  | 188 |  | 
|  | 189 | .. versionadded:: 2.4 | 
|  | 190 |  | 
|  | 191 | Macros to extract fields from time objects.  The argument must be an instance of | 
|  | 192 | :cdata:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*, | 
|  | 193 | and the type is not checked: | 
|  | 194 |  | 
|  | 195 |  | 
|  | 196 | .. cfunction:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o) | 
|  | 197 |  | 
|  | 198 | Return the hour, as an int from 0 through 23. | 
|  | 199 |  | 
|  | 200 | .. versionadded:: 2.4 | 
|  | 201 |  | 
|  | 202 |  | 
|  | 203 | .. cfunction:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o) | 
|  | 204 |  | 
|  | 205 | Return the minute, as an int from 0 through 59. | 
|  | 206 |  | 
|  | 207 | .. versionadded:: 2.4 | 
|  | 208 |  | 
|  | 209 |  | 
|  | 210 | .. cfunction:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o) | 
|  | 211 |  | 
|  | 212 | Return the second, as an int from 0 through 59. | 
|  | 213 |  | 
|  | 214 | .. versionadded:: 2.4 | 
|  | 215 |  | 
|  | 216 |  | 
|  | 217 | .. cfunction:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o) | 
|  | 218 |  | 
|  | 219 | Return the microsecond, as an int from 0 through 999999. | 
|  | 220 |  | 
|  | 221 | .. versionadded:: 2.4 | 
|  | 222 |  | 
|  | 223 | Macros for the convenience of modules implementing the DB API: | 
|  | 224 |  | 
|  | 225 |  | 
|  | 226 | .. cfunction:: PyObject* PyDateTime_FromTimestamp(PyObject *args) | 
|  | 227 |  | 
|  | 228 | Create and return a new ``datetime.datetime`` object given an argument tuple | 
|  | 229 | suitable for passing to ``datetime.datetime.fromtimestamp()``. | 
|  | 230 |  | 
|  | 231 | .. versionadded:: 2.4 | 
|  | 232 |  | 
|  | 233 |  | 
|  | 234 | .. cfunction:: PyObject* PyDate_FromTimestamp(PyObject *args) | 
|  | 235 |  | 
|  | 236 | Create and return a new ``datetime.date`` object given an argument tuple | 
|  | 237 | suitable for passing to ``datetime.date.fromtimestamp()``. | 
|  | 238 |  | 
|  | 239 | .. versionadded:: 2.4 |