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

Function convert_datetime_to_pyobject

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

* Converts a datetime into a PyObject *. * * Not-a-time is returned as the string "NaT". * For days or coarser, returns a datetime.date. * For microseconds or coarser, returns a datetime.datetime. * For units finer than microseconds, returns an integer. */

Source from the content-addressed store, hash-verified

2841 * For units finer than microseconds, returns an integer.
2842 */
2843NPY_NO_EXPORT PyObject *
2844convert_datetime_to_pyobject(npy_datetime dt, PyArray_DatetimeMetaData *meta)
2845{
2846 PyObject *ret = NULL;
2847 npy_datetimestruct dts;
2848
2849 /*
2850 * Convert NaT (not-a-time) and any value with generic units
2851 * into None.
2852 */
2853 if (dt == NPY_DATETIME_NAT || meta->base == NPY_FR_GENERIC) {
2854 Py_RETURN_NONE;
2855 }
2856
2857 /* If the type's precision is greater than microseconds, return an int */
2858 if (meta->base > NPY_FR_us) {
2859 return PyLong_FromLongLong(dt);
2860 }
2861
2862 /* Convert to a datetimestruct */
2863 if (convert_datetime_to_datetimestruct(meta, dt, &dts) < 0) {
2864 return NULL;
2865 }
2866
2867 /*
2868 * If the year is outside the range of years supported by Python's
2869 * datetime, or the datetime64 falls on a leap second,
2870 * return a raw int.
2871 */
2872 if (dts.year < 1 || dts.year > 9999 || dts.sec == 60) {
2873 return PyLong_FromLongLong(dt);
2874 }
2875
2876 /* If the type's precision is greater than days, return a datetime */
2877 if (meta->base > NPY_FR_D) {
2878 ret = PyDateTime_FromDateAndTime(dts.year, dts.month, dts.day,
2879 dts.hour, dts.min, dts.sec, dts.us);
2880 }
2881 /* Otherwise return a date */
2882 else {
2883 ret = PyDate_FromDate(dts.year, dts.month, dts.day);
2884 }
2885
2886 return ret;
2887}
2888
2889/*
2890 * Converts a timedelta into a PyObject *.

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected