def normalize_timedeltas(context, builder, left, right, leftty, rightty): """ Scale either *left* or *right* to the other's unit, in order to have homogenous units. """ factor = npdatetime.get_timedelta_conversion_factor(leftty.unit, rightty.unit) if factor is not None: return scale_by_constant(builder, left, factor), right factor = npdatetime.get_timedelta_conversion_factor(rightty.unit, leftty.unit) if factor is not None: return left, scale_by_constant(builder, right, factor) # Typing should not let this happen, except on == and != operators raise RuntimeError("cannot normalize %r and %r" % (leftty, rightty))
def normalize_timedeltas(context, builder, left, right, leftty, rightty): """ Scale either *left* or *right* to the other's unit, in order to have homogenous units. """ factor = npdatetime.get_timedelta_conversion_factor( leftty.unit, rightty.unit) if factor is not None: return scale_by_constant(builder, left, factor), right factor = npdatetime.get_timedelta_conversion_factor( rightty.unit, leftty.unit) if factor is not None: return left, scale_by_constant(builder, right, factor) # Typing should not let this happen, except on == and != operators raise RuntimeError("cannot normalize %r and %r" % (leftty, rightty))
def impl(context, builder, dt_arg, dt_unit, td_arg, td_unit, ret_unit): ret = alloc_timedelta_result(builder) with cgutils.if_likely(builder, are_not_nat(builder, [dt_arg, td_arg])): dt_arg = convert_datetime_for_arith(builder, dt_arg, dt_unit, ret_unit) td_factor = npdatetime.get_timedelta_conversion_factor(td_unit, ret_unit) td_arg = scale_by_constant(builder, td_arg, td_factor) ret_val = getattr(builder, ll_op_name)(dt_arg, td_arg) builder.store(ret_val, ret) return builder.load(ret)
def scale_timedelta(context, builder, val, srcty, destty): """ Scale the timedelta64 *val* from *srcty* to *destty* (both numba.types.NPTimedelta instances) """ factor = npdatetime.get_timedelta_conversion_factor(srcty.unit, destty.unit) if factor is None: # This can happen when using explicit output in a ufunc. raise NotImplementedError("cannot convert timedelta64 from %r to %r" % (srcty.unit, destty.unit)) return scale_by_constant(builder, val, factor)
def convert_datetime_for_arith(builder, dt_val, src_unit, dest_unit): """ Convert datetime *dt_val* from *src_unit* to *dest_unit*. """ # First partial conversion to days or weeks, if necessary. dt_val, dt_unit = reduce_datetime_for_unit(builder, dt_val, src_unit, dest_unit) # Then multiply by the remaining constant factor. dt_factor = npdatetime.get_timedelta_conversion_factor(dt_unit, dest_unit) if dt_factor is None: # This can happen when using explicit output in a ufunc. raise NotImplementedError("cannot convert datetime64 from %r to %r" % (src_unit, dest_unit)) return scale_by_constant(builder, dt_val, dt_factor)