blob: e2d832c6a9c6c599071028de900040bfc9615d21 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _datetimeobjects:
4
5DateTime Objects
6----------------
7
8Various date and time objects are supplied by the :mod:`datetime` module.
9Before using any of these functions, the header file :file:`datetime.h` must be
10included in your source (note that this is not included by :file:`Python.h`),
Sandro Tosi98ed08f2012-01-14 16:42:02 +010011and the macro :c:macro:`PyDateTime_IMPORT` must be invoked, usually as part of
Alexander Belopolskycaf5db72010-06-26 02:05:19 +000012the module initialisation function. The macro puts a pointer to a C structure
Sandro Tosi98ed08f2012-01-14 16:42:02 +010013into a static variable, :c:data:`PyDateTimeAPI`, that is used by the following
Alexander Belopolsky21887df2010-06-26 18:49:10 +000014macros.
Georg Brandlf6842722008-01-19 22:08:21 +000015
16Type-check macros:
17
18
Sandro Tosi98ed08f2012-01-14 16:42:02 +010019.. c:function:: int PyDate_Check(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000020
Sandro Tosi98ed08f2012-01-14 16:42:02 +010021 Return true if *ob* is of type :c:data:`PyDateTime_DateType` or a subtype of
22 :c:data:`PyDateTime_DateType`. *ob* must not be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +000023
24 .. versionadded:: 2.4
25
26
Sandro Tosi98ed08f2012-01-14 16:42:02 +010027.. c:function:: int PyDate_CheckExact(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000028
Sandro Tosi98ed08f2012-01-14 16:42:02 +010029 Return true if *ob* is of type :c:data:`PyDateTime_DateType`. *ob* must not be
Georg Brandlf6842722008-01-19 22:08:21 +000030 *NULL*.
31
32 .. versionadded:: 2.4
33
34
Sandro Tosi98ed08f2012-01-14 16:42:02 +010035.. c:function:: int PyDateTime_Check(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000036
Sandro Tosi98ed08f2012-01-14 16:42:02 +010037 Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType` or a subtype of
38 :c:data:`PyDateTime_DateTimeType`. *ob* must not be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +000039
40 .. versionadded:: 2.4
41
42
Sandro Tosi98ed08f2012-01-14 16:42:02 +010043.. c:function:: int PyDateTime_CheckExact(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000044
Sandro Tosi98ed08f2012-01-14 16:42:02 +010045 Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType`. *ob* must not
Georg Brandlf6842722008-01-19 22:08:21 +000046 be *NULL*.
47
48 .. versionadded:: 2.4
49
50
Sandro Tosi98ed08f2012-01-14 16:42:02 +010051.. c:function:: int PyTime_Check(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000052
Sandro Tosi98ed08f2012-01-14 16:42:02 +010053 Return true if *ob* is of type :c:data:`PyDateTime_TimeType` or a subtype of
54 :c:data:`PyDateTime_TimeType`. *ob* must not be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +000055
56 .. versionadded:: 2.4
57
58
Sandro Tosi98ed08f2012-01-14 16:42:02 +010059.. c:function:: int PyTime_CheckExact(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000060
Sandro Tosi98ed08f2012-01-14 16:42:02 +010061 Return true if *ob* is of type :c:data:`PyDateTime_TimeType`. *ob* must not be
Georg Brandlf6842722008-01-19 22:08:21 +000062 *NULL*.
63
64 .. versionadded:: 2.4
65
66
Sandro Tosi98ed08f2012-01-14 16:42:02 +010067.. c:function:: int PyDelta_Check(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000068
Sandro Tosi98ed08f2012-01-14 16:42:02 +010069 Return true if *ob* is of type :c:data:`PyDateTime_DeltaType` or a subtype of
70 :c:data:`PyDateTime_DeltaType`. *ob* must not be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +000071
72 .. versionadded:: 2.4
73
74
Sandro Tosi98ed08f2012-01-14 16:42:02 +010075.. c:function:: int PyDelta_CheckExact(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000076
Sandro Tosi98ed08f2012-01-14 16:42:02 +010077 Return true if *ob* is of type :c:data:`PyDateTime_DeltaType`. *ob* must not be
Georg Brandlf6842722008-01-19 22:08:21 +000078 *NULL*.
79
80 .. versionadded:: 2.4
81
82
Sandro Tosi98ed08f2012-01-14 16:42:02 +010083.. c:function:: int PyTZInfo_Check(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000084
Sandro Tosi98ed08f2012-01-14 16:42:02 +010085 Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType` or a subtype of
86 :c:data:`PyDateTime_TZInfoType`. *ob* must not be *NULL*.
Georg Brandlf6842722008-01-19 22:08:21 +000087
88 .. versionadded:: 2.4
89
90
Sandro Tosi98ed08f2012-01-14 16:42:02 +010091.. c:function:: int PyTZInfo_CheckExact(PyObject *ob)
Georg Brandlf6842722008-01-19 22:08:21 +000092
Sandro Tosi98ed08f2012-01-14 16:42:02 +010093 Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType`. *ob* must not be
Georg Brandlf6842722008-01-19 22:08:21 +000094 *NULL*.
95
96 .. versionadded:: 2.4
97
98Macros to create objects:
99
100
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100101.. c:function:: PyObject* PyDate_FromDate(int year, int month, int day)
Georg Brandlf6842722008-01-19 22:08:21 +0000102
103 Return a ``datetime.date`` object with the specified year, month and day.
104
105 .. versionadded:: 2.4
106
107
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100108.. c:function:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
Georg Brandlf6842722008-01-19 22:08:21 +0000109
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
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100116.. c:function:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
Georg Brandlf6842722008-01-19 22:08:21 +0000117
118 Return a ``datetime.time`` object with the specified hour, minute, second and
119 microsecond.
120
121 .. versionadded:: 2.4
122
123
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100124.. c:function:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
Georg Brandlf6842722008-01-19 22:08:21 +0000125
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
133Macros to extract fields from date objects. The argument must be an instance of
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100134:c:data:`PyDateTime_Date`, including subclasses (such as
135:c:data:`PyDateTime_DateTime`). The argument must not be *NULL*, and the type is
Georg Brandlf6842722008-01-19 22:08:21 +0000136not checked:
137
138
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100139.. c:function:: int PyDateTime_GET_YEAR(PyDateTime_Date *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000140
141 Return the year, as a positive int.
142
143 .. versionadded:: 2.4
144
145
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100146.. c:function:: int PyDateTime_GET_MONTH(PyDateTime_Date *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000147
148 Return the month, as an int from 1 through 12.
149
150 .. versionadded:: 2.4
151
152
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100153.. c:function:: int PyDateTime_GET_DAY(PyDateTime_Date *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000154
155 Return the day, as an int from 1 through 31.
156
157 .. versionadded:: 2.4
158
159Macros to extract fields from datetime objects. The argument must be an
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100160instance of :c:data:`PyDateTime_DateTime`, including subclasses. The argument
Georg Brandlf6842722008-01-19 22:08:21 +0000161must not be *NULL*, and the type is not checked:
162
163
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100164.. c:function:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000165
166 Return the hour, as an int from 0 through 23.
167
168 .. versionadded:: 2.4
169
170
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100171.. c:function:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000172
173 Return the minute, as an int from 0 through 59.
174
175 .. versionadded:: 2.4
176
177
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100178.. c:function:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000179
180 Return the second, as an int from 0 through 59.
181
182 .. versionadded:: 2.4
183
184
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100185.. c:function:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000186
187 Return the microsecond, as an int from 0 through 999999.
188
189 .. versionadded:: 2.4
190
191Macros to extract fields from time objects. The argument must be an instance of
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100192:c:data:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*,
Georg Brandlf6842722008-01-19 22:08:21 +0000193and the type is not checked:
194
195
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100196.. c:function:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000197
198 Return the hour, as an int from 0 through 23.
199
200 .. versionadded:: 2.4
201
202
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100203.. c:function:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000204
205 Return the minute, as an int from 0 through 59.
206
207 .. versionadded:: 2.4
208
209
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100210.. c:function:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000211
212 Return the second, as an int from 0 through 59.
213
214 .. versionadded:: 2.4
215
216
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100217.. c:function:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
Georg Brandlf6842722008-01-19 22:08:21 +0000218
219 Return the microsecond, as an int from 0 through 999999.
220
221 .. versionadded:: 2.4
222
223Macros for the convenience of modules implementing the DB API:
224
225
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100226.. c:function:: PyObject* PyDateTime_FromTimestamp(PyObject *args)
Georg Brandlf6842722008-01-19 22:08:21 +0000227
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
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100234.. c:function:: PyObject* PyDate_FromTimestamp(PyObject *args)
Georg Brandlf6842722008-01-19 22:08:21 +0000235
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