blob: fcd13954a335a6025dfb470690c8b76e1af8a3d0 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +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`),
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
16Type-check macros:
17
Georg Brandl60203b42010-10-06 10:11:56 +000018.. c:function:: int PyDate_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
Georg Brandl60203b42010-10-06 10:11:56 +000020 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 Brandl54a3faa2008-01-20 09:30:57 +000022
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: int PyDate_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
Georg Brandl60203b42010-10-06 10:11:56 +000026 Return true if *ob* is of type :c:data:`PyDateTime_DateType`. *ob* must not be
Georg Brandl54a3faa2008-01-20 09:30:57 +000027 *NULL*.
28
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: int PyDateTime_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
Georg Brandl60203b42010-10-06 10:11:56 +000032 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 Brandl54a3faa2008-01-20 09:30:57 +000034
35
Georg Brandl60203b42010-10-06 10:11:56 +000036.. c:function:: int PyDateTime_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
Georg Brandl60203b42010-10-06 10:11:56 +000038 Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType`. *ob* must not
Georg Brandl54a3faa2008-01-20 09:30:57 +000039 be *NULL*.
40
41
Georg Brandl60203b42010-10-06 10:11:56 +000042.. c:function:: int PyTime_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000043
Georg Brandl60203b42010-10-06 10:11:56 +000044 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 Brandl54a3faa2008-01-20 09:30:57 +000046
47
Georg Brandl60203b42010-10-06 10:11:56 +000048.. c:function:: int PyTime_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_TimeType`. *ob* must not be
Georg Brandl54a3faa2008-01-20 09:30:57 +000051 *NULL*.
52
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: int PyDelta_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_DeltaType` or a subtype of
57 :c:data:`PyDateTime_DeltaType`. *ob* must not be *NULL*.
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59
Georg Brandl60203b42010-10-06 10:11:56 +000060.. c:function:: int PyDelta_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
Georg Brandl60203b42010-10-06 10:11:56 +000062 Return true if *ob* is of type :c:data:`PyDateTime_DeltaType`. *ob* must not be
Georg Brandl54a3faa2008-01-20 09:30:57 +000063 *NULL*.
64
65
Georg Brandl60203b42010-10-06 10:11:56 +000066.. c:function:: int PyTZInfo_Check(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
Georg Brandl60203b42010-10-06 10:11:56 +000068 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 Brandl54a3faa2008-01-20 09:30:57 +000070
71
Georg Brandl60203b42010-10-06 10:11:56 +000072.. c:function:: int PyTZInfo_CheckExact(PyObject *ob)
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Georg Brandl60203b42010-10-06 10:11:56 +000074 Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType`. *ob* must not be
Georg Brandl54a3faa2008-01-20 09:30:57 +000075 *NULL*.
76
77
78Macros to create objects:
79
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: PyObject* PyDate_FromDate(int year, int month, int day)
Georg Brandl54a3faa2008-01-20 09:30:57 +000081
82 Return a ``datetime.date`` object with the specified year, month and day.
83
84
Georg Brandl60203b42010-10-06 10:11:56 +000085.. 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 +000086
87 Return a ``datetime.datetime`` object with the specified year, month, day, hour,
88 minute, second and microsecond.
89
90
Georg Brandl60203b42010-10-06 10:11:56 +000091.. c:function:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
93 Return a ``datetime.time`` object with the specified hour, minute, second and
94 microsecond.
95
96
Georg Brandl60203b42010-10-06 10:11:56 +000097.. c:function:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
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
105Macros to extract fields from date objects. The argument must be an instance of
Georg Brandl60203b42010-10-06 10:11:56 +0000106:c:data:`PyDateTime_Date`, including subclasses (such as
107:c:data:`PyDateTime_DateTime`). The argument must not be *NULL*, and the type is
Georg Brandl54a3faa2008-01-20 09:30:57 +0000108not checked:
109
Georg Brandl60203b42010-10-06 10:11:56 +0000110.. c:function:: int PyDateTime_GET_YEAR(PyDateTime_Date *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000111
112 Return the year, as a positive int.
113
114
Georg Brandl60203b42010-10-06 10:11:56 +0000115.. c:function:: int PyDateTime_GET_MONTH(PyDateTime_Date *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000116
117 Return the month, as an int from 1 through 12.
118
119
Georg Brandl60203b42010-10-06 10:11:56 +0000120.. c:function:: int PyDateTime_GET_DAY(PyDateTime_Date *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000121
122 Return the day, as an int from 1 through 31.
123
124
125Macros to extract fields from datetime objects. The argument must be an
Georg Brandl60203b42010-10-06 10:11:56 +0000126instance of :c:data:`PyDateTime_DateTime`, including subclasses. The argument
Georg Brandl54a3faa2008-01-20 09:30:57 +0000127must not be *NULL*, and the type is not checked:
128
Georg Brandl60203b42010-10-06 10:11:56 +0000129.. c:function:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000130
131 Return the hour, as an int from 0 through 23.
132
133
Georg Brandl60203b42010-10-06 10:11:56 +0000134.. c:function:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000135
136 Return the minute, as an int from 0 through 59.
137
138
Georg Brandl60203b42010-10-06 10:11:56 +0000139.. c:function:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140
141 Return the second, as an int from 0 through 59.
142
143
Georg Brandl60203b42010-10-06 10:11:56 +0000144.. c:function:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000145
146 Return the microsecond, as an int from 0 through 999999.
147
148
149Macros to extract fields from time objects. The argument must be an instance of
Georg Brandl60203b42010-10-06 10:11:56 +0000150:c:data:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000151and the type is not checked:
152
Georg Brandl60203b42010-10-06 10:11:56 +0000153.. c:function:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000154
155 Return the hour, as an int from 0 through 23.
156
157
Georg Brandl60203b42010-10-06 10:11:56 +0000158.. c:function:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000159
160 Return the minute, as an int from 0 through 59.
161
162
Georg Brandl60203b42010-10-06 10:11:56 +0000163.. c:function:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000164
165 Return the second, as an int from 0 through 59.
166
167
Georg Brandl60203b42010-10-06 10:11:56 +0000168.. c:function:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000169
170 Return the microsecond, as an int from 0 through 999999.
171
172
173Macros for the convenience of modules implementing the DB API:
174
Georg Brandl60203b42010-10-06 10:11:56 +0000175.. c:function:: PyObject* PyDateTime_FromTimestamp(PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000176
177 Create and return a new ``datetime.datetime`` object given an argument tuple
178 suitable for passing to ``datetime.datetime.fromtimestamp()``.
179
180
Georg Brandl60203b42010-10-06 10:11:56 +0000181.. c:function:: PyObject* PyDate_FromTimestamp(PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000182
183 Create and return a new ``datetime.date`` object given an argument tuple
184 suitable for passing to ``datetime.date.fromtimestamp()``.