dt_status_t dt_sub_intervals()

in src/dt.c [321:352]


dt_status_t dt_sub_intervals(const dt_interval_t *lhs, const dt_interval_t *rhs, dt_interval_t *result)
{
    dt_compare_result_t cr = 0;
    unsigned long seconds = 0;
    unsigned long nano_seconds = 0;
    dt_status_t s = DT_UNKNOWN_ERROR;

    if (!lhs || !rhs || !result) {
        return DT_INVALID_ARGUMENT;
    }

    s = dt_compare_intervals(lhs, rhs, &cr);

    if (s != DT_OK) {
        return s;
    }
    if (cr == DT_LESSER) {
        return DT_OVERFLOW;
    }

    seconds = lhs->seconds - rhs->seconds - (lhs->nano_seconds < rhs->nano_seconds ? 1L : 0L);
    if (lhs->nano_seconds < rhs->nano_seconds) {
        nano_seconds = 1000000000UL + lhs->nano_seconds - rhs->nano_seconds;
    } else {
        nano_seconds = lhs->nano_seconds - rhs->nano_seconds;
    }


    result->seconds = seconds;
    result->nano_seconds = nano_seconds;
    return DT_OK;
}