Exemple #1
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));
}
Exemple #2
0
static void test_hourly_bug_4031(void) {
        CalendarSpec *c;
        usec_t n, u, w;
        char buf[FORMAT_TIMESTAMP_MAX], zaf[FORMAT_TIMESTAMP_MAX];
        int r;

        assert_se(calendar_spec_from_string("hourly", &c) >= 0);
        n = now(CLOCK_REALTIME);
        assert_se((r = calendar_spec_next_usec(c, n, &u)) >= 0);

        printf("Now: %s (%"PRIu64")\n", format_timestamp_us(buf, sizeof buf, n), n);
        printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror(-r) : format_timestamp_us(buf, sizeof buf, u), u);

        assert_se((r = calendar_spec_next_usec(c, u, &w)) >= 0);
        printf("Next hourly: %s (%"PRIu64")\n", r < 0 ? strerror(-r) : format_timestamp_us(zaf, sizeof zaf, w), w);

        assert_se(n < u);
        assert_se(u <= n + USEC_PER_HOUR);
        assert_se(u < w);
        assert_se(w <= u + USEC_PER_HOUR);

        calendar_spec_free(c);
}
Exemple #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];

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

        /* Chop off timezone */
        *strrchr(buf, ' ') = 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);
}
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);
        }
}
Exemple #5
0
static void test_timestamp(void) {
        char buf[FORMAT_TIMESTAMP_MAX];
        _cleanup_free_ char *t = NULL;
        CalendarSpec *c;
        usec_t x, y;

        /* Ensure that a timestamp is also a valid calendar specification. Convert forth and back */

        x = now(CLOCK_REALTIME);

        assert_se(format_timestamp_us(buf, sizeof(buf), x));
        printf("%s\n", buf);
        assert_se(calendar_spec_from_string(buf, &c) >= 0);
        assert_se(calendar_spec_to_string(c, &t) >= 0);
        calendar_spec_free(c);
        printf("%s\n", t);

        assert_se(parse_timestamp(t, &y) >= 0);
        assert_se(y == x);
}
Exemple #6
0
static void test_next(const char *input, const char *new_tz, usec_t after, usec_t expect) {
        CalendarSpec *c;
        usec_t u;
        char *old_tz;
        char buf[FORMAT_TIMESTAMP_MAX];
        int r;

        old_tz = getenv("TZ");
        if (old_tz)
                old_tz = strdupa(old_tz);

        if (new_tz)
                assert_se(setenv("TZ", new_tz, 1) >= 0);
        else
                assert_se(unsetenv("TZ") >= 0);
        tzset();

        assert_se(calendar_spec_from_string(input, &c) >= 0);

        printf("\"%s\"\n", input);

        u = after;
        r = calendar_spec_next_usec(c, after, &u);
        printf("At: %s\n", r < 0 ? strerror(-r) : format_timestamp_us(buf, sizeof buf, u));
        if (expect != (usec_t)-1)
                assert_se(r >= 0 && u == expect);
        else
                assert(r == -ENOENT);

        calendar_spec_free(c);

        if (old_tz)
                assert_se(setenv("TZ", old_tz, 1) >= 0);
        else
                assert_se(unsetenv("TZ") >= 0);
        tzset();
}