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

Function convert_timedelta_to_pyobject

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

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

Source from the content-addressed store, hash-verified

2894 * For units finer than microseconds, returns an integer.
2895 */
2896NPY_NO_EXPORT PyObject *
2897convert_timedelta_to_pyobject(npy_timedelta td, PyArray_DatetimeMetaData *meta)
2898{
2899 npy_timedelta value;
2900 int days = 0, seconds = 0, useconds = 0;
2901
2902 /*
2903 * Convert NaT (not-a-time) into None.
2904 */
2905 if (td == NPY_DATETIME_NAT) {
2906 Py_RETURN_NONE;
2907 }
2908
2909 /*
2910 * If the type's precision is greater than microseconds, is
2911 * Y/M/B (nonlinear units), or is generic units, return an int
2912 */
2913 if (meta->base > NPY_FR_us ||
2914 meta->base == NPY_FR_Y ||
2915 meta->base == NPY_FR_M ||
2916 meta->base == NPY_FR_GENERIC) {
2917 return PyLong_FromLongLong(td);
2918 }
2919
2920 value = td;
2921
2922 /* Apply the unit multiplier (TODO: overflow treatment...) */
2923 value *= meta->num;
2924
2925 /* Convert to days/seconds/useconds */
2926 switch (meta->base) {
2927 case NPY_FR_W:
2928 days = value * 7;
2929 break;
2930 case NPY_FR_D:
2931 days = value;
2932 break;
2933 case NPY_FR_h:
2934 days = extract_unit_64(&value, 24ULL);
2935 seconds = value*60*60;
2936 break;
2937 case NPY_FR_m:
2938 days = extract_unit_64(&value, 60ULL*24);
2939 seconds = value*60;
2940 break;
2941 case NPY_FR_s:
2942 days = extract_unit_64(&value, 60ULL*60*24);
2943 seconds = value;
2944 break;
2945 case NPY_FR_ms:
2946 days = extract_unit_64(&value, 1000ULL*60*60*24);
2947 seconds = extract_unit_64(&value, 1000ULL);
2948 useconds = value*1000;
2949 break;
2950 case NPY_FR_us:
2951 days = extract_unit_64(&value, 1000ULL*1000*60*60*24);
2952 seconds = extract_unit_64(&value, 1000ULL*1000);
2953 useconds = value;

Callers

nothing calls this directly

Calls 1

extract_unit_64Function · 0.85

Tested by

no test coverage detected