blob: 6515babbde03ff9e3fee23410a20bdf088e0a8a1 [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`),
Alexander Belopolsky29605712010-06-26 02:16:57 +000011and the macro :cmacro:`PyDateTime_IMPORT` must be invoked, usually as part of
12the module initialisation function. The macro puts a pointer to a C structure
Alexander Belopolsky46980d82010-06-26 18:58:29 +000013into a static variable, :cdata:`PyDateTimeAPI`, that is used by the following
14macros.
Georg Brandl54a3faa2008-01-20 09:30:57 +000015
16Type-check macros:
17
18.. cfunction:: int PyDate_Check(PyObject *ob)
19
20 Return true if *ob* is of type :cdata:`PyDateTime_DateType` or a subtype of
21 :cdata:`PyDateTime_DateType`. *ob* must not be *NULL*.
22
23
24.. cfunction:: int PyDate_CheckExact(PyObject *ob)
25
26 Return true if *ob* is of type :cdata:`PyDateTime_DateType`. *ob* must not be
27 *NULL*.
28
29
30.. cfunction:: int PyDateTime_Check(PyObject *ob)
31
32 Return true if *ob* is of type :cdata:`PyDateTime_DateTimeType` or a subtype of
33 :cdata:`PyDateTime_DateTimeType`. *ob* must not be *NULL*.
34
35
36.. cfunction:: int PyDateTime_CheckExact(PyObject *ob)
37
38 Return true if *ob* is of type :cdata:`PyDateTime_DateTimeType`. *ob* must not
39 be *NULL*.
40
41
42.. cfunction:: int PyTime_Check(PyObject *ob)
43
44 Return true if *ob* is of type :cdata:`PyDateTime_TimeType` or a subtype of
45 :cdata:`PyDateTime_TimeType`. *ob* must not be *NULL*.
46
47
48.. cfunction:: int PyTime_CheckExact(PyObject *ob)
49
50 Return true if *ob* is of type :cdata:`PyDateTime_TimeType`. *ob* must not be
51 *NULL*.
52
53
54.. cfunction:: int PyDelta_Check(PyObject *ob)
55
56 Return true if *ob* is of type :cdata:`PyDateTime_DeltaType` or a subtype of
57 :cdata:`PyDateTime_DeltaType`. *ob* must not be *NULL*.
58
59
60.. cfunction:: int PyDelta_CheckExact(PyObject *ob)
61
62 Return true if *ob* is of type :cdata:`PyDateTime_DeltaType`. *ob* must not be
63 *NULL*.
64
65
66.. cfunction:: int PyTZInfo_Check(PyObject *ob)
67
68 Return true if *ob* is of type :cdata:`PyDateTime_TZInfoType` or a subtype of
69 :cdata:`PyDateTime_TZInfoType`. *ob* must not be *NULL*.
70
71
72.. cfunction:: int PyTZInfo_CheckExact(PyObject *ob)
73
74 Return true if *ob* is of type :cdata:`PyDateTime_TZInfoType`. *ob* must not be
75 *NULL*.
76
77
78Macros to create objects:
79
80.. cfunction:: PyObject* PyDate_FromDate(int year, int month, int day)
81
82 Return a ``datetime.date`` object with the specified year, month and day.
83
84
85.. cfunction:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
86
87 Return a ``datetime.datetime`` object with the specified year, month, day, hour,
88 minute, second and microsecond.
89
90
91.. cfunction:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
92
93 Return a ``datetime.time`` object with the specified hour, minute, second and
94 microsecond.
95
96
97.. cfunction:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
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
105Macros to extract fields from date objects. The argument must be an instance of
106:cdata:`PyDateTime_Date`, including subclasses (such as
107:cdata:`PyDateTime_DateTime`). The argument must not be *NULL*, and the type is
108not checked:
109
110.. cfunction:: int PyDateTime_GET_YEAR(PyDateTime_Date *o)
111
112 Return the year, as a positive int.
113
114
115.. cfunction:: int PyDateTime_GET_MONTH(PyDateTime_Date *o)
116
117 Return the month, as an int from 1 through 12.
118
119
120.. cfunction:: int PyDateTime_GET_DAY(PyDateTime_Date *o)
121
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
126instance of :cdata:`PyDateTime_DateTime`, including subclasses. The argument
127must not be *NULL*, and the type is not checked:
128
129.. cfunction:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
130
131 Return the hour, as an int from 0 through 23.
132
133
134.. cfunction:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
135
136 Return the minute, as an int from 0 through 59.
137
138
139.. cfunction:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
140
141 Return the second, as an int from 0 through 59.
142
143
144.. cfunction:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
145
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
150:cdata:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*,
151and the type is not checked:
152
153.. cfunction:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
154
155 Return the hour, as an int from 0 through 23.
156
157
158.. cfunction:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
159
160 Return the minute, as an int from 0 through 59.
161
162
163.. cfunction:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
164
165 Return the second, as an int from 0 through 59.
166
167
168.. cfunction:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
169
170 Return the microsecond, as an int from 0 through 999999.
171
172
173Macros for the convenience of modules implementing the DB API:
174
175.. cfunction:: PyObject* PyDateTime_FromTimestamp(PyObject *args)
176
177 Create and return a new ``datetime.datetime`` object given an argument tuple
178 suitable for passing to ``datetime.datetime.fromtimestamp()``.
179
180
181.. cfunction:: PyObject* PyDate_FromTimestamp(PyObject *args)
182
183 Create and return a new ``datetime.date`` object given an argument tuple
184 suitable for passing to ``datetime.date.fromtimestamp()``.