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

Function time_to_time_resolve_descriptors

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

* Describes casting within datetimes or timedelta */

Source from the content-addressed store, hash-verified

3756 * Describes casting within datetimes or timedelta
3757 */
3758static NPY_CASTING
3759time_to_time_resolve_descriptors(
3760 PyArrayMethodObject *NPY_UNUSED(self),
3761 PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
3762 PyArray_Descr *given_descrs[2],
3763 PyArray_Descr *loop_descrs[2],
3764 npy_intp *view_offset)
3765{
3766 /* This is a within-dtype cast, which currently must handle byteswapping */
3767 Py_INCREF(given_descrs[0]);
3768 loop_descrs[0] = given_descrs[0];
3769 if (given_descrs[1] == NULL) {
3770 loop_descrs[1] = NPY_DT_CALL_ensure_canonical(given_descrs[0]);
3771 }
3772 else {
3773 Py_INCREF(given_descrs[1]);
3774 loop_descrs[1] = given_descrs[1];
3775 }
3776
3777 int is_timedelta = given_descrs[0]->type_num == NPY_TIMEDELTA;
3778
3779 if (given_descrs[0] == given_descrs[1]) {
3780 *view_offset = 0;
3781 return NPY_NO_CASTING;
3782 }
3783
3784 npy_bool byteorder_may_allow_view = (
3785 PyDataType_ISNOTSWAPPED(loop_descrs[0])
3786 == PyDataType_ISNOTSWAPPED(loop_descrs[1]));
3787
3788 PyArray_DatetimeMetaData *meta1, *meta2;
3789 meta1 = get_datetime_metadata_from_dtype(loop_descrs[0]);
3790 assert(meta1 != NULL);
3791 meta2 = get_datetime_metadata_from_dtype(loop_descrs[1]);
3792 assert(meta2 != NULL);
3793
3794 if ((meta1->base == meta2->base && meta1->num == meta2->num) ||
3795 // handle some common metric prefix conversions
3796 // 1000 fold conversions
3797 ((meta2->base >= 7) && (meta1->base - meta2->base == 1)
3798 && ((meta1->num / meta2->num) == 1000)) ||
3799 // 10^6 fold conversions
3800 ((meta2->base >= 7) && (meta1->base - meta2->base == 2)
3801 && ((meta1->num / meta2->num) == 1000000)) ||
3802 // 10^9 fold conversions
3803 ((meta2->base >= 7) && (meta1->base - meta2->base == 3)
3804 && ((meta1->num / meta2->num) == 1000000000))) {
3805 if (byteorder_may_allow_view) {
3806 *view_offset = 0;
3807 return NPY_NO_CASTING;
3808 }
3809 return NPY_EQUIV_CASTING;
3810 }
3811 else if (meta1->base == NPY_FR_GENERIC) {
3812 if (byteorder_may_allow_view) {
3813 *view_offset = 0;
3814 }
3815 return NPY_SAFE_CASTING ;

Callers

nothing calls this directly

Calls 2

Tested by

no test coverage detected