示例#1
0
static void test_one(const char *p) {
        usec_t t, q;
        char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];

        assert_se(parse_timestamp(p, &t) >= 0);
        log_info("%s", format_timestamp(buf, sizeof(buf), t));

        /* Chop off timezone */
        *strrchr(buf, ' ') = 0;

        assert_se(parse_timestamp(buf, &q) >= 0);
        assert_se(q == t);

        log_info("%s", strna(format_timestamp_relative(buf_relative, sizeof(buf_relative), t)));
        assert_se(parse_timestamp(buf, &q) >= 0);
}
static void test_format_timestamp(void) {
        unsigned i;

        log_info("/* %s */", __func__);

        for (i = 0; i < 100; i++) {
                char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)];
                usec_t x, y;

                random_bytes(&x, sizeof(x));
                x = x % (2147483600 * USEC_PER_SEC) + 1;

                assert_se(format_timestamp(buf, sizeof(buf), x));
                log_info("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);

                assert_se(format_timestamp_utc(buf, sizeof(buf), x));
                log_info("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);

                assert_se(format_timestamp_us(buf, sizeof(buf), x));
                log_info("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x == y);

                assert_se(format_timestamp_us_utc(buf, sizeof(buf), x));
                log_info("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);
                assert_se(x == y);

                assert_se(format_timestamp_relative(buf, sizeof(buf), x));
                log_info("%s", buf);
                assert_se(parse_timestamp(buf, &y) >= 0);

                /* The two calls above will run with a slightly different local time. Make sure we are in the same
                 * range however, but give enough leeway that this is unlikely to explode. And of course,
                 * format_timestamp_relative() scales the accuracy with the distance from the current time up to one
                 * month, cover for that too. */
                assert_se(y > x ? y - x : x - y <= USEC_PER_MONTH + USEC_PER_DAY);
        }
}
示例#3
0
static void test_should_pass(const char *p) {
        usec_t t, q;
        char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX], *sp;

        assert_se(parse_timestamp(p, &t) >= 0);
        format_timestamp_us(buf, sizeof(buf), t);
        log_info("%s", buf);

        /* Chop off timezone */
        sp = strrchr(buf, ' ');
        assert_se(sp);
        *sp = 0;

        assert_se(parse_timestamp(buf, &q) >= 0);
        assert_se(q == t);

        format_timestamp_relative(buf_relative, sizeof(buf_relative), t);
        log_info("%s", strna(buf_relative));
        assert_se(parse_timestamp(buf, &q) >= 0);
}
示例#4
0
static void test_should_pass(const char *p) {
        usec_t t, q;
        char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];

        log_info("Test: %s", p);
        assert_se(parse_timestamp(p, &t) >= 0);
        assert_se(format_timestamp_us(buf, sizeof(buf), t));
        log_info("\"%s\" → \"%s\"", p, buf);

        assert_se(parse_timestamp(buf, &q) >= 0);
        if (q != t) {
                char tmp[FORMAT_TIMESTAMP_MAX];

                log_error("round-trip failed: \"%s\" → \"%s\"",
                          buf, format_timestamp_us(tmp, sizeof(tmp), q));
        }
        assert_se(q == t);

        assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
        log_info("%s", strna(buf_relative));
}