blob: 4c4e4bcfa63335ae217b8a6a908f0af6667b571e [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
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`),
Georg Brandl60203b42010-10-06 10:11:56 +000011and the macro :c:macro:`PyDateTime_IMPORT` must be invoked, usually as part of
Alexander Belopolsky56303e02010-06-26 02:15:07 +000012the module initialisation function. The macro puts a pointer to a C structure
Georg Brandl60203b42010-10-06 10:11:56 +000013into a static variable, :c:data:`PyDateTimeAPI`, that is used by the following
Alexander Belopolsky1341f572010-06-26 18:57:02 +000014macros.
Georg Brandl54a3faa2008-01-20 09:30:57 +000015
Paul Ganssle04af5b12018-01-24 17:29:30 -050016Macro for access to the UTC singleton:
17
18.. c:var:: PyObject* PyDateTime_TimeZone_UTC
19
20 Returns the time zone singleton representing UTC, the same object as
21 :attr:`datetime.timezone.utc`.
22
23 .. versionadded:: 3.7
24
25
Georg Brandl54a3faa2008-01-20 09:30:57 +000026Type-check macros:
27
Georg Brandl60203b42010-10-06 10:11:56 +000028.. c:function:: int PyDate_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000029
Georg Brandl60203b42010-10-06 10:11:56 +000030 Return true if *ob* is of type :c:data:`PyDateTime_DateType` or a subtype of
Antonio Cuni315fc522021-01-06 12:38:26 +010031 :c:data:`PyDateTime_DateType`. *ob* must not be ``NULL``. This function always
32 succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000033
34
Georg Brandl60203b42010-10-06 10:11:56 +000035.. c:function:: int PyDate_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000036
Georg Brandl60203b42010-10-06 10:11:56 +000037 Return true if *ob* is of type :c:data:`PyDateTime_DateType`. *ob* must not be
Antonio Cuni315fc522021-01-06 12:38:26 +010038 ``NULL``. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000039
40
Georg Brandl60203b42010-10-06 10:11:56 +000041.. c:function:: int PyDateTime_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000042
Georg Brandl60203b42010-10-06 10:11:56 +000043 Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType` or a subtype of
Antonio Cuni315fc522021-01-06 12:38:26 +010044 :c:data:`PyDateTime_DateTimeType`. *ob* must not be ``NULL``. This function always
45 succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000046
47
Georg Brandl60203b42010-10-06 10:11:56 +000048.. c:function:: int PyDateTime_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
Georg Brandl60203b42010-10-06 10:11:56 +000050 Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType`. *ob* must not
Antonio Cuni315fc522021-01-06 12:38:26 +010051 be ``NULL``. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: int PyTime_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Georg Brandl60203b42010-10-06 10:11:56 +000056 Return true if *ob* is of type :c:data:`PyDateTime_TimeType` or a subtype of
Antonio Cuni315fc522021-01-06 12:38:26 +010057 :c:data:`PyDateTime_TimeType`. *ob* must not be ``NULL``. This function always
58 succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000059
60
Georg Brandl60203b42010-10-06 10:11:56 +000061.. c:function:: int PyTime_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000062
Georg Brandl60203b42010-10-06 10:11:56 +000063 Return true if *ob* is of type :c:data:`PyDateTime_TimeType`. *ob* must not be
Antonio Cuni315fc522021-01-06 12:38:26 +010064 ``NULL``. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
66
Georg Brandl60203b42010-10-06 10:11:56 +000067.. c:function:: int PyDelta_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
Georg Brandl60203b42010-10-06 10:11:56 +000069 Return true if *ob* is of type :c:data:`PyDateTime_DeltaType` or a subtype of
Antonio Cuni315fc522021-01-06 12:38:26 +010070 :c:data:`PyDateTime_DeltaType`. *ob* must not be ``NULL``. This function always
71 succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
73
Georg Brandl60203b42010-10-06 10:11:56 +000074.. c:function:: int PyDelta_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000075
Georg Brandl60203b42010-10-06 10:11:56 +000076 Return true if *ob* is of type :c:data:`PyDateTime_DeltaType`. *ob* must not be
Antonio Cuni315fc522021-01-06 12:38:26 +010077 ``NULL``. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: int PyTZInfo_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
Georg Brandl60203b42010-10-06 10:11:56 +000082 Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType` or a subtype of
Antonio Cuni315fc522021-01-06 12:38:26 +010083 :c:data:`PyDateTime_TZInfoType`. *ob* must not be ``NULL``. This function always
84 succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
86
Georg Brandl60203b42010-10-06 10:11:56 +000087.. c:function:: int PyTZInfo_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000088
Georg Brandl60203b42010-10-06 10:11:56 +000089 Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType`. *ob* must not be
Antonio Cuni315fc522021-01-06 12:38:26 +010090 ``NULL``. This function always succeeds.
Georg Brandl54a3faa2008-01-20 09:30:57 +000091
92
93Macros to create objects:
94
Georg Brandl60203b42010-10-06 10:11:56 +000095.. c:function:: PyObject* PyDate_FromDate(int year, int month, int day)
Georg Brandl54a3faa2008-01-20 09:30:57 +000096
Paul Ganssle04af5b12018-01-24 17:29:30 -050097 Return a :class:`datetime.date` object with the specified year, month and day.
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
99
Georg Brandl60203b42010-10-06 10:11:56 +0000100.. c:function:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101
Paul Ganssle04af5b12018-01-24 17:29:30 -0500102 Return a :class:`datetime.datetime` object with the specified year, month, day, hour,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000103 minute, second and microsecond.
104
105
Edison A5765ecf2019-05-07 08:00:21 -0700106.. c:function:: PyObject* PyDateTime_FromDateAndTimeAndFold(int year, int month, int day, int hour, int minute, int second, int usecond, int fold)
107
108 Return a :class:`datetime.datetime` object with the specified year, month, day, hour,
109 minute, second, microsecond and fold.
110
111 .. versionadded:: 3.6
112
113
Edison Ad28772a2019-05-13 00:23:38 -0700114.. c:function:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
115
116 Return a :class:`datetime.time` object with the specified hour, minute, second and
117 microsecond.
118
119
Edison A5765ecf2019-05-07 08:00:21 -0700120.. c:function:: PyObject* PyTime_FromTimeAndFold(int hour, int minute, int second, int usecond, int fold)
121
122 Return a :class:`datetime.time` object with the specified hour, minute, second,
123 microsecond and fold.
124
125 .. versionadded:: 3.6
126
127
Georg Brandl60203b42010-10-06 10:11:56 +0000128.. c:function:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
Paul Ganssle04af5b12018-01-24 17:29:30 -0500130 Return a :class:`datetime.timedelta` object representing the given number
131 of days, seconds and microseconds. Normalization is performed so that the
132 resulting number of microseconds and seconds lie in the ranges documented for
133 :class:`datetime.timedelta` objects.
134
135.. c:function:: PyObject* PyTimeZone_FromOffset(PyDateTime_DeltaType* offset)
136
137 Return a :class:`datetime.timezone` object with an unnamed fixed offset
138 represented by the *offset* argument.
139
140 .. versionadded:: 3.7
141
142.. c:function:: PyObject* PyTimeZone_FromOffsetAndName(PyDateTime_DeltaType* offset, PyUnicode* name)
143
144 Return a :class:`datetime.timezone` object with a fixed offset represented
145 by the *offset* argument and with tzname *name*.
146
147 .. versionadded:: 3.7
Georg Brandl54a3faa2008-01-20 09:30:57 +0000148
149
150Macros to extract fields from date objects. The argument must be an instance of
Georg Brandl60203b42010-10-06 10:11:56 +0000151:c:data:`PyDateTime_Date`, including subclasses (such as
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200152:c:data:`PyDateTime_DateTime`). The argument must not be ``NULL``, and the type is
Georg Brandl54a3faa2008-01-20 09:30:57 +0000153not checked:
154
Georg Brandl60203b42010-10-06 10:11:56 +0000155.. c:function:: int PyDateTime_GET_YEAR(PyDateTime_Date *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000156
157 Return the year, as a positive int.
158
159
Georg Brandl60203b42010-10-06 10:11:56 +0000160.. c:function:: int PyDateTime_GET_MONTH(PyDateTime_Date *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000161
162 Return the month, as an int from 1 through 12.
163
164
Georg Brandl60203b42010-10-06 10:11:56 +0000165.. c:function:: int PyDateTime_GET_DAY(PyDateTime_Date *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000166
167 Return the day, as an int from 1 through 31.
168
169
170Macros to extract fields from datetime objects. The argument must be an
Georg Brandl60203b42010-10-06 10:11:56 +0000171instance of :c:data:`PyDateTime_DateTime`, including subclasses. The argument
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200172must not be ``NULL``, and the type is not checked:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000173
Georg Brandl60203b42010-10-06 10:11:56 +0000174.. c:function:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000175
176 Return the hour, as an int from 0 through 23.
177
178
Georg Brandl60203b42010-10-06 10:11:56 +0000179.. c:function:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000180
181 Return the minute, as an int from 0 through 59.
182
183
Georg Brandl60203b42010-10-06 10:11:56 +0000184.. c:function:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000185
186 Return the second, as an int from 0 through 59.
187
188
Georg Brandl60203b42010-10-06 10:11:56 +0000189.. c:function:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000190
191 Return the microsecond, as an int from 0 through 999999.
192
Zackery Spytz2e4dd332020-09-23 12:43:45 -0600193.. c:function:: PyObject* PyDateTime_DATE_GET_TZINFO(PyDateTime_DateTime *o)
194
195 Return the tzinfo (which may be ``None``).
196
197 .. versionadded:: 3.10
Georg Brandl54a3faa2008-01-20 09:30:57 +0000198
199Macros to extract fields from time objects. The argument must be an instance of
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200200:c:data:`PyDateTime_Time`, including subclasses. The argument must not be ``NULL``,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000201and the type is not checked:
202
Georg Brandl60203b42010-10-06 10:11:56 +0000203.. c:function:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000204
205 Return the hour, as an int from 0 through 23.
206
207
Georg Brandl60203b42010-10-06 10:11:56 +0000208.. c:function:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000209
210 Return the minute, as an int from 0 through 59.
211
212
Georg Brandl60203b42010-10-06 10:11:56 +0000213.. c:function:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000214
215 Return the second, as an int from 0 through 59.
216
217
Georg Brandl60203b42010-10-06 10:11:56 +0000218.. c:function:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000219
220 Return the microsecond, as an int from 0 through 999999.
221
Zackery Spytz2e4dd332020-09-23 12:43:45 -0600222.. c:function:: PyObject* PyDateTime_TIME_GET_TZINFO(PyDateTime_Time *o)
223
224 Return the tzinfo (which may be ``None``).
225
226 .. versionadded:: 3.10
227
Georg Brandl54a3faa2008-01-20 09:30:57 +0000228
Amaury Forgeot d'Arc5e8260b2012-01-17 21:31:50 +0100229Macros to extract fields from time delta objects. The argument must be an
230instance of :c:data:`PyDateTime_Delta`, including subclasses. The argument must
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200231not be ``NULL``, and the type is not checked:
Amaury Forgeot d'Arc5e8260b2012-01-17 21:31:50 +0100232
233.. c:function:: int PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o)
234
235 Return the number of days, as an int from -999999999 to 999999999.
236
237 .. versionadded:: 3.3
238
239
240.. c:function:: int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)
241
242 Return the number of seconds, as an int from 0 through 86399.
243
244 .. versionadded:: 3.3
245
246
Phobosmir82cd3ce2017-11-04 13:39:45 +0300247.. c:function:: int PyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta *o)
Amaury Forgeot d'Arc5e8260b2012-01-17 21:31:50 +0100248
249 Return the number of microseconds, as an int from 0 through 999999.
250
251 .. versionadded:: 3.3
252
253
Georg Brandl54a3faa2008-01-20 09:30:57 +0000254Macros for the convenience of modules implementing the DB API:
255
Georg Brandl60203b42010-10-06 10:11:56 +0000256.. c:function:: PyObject* PyDateTime_FromTimestamp(PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000257
Paul Ganssle04af5b12018-01-24 17:29:30 -0500258 Create and return a new :class:`datetime.datetime` object given an argument
259 tuple suitable for passing to :meth:`datetime.datetime.fromtimestamp()`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000260
261
Georg Brandl60203b42010-10-06 10:11:56 +0000262.. c:function:: PyObject* PyDate_FromTimestamp(PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000263
Paul Ganssle04af5b12018-01-24 17:29:30 -0500264 Create and return a new :class:`datetime.date` object given an argument
265 tuple suitable for passing to :meth:`datetime.date.fromtimestamp()`.