コード例 #1
0
ファイル: end_of_week.c プロジェクト: chansen/c-dt
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_ywd(t.y, t.w, t.d);
            dt_t got = dt_end_of_week(src, t.dow);
            dt_t exp = dt_from_ywd(t.ey, t.ew, t.ed);
            cmp_ok(got, "==", exp, "dt_end_of_week(%d, %d)", src, t.dow);
        }

        {
            dt_t src = dt_from_ywd(t.y, t.w, t.d);
            dt_t dt1 = dt_end_of_week(src, t.dow);
            dt_t dt2 = dt_start_of_week(src, t.dow) + 6;
            cmp_ok(dt1, "==", dt2, "dt_end_of_week(%d, %d) == dt_start_of_week(%d, %d) + 6", 
              src, t.dow, src, t.dow);
        }

    }
    done_testing();
}
コード例 #2
0
ファイル: moment.c プロジェクト: gitpan/Time-Moment
static moment_t
THX_moment_with_week(pTHX_ const moment_t *mt, IV v) {
    int y, d;

    dt_to_ywd(moment_local_dt(mt), &y, NULL, &d);
    if (v < 1 || v > 52) {
        int wiy = dt_weeks_in_year(y);
        if (v < 1 || v > wiy)
            croak("Parameter 'week' is out of the range [1, %d]", wiy);
    }
    return THX_moment_with_local_dt(aTHX_ mt, dt_from_ywd(y, (int)v, d));
}
コード例 #3
0
ファイル: ywd.c プロジェクト: chansen/c-dt
int 
main() {
    int i, ntests;

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

        {
            dt_t got = dt_rdn(dt_from_ywd(t.y, t.w, t.d));
            cmp_ok(got, "==", t.rdn, "dt_rdn(dt_from_ywd(%d, %d, %d))", t.y, t.w, t.d);
        }

        {
            int y, w, d;
            dt_to_ywd(dt_from_rdn(t.rdn), &y, &w, &d);
            if (!ok((y == t.y && w == t.w && d == t.d), "dt_to_ywd(dt_from_rdn(%d))", t.rdn)) {
                diag("     got: %.4d-W%.2d-%d", y, w, d);
                diag("     exp: %.4d-W%.2d-%d", t.y, t.w, t.d);
            }
        }

        {
            int got = dt_woy(dt_from_rdn(t.rdn));
            cmp_ok(got, "==", t.w, "dt_woy(dt_from_rdn(%d))", t.rdn);
        }

        {
            int got = dt_yow(dt_from_rdn(t.rdn));
            cmp_ok(got, "==", t.y, "dt_yow(dt_from_rdn(%d))", t.rdn);
        }

        {
            int got = dt_dow(dt_from_rdn(t.rdn));
            cmp_ok(got, "==", t.d, "dt_dow(dt_from_rdn(%d))", t.rdn);
        }

        {
            ok(dt_valid_ywd(t.y, t.w, t.d), "dt_valid_ywd(%d, %d, %d)", t.y, t.w, t.d);
        }

    }
    done_testing();
}
コード例 #4
0
ファイル: dt_parse_iso.c プロジェクト: chansen/p5-time-moment
/*
 *  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;
}