Spaces:
Running on Zero
Running on Zero
File size: 9,567 Bytes
9273228 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | # DateTime Objects {#datetimeobjects}
Various date and time objects are supplied by the `datetime`{.interpreted-text role="mod"} module. Before using any of these functions, the header file `datetime.h`{.interpreted-text role="file"} must be included in your source (note that this is not included by `Python.h`{.interpreted-text role="file"}), and the macro `PyDateTime_IMPORT`{.interpreted-text role="c:macro"} must be invoked, usually as part of the module initialisation function. The macro puts a pointer to a C structure into a static variable, `PyDateTimeAPI`{.interpreted-text role="c:data"}, that is used by the following macros.
> Import the datetime C API.
>
> On success, populate the `PyDateTimeAPI`{.interpreted-text role="c:var"} pointer. On failure, set `PyDateTimeAPI`{.interpreted-text role="c:var"} to `NULL` and set an exception. The caller must check if an error occurred via `PyErr_Occurred`{.interpreted-text role="c:func"}:
>
> ```
> PyDateTime_IMPORT;
> if (PyErr_Occurred()) { /* cleanup */ }
> ```
>
> :::: warning
> ::: title
> Warning
> :::
>
> This is not compatible with subinterpreters.
> ::::
>
> ::: versionchanged
> 3.15
>
> This macro is now thread safe.
> :::
> Structure containing the fields for the datetime C API.
>
> The fields of this structure are private and subject to change.
>
> Do not use this directly; prefer `PyDateTime_*` APIs instead.
> Dynamically allocated object containing the datetime C API.
>
> This variable is only available once `PyDateTime_IMPORT`{.interpreted-text role="c:macro"} succeeds.
>
> ::: versionchanged
> 3.15
>
> This variable should not be accessed directly as direct access is not thread-safe. Use `PyDateTime_IMPORT`{.interpreted-text role="c:func"} instead.
> :::
> This subtype of `PyObject`{.interpreted-text role="c:type"} represents a Python date object.
> This subtype of `PyObject`{.interpreted-text role="c:type"} represents a Python datetime object.
> This subtype of `PyObject`{.interpreted-text role="c:type"} represents a Python time object.
> This subtype of `PyObject`{.interpreted-text role="c:type"} represents the difference between two datetime values.
> This instance of `PyTypeObject`{.interpreted-text role="c:type"} represents the Python date type; it is the same object as `datetime.date`{.interpreted-text role="class"} in the Python layer.
> This instance of `PyTypeObject`{.interpreted-text role="c:type"} represents the Python datetime type; it is the same object as `datetime.datetime`{.interpreted-text role="class"} in the Python layer.
> This instance of `PyTypeObject`{.interpreted-text role="c:type"} represents the Python time type; it is the same object as `datetime.time`{.interpreted-text role="class"} in the Python layer.
> This instance of `PyTypeObject`{.interpreted-text role="c:type"} represents the Python type for the difference between two datetime values; it is the same object as `datetime.timedelta`{.interpreted-text role="class"} in the Python layer.
> This instance of `PyTypeObject`{.interpreted-text role="c:type"} represents the Python time zone info type; it is the same object as `datetime.tzinfo`{.interpreted-text role="class"} in the Python layer.
Macro for access to the UTC singleton:
> Returns the time zone singleton representing UTC, the same object as `datetime.timezone.utc`{.interpreted-text role="attr"}.
>
> ::: versionadded
> 3.7
> :::
Type-check macros:
> Return true if *ob* is of type `PyDateTime_DateType`{.interpreted-text role="c:data"} or a subtype of `!PyDateTime_DateType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_DateType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_DateTimeType`{.interpreted-text role="c:data"} or a subtype of `!PyDateTime_DateTimeType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_DateTimeType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_TimeType`{.interpreted-text role="c:data"} or a subtype of `!PyDateTime_TimeType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_TimeType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_DeltaType`{.interpreted-text role="c:data"} or a subtype of `!PyDateTime_DeltaType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_DeltaType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_TZInfoType`{.interpreted-text role="c:data"} or a subtype of `!PyDateTime_TZInfoType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
> Return true if *ob* is of type `PyDateTime_TZInfoType`{.interpreted-text role="c:data"}. *ob* must not be `NULL`. This function always succeeds.
Macros to create objects:
> Return a `datetime.date`{.interpreted-text role="class"} object with the specified year, month and day.
> Return a `datetime.datetime`{.interpreted-text role="class"} object with the specified year, month, day, hour, minute, second and microsecond.
> Return a `datetime.datetime`{.interpreted-text role="class"} object with the specified year, month, day, hour, minute, second, microsecond and fold.
>
> ::: versionadded
> 3.6
> :::
> Return a `datetime.time`{.interpreted-text role="class"} object with the specified hour, minute, second and microsecond.
> Return a `datetime.time`{.interpreted-text role="class"} object with the specified hour, minute, second, microsecond and fold.
>
> ::: versionadded
> 3.6
> :::
> Return a `datetime.timedelta`{.interpreted-text role="class"} 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`{.interpreted-text role="class"} objects.
> Return a `datetime.timezone`{.interpreted-text role="class"} object with an unnamed fixed offset represented by the *offset* argument.
>
> ::: versionadded
> 3.7
> :::
> Return a `datetime.timezone`{.interpreted-text role="class"} object with a fixed offset represented by the *offset* argument and with tzname *name*.
>
> ::: versionadded
> 3.7
> :::
Macros to extract fields from date objects. The argument must be an instance of `PyDateTime_Date`{.interpreted-text role="c:type"}, including subclasses (such as `PyDateTime_DateTime`{.interpreted-text role="c:type"}). The argument must not be `NULL`, and the type is not checked:
> Return the year, as a positive int.
> Return the month, as an int from 1 through 12.
> Return the day, as an int from 1 through 31.
Macros to extract fields from datetime objects. The argument must be an instance of `PyDateTime_DateTime`{.interpreted-text role="c:type"}, including subclasses. The argument must not be `NULL`, and the type is not checked:
> Return the hour, as an int from 0 through 23.
> Return the minute, as an int from 0 through 59.
> Return the second, as an int from 0 through 59.
> Return the microsecond, as an int from 0 through 999999.
> Return the fold, as an int from 0 through 1.
>
> ::: versionadded
> 3.6
> :::
> Return the tzinfo (which may be `None`).
>
> ::: versionadded
> 3.10
> :::
Macros to extract fields from time objects. The argument must be an instance of `PyDateTime_Time`{.interpreted-text role="c:type"}, including subclasses. The argument must not be `NULL`, and the type is not checked:
> Return the hour, as an int from 0 through 23.
> Return the minute, as an int from 0 through 59.
> Return the second, as an int from 0 through 59.
> Return the microsecond, as an int from 0 through 999999.
> Return the fold, as an int from 0 through 1.
>
> ::: versionadded
> 3.6
> :::
> Return the tzinfo (which may be `None`).
>
> ::: versionadded
> 3.10
> :::
Macros to extract fields from time delta objects. The argument must be an instance of `PyDateTime_Delta`{.interpreted-text role="c:type"}, including subclasses. The argument must not be `NULL`, and the type is not checked:
> Return the number of days, as an int from -999999999 to 999999999.
>
> ::: versionadded
> 3.3
> :::
> Return the number of seconds, as an int from 0 through 86399.
>
> ::: versionadded
> 3.3
> :::
> Return the number of microseconds, as an int from 0 through 999999.
>
> ::: versionadded
> 3.3
> :::
Macros for the convenience of modules implementing the DB API:
> Create and return a new `datetime.datetime`{.interpreted-text role="class"} object given an argument tuple suitable for passing to `datetime.datetime.fromtimestamp`{.interpreted-text role="meth"}.
> Create and return a new `datetime.date`{.interpreted-text role="class"} object given an argument tuple suitable for passing to `datetime.date.fromtimestamp`{.interpreted-text role="meth"}.
# Internal data
The following symbols are exposed by the C API but should be considered internal-only.
> Name of the datetime capsule to pass to `PyCapsule_Import`{.interpreted-text role="c:func"}.
>
> Internal usage only. Use `PyDateTime_IMPORT`{.interpreted-text role="c:macro"} instead.
|