Exemplo n.º 1
0
int
main() {
    int ntests, nholidays;
    int i;
    dt_t holidays[20];

    nholidays = sizeof(days) / sizeof(*days);
    for (i = 0; i < nholidays; i++) {
        const struct ymd date = days[i];
        holidays[i] = dt_from_ymd(date.y, date.m, date.d);
    }

    ntests = sizeof(tests) / sizeof(*tests);
    for (i = 0; i < ntests; i++) {
        const struct test t = tests[i];

        {
            dt_t src = dt_from_ymd(t.y, t.m, t.d);
            dt_t got = dt_add_workdays(src, t.delta, holidays, nholidays);
            dt_t exp = dt_from_ymd(t.ey, t.em, t.ed);
            cmp_ok(got, "==", exp, "dt_add_workdays(%.4d-%.2d-%.2d, %d)", 
              src, t.y, t.m, t.d, t.delta);
        }
    }
    done_testing();
}
Exemplo n.º 2
0
int 
main() {
    int i, ntests;

    ntests = sizeof(tests) / sizeof(*tests);
    for (i = 0; i < ntests; i++) {
        const struct test t = tests[i];

        {
            dt_t src = dt_from_ymd(t.y, t.m, t.d);
            dt_t got = dt_start_of_month(src, t.delta);
            dt_t exp = dt_from_ymd(t.ey, t.em, t.ed);
            cmp_ok(got, "==", exp, "dt_start_of_month(%d (%.4d-%.2d-%.2d), %d)", 
              src, t.y, t.m, t.d, t.delta);
        }

        {
            dt_t src = dt_from_ymd(t.y, t.m, t.d);
            dt_t dt1 = dt_start_of_month(src, t.delta);
            dt_t dt2 = dt_end_of_month(src, t.delta - 1) + 1;
            cmp_ok(dt1, "==", dt2, "dt_start_of_month(%d, %d) == dt_end_of_month(%d, %d - 1) + 1",
              src, t.y, src, t.y);
        }

        {
            dt_t dt1 = dt_from_ymd(t.y, t.m, t.d);
            dt_t dt2 = dt_start_of_month(dt1, t.delta);
            int got = dt_delta_months(dt1, dt2, 0);
            cmp_ok(got, "==", t.delta, "dt_delta_months(%d, %d, false)", dt1, dt2);
        }
    }
    done_testing();
}
Exemplo n.º 3
0
int 
main() {
    int i, ntests;

    ntests = sizeof(good) / sizeof(*good);
    for (i = 0; i < ntests; i++) {
        const struct good_t t = good[i];

        {
            dt_t got = 0, exp = 0;
            size_t glen;

            glen = dt_parse_iso_date(t.str, strlen(t.str), &got);
            ok(glen == t.elen, "dt_parse_iso_date(%s) size_t: %d", t.str, (int)glen);
            exp = dt_from_ymd(t.ey, t.em, t.ed);
            cmp_ok(got, "==", exp, "dt_parse_iso_date(%s)", t.str);
        }
    }

    ntests = sizeof(bad) / sizeof(*bad);
    for (i = 0; i < ntests; i++) {
        const struct bad_t t = bad[i];

        {
            dt_t got = 0;
            size_t glen;

            glen = dt_parse_iso_date(t.str, strlen(t.str), &got);
            ok(glen == 0, "dt_parse_iso_date(%s) size_t: %d", t.str, (int)glen);
        }
    }
    done_testing();
}
Exemplo n.º 4
0
moment_t
THX_moment_new(pTHX_ IV Y, IV M, IV D, IV h, IV m, IV s, IV ns, IV offset) {
    moment_t r;
    int64_t rdn, sod;

    THX_check_year(aTHX_ Y);
    THX_check_month(aTHX_ M);
    if (D < 1 || D > 28) {
        int dim = dt_days_in_month(Y, M);
        if (D < 1 || D > dim)
            croak("Parameter 'day' is out of the range [1, %d]", dim);
    }
    THX_check_hour(aTHX_ h);
    THX_check_minute(aTHX_ m);
    THX_check_second(aTHX_ s);
    THX_check_nanosecond(aTHX_ ns);
    THX_check_offset(aTHX_ offset);

    rdn = dt_rdn(dt_from_ymd(Y, M, D));
    sod = h * 3600 + m * 60 + s;
    r.sec    = rdn * SECS_PER_DAY + sod;
    r.nsec   = ns;
    r.offset = offset;
    return r;
}
Exemplo n.º 5
0
int
main() {
    int ntests, nholidays;
    int i;
    dt_t holidays[20];

    nholidays = sizeof(days) / sizeof(*days);
    for (i = 0; i < nholidays; i++) {
        const struct ymd date = days[i];
        holidays[i] = dt_from_ymd(date.y, date.m, date.d);
    }

    ntests = sizeof(days) / sizeof(*days);
    for (i = 0; i < ntests; i++) {
        const struct ymd t = days[i];

        {
            dt_t dt = dt_from_ymd(t.y, t.m, t.d);
            ok(!dt_is_workday(dt, holidays, nholidays), "dt_is_workday(%.4d-%.2d-%.2d)",
              t.y, t.m, t.d);
        }
    }

    ntests = sizeof(weekends) / sizeof(*weekends);
    for (i = 0; i < ntests; i++) {
        const struct ymd t = weekends[i];

        {
            dt_t dt = dt_from_ymd(t.y, t.m, t.d);
            ok(!dt_is_workday(dt, holidays, nholidays), "dt_is_workday(%.4d-%.2d-%.2d)",
              t.y, t.m, t.d);
        }
    }

    ntests = sizeof(weekdays) / sizeof(*weekdays);
    for (i = 0; i < ntests; i++) {
        const struct ymd t = weekdays[i];

        {
            dt_t dt = dt_from_ymd(t.y, t.m, t.d);
            ok(dt_is_workday(dt, holidays, nholidays), "dt_is_workday(%.4d-%.2d-%.2d)",
              t.y, t.m, t.d);
        }
    }

    done_testing();
}
Exemplo n.º 6
0
static moment_t
THX_moment_with_day_of_month(pTHX_ const moment_t *mt, IV v) {
    int y, m;

    dt_to_ymd(moment_local_dt(mt), &y, &m, NULL);
    if (v < 1 || v > 28) {
        int dim = dt_days_in_month(y, m);
        if (v < 1 || v > dim)
            croak("Parameter 'day' is out of the range [1, %d]", dim);
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ymd(y, m, (int)v));
}
Exemplo n.º 7
0
static moment_t
THX_moment_with_month(pTHX_ const moment_t *mt, IV v) {
    int y, m, d;

    THX_check_month(aTHX_ v);
    dt_to_ymd(moment_local_dt(mt), &y, NULL, &d);
    m = (int)v;
    if (d > 28) {
        int dim = dt_days_in_month(y, m);
        if (d > dim)
            d = dim;
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ymd(y, m, d));
}
Exemplo n.º 8
0
dt_t
dt_end_of_month(dt_t dt, int offset) {
    int y, m;
    dt_to_ymd(dt, &y, &m, NULL);
    return dt_from_ymd(y, m + offset + 1, 0);
}
Exemplo n.º 9
0
dt_t
dt_start_of_month(dt_t dt, int offset) {
    int y, m;
    dt_to_ymd(dt, &y, &m, NULL);
    return dt_from_ymd(y, m + offset, 1);
}
Exemplo n.º 10
0
/*
 *  Basic      Extended
 *  20121224   2012-12-24   Calendar date   (ISO 8601)
 *  2012359    2012-359     Ordinal date    (ISO 8601)
 *  2012W521   2012-W52-1   Week date       (ISO 8601)
 *  2012Q485   2012-Q4-85   Quarter date
 */
size_t
dt_parse_iso_date(const char *str, size_t len, dt_t *dtp) {
    const unsigned char *p;
    int y, x, d;
    size_t n;
    dt_t dt;

    p = (const unsigned char *)str;
    n = count_digits(p, 0, len);
    switch (n) {
        case 4: /* 2012 (year) */
            y = parse_number(p, 0, 4);
            break;
        case 7: /* 2012359 (basic ordinal date) */
            y = parse_number(p, 0, 4);
            d = parse_number(p, 4, 3);
            goto yd;
        case 8: /* 20121224 (basic calendar date) */
            y = parse_number(p, 0, 4);
            x = parse_number(p, 4, 2);
            d = parse_number(p, 6, 2);
            goto ymd;
        default:
            return 0;
    }

    if (len < 8)
        return 0;

    n = count_digits(p, 5, len);
    switch (p[4]) {
        case '-': /* 2012-359 | 2012-12-24 | 2012-W52-1 | 2012-Q4-85 */
            break;
#ifndef DT_PARSE_ISO_STRICT
        case 'Q': /* 2012Q485 */
            if (n != 3)
                return 0;
            x = parse_number(p, 5, 1);
            d = parse_number(p, 6, 2);
            n = 8;
            goto yqd;
#endif
        case 'W': /* 2012W521 */
            if (n != 3)
                return 0;
            x = parse_number(p, 5, 2);
            d = parse_number(p, 7, 1);
            n = 8;
            goto ywd;
        default:
            return 0;
    }

    switch (n) {
        case 0: /* 2012-Q4-85 | 2012-W52-1 */
            break;
        case 2: /* 2012-12-24 */
            if (p[7] != '-' || count_digits(p, 8, len) != 2)
                return 0;
            x = parse_number(p, 5, 2);
            d = parse_number(p, 8, 2);
            n = 10;
            goto ymd;
        case 3: /* 2012-359 */
            d = parse_number(p, 5, 3);
            n = 8;
            goto yd;
        default:
            return 0;
    }

    if (len < 10)
        return 0;

    n = count_digits(p, 6, len);
    switch (p[5]) {
#ifndef DT_PARSE_ISO_STRICT
        case 'Q': /* 2012-Q4-85 */
            if (n != 1 || p[7] != '-' || count_digits(p, 8, len) != 2)
                return 0;
            x = parse_number(p, 6, 1);
            d = parse_number(p, 8, 2);
            n = 10;
            goto yqd;
#endif
        case 'W': /* 2012-W52-1 */
            if (n != 2 || p[8] != '-' || count_digits(p, 9, len) != 1)
                return 0;
            x = parse_number(p, 6, 2);
            d = parse_number(p, 9, 1);
            n = 10;
            goto ywd;
        default:
            return 0;
    }

  yd:
    if (!dt_valid_yd(y, d))
        return 0;
    dt = dt_from_yd(y, d);
    goto finish;

  ymd:
    if (!dt_valid_ymd(y, x, d))
        return 0;
    dt = dt_from_ymd(y, x, d);
    goto finish;

#ifndef DT_PARSE_ISO_STRICT
  yqd:
    if (!dt_valid_yqd(y, x, d))
        return 0;
    dt = dt_from_yqd(y, x, d);
    goto finish;
#endif

  ywd:
    if (!dt_valid_ywd(y, x, d))
        return 0;
    dt = dt_from_ywd(y, x, d);

  finish:
#ifndef DT_PARSE_ISO_YEAR0
    if (y < 1)
        return 0;
#endif
    if (dtp)
        *dtp = dt;
    return n;
}