MCPcopy Create free account
hub / github.com/numpy/numpy / convert_pydatetime_to_datetimestruct

Function convert_pydatetime_to_datetimestruct

numpy/core/src/multiarray/datetime.c:2111–2319  ·  view source on GitHub ↗

* Tests for and converts a Python datetime.datetime or datetime.date * object into a NumPy npy_datetimestruct. * * While the C API has PyDate_* and PyDateTime_* functions, the following * implementation just asks for attributes, and thus supports * datetime duck typing. The tzinfo time zone conversion would require * this style of access anyway. * * 'out_bestunit' gives a suggested unit ba

Source from the content-addressed store, hash-verified

2109 * if obj doesn't have the needed date or datetime attributes.
2110 */
2111NPY_NO_EXPORT int
2112convert_pydatetime_to_datetimestruct(PyObject *obj, npy_datetimestruct *out,
2113 NPY_DATETIMEUNIT *out_bestunit,
2114 int apply_tzinfo)
2115{
2116 PyObject *tmp;
2117 int isleap;
2118
2119 /* Initialize the output to all zeros */
2120 memset(out, 0, sizeof(npy_datetimestruct));
2121 out->month = 1;
2122 out->day = 1;
2123
2124 /* Need at least year/month/day attributes */
2125 if (!PyObject_HasAttrString(obj, "year") ||
2126 !PyObject_HasAttrString(obj, "month") ||
2127 !PyObject_HasAttrString(obj, "day")) {
2128 return 1;
2129 }
2130
2131 /* Get the year */
2132 tmp = PyObject_GetAttrString(obj, "year");
2133 if (tmp == NULL) {
2134 return -1;
2135 }
2136 out->year = PyLong_AsLong(tmp);
2137 if (error_converting(out->year)) {
2138 Py_DECREF(tmp);
2139 return -1;
2140 }
2141 Py_DECREF(tmp);
2142
2143 /* Get the month */
2144 tmp = PyObject_GetAttrString(obj, "month");
2145 if (tmp == NULL) {
2146 return -1;
2147 }
2148 out->month = PyLong_AsLong(tmp);
2149 if (error_converting(out->month)) {
2150 Py_DECREF(tmp);
2151 return -1;
2152 }
2153 Py_DECREF(tmp);
2154
2155 /* Get the day */
2156 tmp = PyObject_GetAttrString(obj, "day");
2157 if (tmp == NULL) {
2158 return -1;
2159 }
2160 out->day = PyLong_AsLong(tmp);
2161 if (error_converting(out->day)) {
2162 Py_DECREF(tmp);
2163 return -1;
2164 }
2165 Py_DECREF(tmp);
2166
2167 /* Validate that the month and day are valid for the year */
2168 if (out->month < 1 || out->month > 12) {

Callers 2

Calls 2

is_leapyearFunction · 0.85

Tested by

no test coverage detected