datetime.rst 6.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
.. highlightlang:: c

.. _datetimeobjects:

DateTime Objects
----------------

Various date and time objects are supplied by the :mod:`datetime` module.
Before using any of these functions, the header file :file:`datetime.h` must be
included in your source (note that this is not included by :file:`Python.h`),
11
and the macro :c:macro:`PyDateTime_IMPORT` must be invoked, usually as part of
12
the module initialisation function.  The macro puts a pointer to a C structure
13
into a static variable, :c:data:`PyDateTimeAPI`, that is used by the following
14
macros.
15 16 17

Type-check macros:

18
.. c:function:: int PyDate_Check(PyObject *ob)
19

20 21
   Return true if *ob* is of type :c:data:`PyDateTime_DateType` or a subtype of
   :c:data:`PyDateTime_DateType`.  *ob* must not be *NULL*.
22 23


24
.. c:function:: int PyDate_CheckExact(PyObject *ob)
25

26
   Return true if *ob* is of type :c:data:`PyDateTime_DateType`. *ob* must not be
27 28 29
   *NULL*.


30
.. c:function:: int PyDateTime_Check(PyObject *ob)
31

32 33
   Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType` or a subtype of
   :c:data:`PyDateTime_DateTimeType`.  *ob* must not be *NULL*.
34 35


36
.. c:function:: int PyDateTime_CheckExact(PyObject *ob)
37

38
   Return true if *ob* is of type :c:data:`PyDateTime_DateTimeType`. *ob* must not
39 40 41
   be *NULL*.


42
.. c:function:: int PyTime_Check(PyObject *ob)
43

44 45
   Return true if *ob* is of type :c:data:`PyDateTime_TimeType` or a subtype of
   :c:data:`PyDateTime_TimeType`.  *ob* must not be *NULL*.
46 47


48
.. c:function:: int PyTime_CheckExact(PyObject *ob)
49

50
   Return true if *ob* is of type :c:data:`PyDateTime_TimeType`. *ob* must not be
51 52 53
   *NULL*.


54
.. c:function:: int PyDelta_Check(PyObject *ob)
55

56 57
   Return true if *ob* is of type :c:data:`PyDateTime_DeltaType` or a subtype of
   :c:data:`PyDateTime_DeltaType`.  *ob* must not be *NULL*.
58 59


60
.. c:function:: int PyDelta_CheckExact(PyObject *ob)
61

62
   Return true if *ob* is of type :c:data:`PyDateTime_DeltaType`. *ob* must not be
63 64 65
   *NULL*.


66
.. c:function:: int PyTZInfo_Check(PyObject *ob)
67

68 69
   Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType` or a subtype of
   :c:data:`PyDateTime_TZInfoType`.  *ob* must not be *NULL*.
70 71


72
.. c:function:: int PyTZInfo_CheckExact(PyObject *ob)
73

74
   Return true if *ob* is of type :c:data:`PyDateTime_TZInfoType`. *ob* must not be
75 76 77 78 79
   *NULL*.


Macros to create objects:

80
.. c:function:: PyObject* PyDate_FromDate(int year, int month, int day)
81 82 83 84

   Return a ``datetime.date`` object with the specified year, month and day.


85
.. c:function:: PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)
86 87 88 89 90

   Return a ``datetime.datetime`` object with the specified year, month, day, hour,
   minute, second and microsecond.


91
.. c:function:: PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)
92 93 94 95 96

   Return a ``datetime.time`` object with the specified hour, minute, second and
   microsecond.


97
.. c:function:: PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)
98 99 100 101 102 103 104 105

   Return a ``datetime.timedelta`` object representing the given number of days,
   seconds and microseconds.  Normalization is performed so that the resulting
   number of microseconds and seconds lie in the ranges documented for
   ``datetime.timedelta`` objects.


Macros to extract fields from date objects.  The argument must be an instance of
106 107
:c:data:`PyDateTime_Date`, including subclasses (such as
:c:data:`PyDateTime_DateTime`).  The argument must not be *NULL*, and the type is
108 109
not checked:

110
.. c:function:: int PyDateTime_GET_YEAR(PyDateTime_Date *o)
111 112 113 114

   Return the year, as a positive int.


115
.. c:function:: int PyDateTime_GET_MONTH(PyDateTime_Date *o)
116 117 118 119

   Return the month, as an int from 1 through 12.


120
.. c:function:: int PyDateTime_GET_DAY(PyDateTime_Date *o)
121 122 123 124 125

   Return the day, as an int from 1 through 31.


Macros to extract fields from datetime objects.  The argument must be an
126
instance of :c:data:`PyDateTime_DateTime`, including subclasses. The argument
127 128
must not be *NULL*, and the type is not checked:

129
.. c:function:: int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)
130 131 132 133

   Return the hour, as an int from 0 through 23.


134
.. c:function:: int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)
135 136 137 138

   Return the minute, as an int from 0 through 59.


139
.. c:function:: int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)
140 141 142 143

   Return the second, as an int from 0 through 59.


144
.. c:function:: int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)
145 146 147 148 149

   Return the microsecond, as an int from 0 through 999999.


Macros to extract fields from time objects.  The argument must be an instance of
150
:c:data:`PyDateTime_Time`, including subclasses. The argument must not be *NULL*,
151 152
and the type is not checked:

153
.. c:function:: int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)
154 155 156 157

   Return the hour, as an int from 0 through 23.


158
.. c:function:: int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)
159 160 161 162

   Return the minute, as an int from 0 through 59.


163
.. c:function:: int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)
164 165 166 167

   Return the second, as an int from 0 through 59.


168
.. c:function:: int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)
169 170 171 172

   Return the microsecond, as an int from 0 through 999999.


173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
Macros to extract fields from time delta objects.  The argument must be an
instance of :c:data:`PyDateTime_Delta`, including subclasses. The argument must
not be *NULL*, and the type is not checked:

.. c:function:: int PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o)

   Return the number of days, as an int from -999999999 to 999999999.

   .. versionadded:: 3.3


.. c:function:: int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)

   Return the number of seconds, as an int from 0 through 86399.

   .. versionadded:: 3.3


.. c:function:: int PyDateTime_DELTA_GET_MICROSECOND(PyDateTime_Delta *o)

   Return the number of microseconds, as an int from 0 through 999999.

   .. versionadded:: 3.3


198 199
Macros for the convenience of modules implementing the DB API:

200
.. c:function:: PyObject* PyDateTime_FromTimestamp(PyObject *args)
201 202 203 204 205

   Create and return a new ``datetime.datetime`` object given an argument tuple
   suitable for passing to ``datetime.datetime.fromtimestamp()``.


206
.. c:function:: PyObject* PyDate_FromTimestamp(PyObject *args)
207 208 209

   Create and return a new ``datetime.date`` object given an argument tuple
   suitable for passing to ``datetime.date.fromtimestamp()``.