2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-10-11 16:42:46 +02:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-09-13 14:31:13 +02:00
|
|
|
#include "tests.h"
|
2019-03-27 11:32:41 +01:00
|
|
|
#include "time-util.h"
|
2012-10-11 16:42:46 +02:00
|
|
|
|
2015-10-15 01:56:57 +02:00
|
|
|
static void test_should_pass(const char *p) {
|
2012-11-23 22:52:46 +01:00
|
|
|
usec_t t, q;
|
2017-07-11 19:12:48 +02:00
|
|
|
char buf[FORMAT_TIMESTAMP_MAX], buf_relative[FORMAT_TIMESTAMP_RELATIVE_MAX];
|
2012-10-11 16:42:46 +02:00
|
|
|
|
2017-07-08 21:59:07 +02:00
|
|
|
log_info("Test: %s", p);
|
2012-11-23 22:52:46 +01:00
|
|
|
assert_se(parse_timestamp(p, &t) >= 0);
|
2020-06-19 12:24:09 +02:00
|
|
|
assert_se(format_timestamp_style(buf, sizeof(buf), t, TIMESTAMP_US));
|
2017-05-17 11:40:49 +02:00
|
|
|
log_info("\"%s\" → \"%s\"", p, buf);
|
2012-10-11 16:42:46 +02:00
|
|
|
|
2012-11-23 22:52:46 +01:00
|
|
|
assert_se(parse_timestamp(buf, &q) >= 0);
|
2017-09-17 09:50:52 +02:00
|
|
|
if (q != t) {
|
|
|
|
char tmp[FORMAT_TIMESTAMP_MAX];
|
|
|
|
|
|
|
|
log_error("round-trip failed: \"%s\" → \"%s\"",
|
2020-06-19 12:24:09 +02:00
|
|
|
buf, format_timestamp_style(tmp, sizeof(tmp), q, TIMESTAMP_US));
|
2017-09-17 09:50:52 +02:00
|
|
|
}
|
2012-11-23 22:52:46 +01:00
|
|
|
assert_se(q == t);
|
2012-10-11 16:42:46 +02:00
|
|
|
|
2017-07-11 19:12:48 +02:00
|
|
|
assert_se(format_timestamp_relative(buf_relative, sizeof(buf_relative), t));
|
2014-09-30 23:30:30 +02:00
|
|
|
log_info("%s", strna(buf_relative));
|
2012-11-23 22:52:46 +01:00
|
|
|
}
|
2012-10-11 16:42:46 +02:00
|
|
|
|
2015-10-25 17:25:58 +01:00
|
|
|
static void test_should_parse(const char *p) {
|
|
|
|
usec_t t;
|
|
|
|
|
2017-07-08 21:59:07 +02:00
|
|
|
log_info("Test: %s", p);
|
2015-10-25 17:25:58 +01:00
|
|
|
assert_se(parse_timestamp(p, &t) >= 0);
|
2017-07-08 21:59:07 +02:00
|
|
|
log_info("\"%s\" → \"@%" PRI_USEC "\"", p, t);
|
2015-10-25 17:25:58 +01:00
|
|
|
}
|
|
|
|
|
2015-10-15 01:56:57 +02:00
|
|
|
static void test_should_fail(const char *p) {
|
|
|
|
usec_t t;
|
2017-07-08 21:59:07 +02:00
|
|
|
int r;
|
2015-10-15 01:56:57 +02:00
|
|
|
|
2017-07-08 21:59:07 +02:00
|
|
|
log_info("Test: %s", p);
|
|
|
|
r = parse_timestamp(p, &t);
|
|
|
|
if (r >= 0)
|
|
|
|
log_info("\"%s\" → \"@%" PRI_USEC "\" (unexpected)", p, t);
|
|
|
|
else
|
|
|
|
log_info("parse_timestamp() returns %d (expected)", r);
|
|
|
|
assert_se(r < 0);
|
2015-10-15 01:56:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_one(const char *p) {
|
|
|
|
_cleanup_free_ char *with_utc;
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
with_utc = strjoin(p, " UTC");
|
2015-10-15 01:56:57 +02:00
|
|
|
test_should_pass(p);
|
|
|
|
test_should_pass(with_utc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_one_noutc(const char *p) {
|
|
|
|
_cleanup_free_ char *with_utc;
|
|
|
|
|
2016-10-23 17:43:27 +02:00
|
|
|
with_utc = strjoin(p, " UTC");
|
2015-10-15 01:56:57 +02:00
|
|
|
test_should_pass(p);
|
|
|
|
test_should_fail(with_utc);
|
|
|
|
}
|
|
|
|
|
2012-11-23 22:52:46 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
2018-09-13 14:31:13 +02:00
|
|
|
test_setup_logging(LOG_DEBUG);
|
2017-09-17 09:50:52 +02:00
|
|
|
|
2012-11-23 22:52:46 +01:00
|
|
|
test_one("17:41");
|
|
|
|
test_one("18:42:44");
|
2015-10-15 01:56:57 +02:00
|
|
|
test_one("18:42:44.0");
|
|
|
|
test_one("18:42:44.999999999999");
|
2012-11-23 22:52:46 +01:00
|
|
|
test_one("12-10-02 12:13:14");
|
|
|
|
test_one("12-10-2 12:13:14");
|
|
|
|
test_one("12-10-03 12:13");
|
|
|
|
test_one("2012-12-30 18:42");
|
|
|
|
test_one("2012-10-02");
|
|
|
|
test_one("Tue 2012-10-02");
|
|
|
|
test_one("yesterday");
|
|
|
|
test_one("today");
|
|
|
|
test_one("tomorrow");
|
2017-09-06 11:56:36 +02:00
|
|
|
test_one_noutc("16:20 UTC");
|
|
|
|
test_one_noutc("16:20 Asia/Seoul");
|
|
|
|
test_one_noutc("tomorrow Asia/Seoul");
|
|
|
|
test_one_noutc("2012-12-30 18:42 Asia/Seoul");
|
2017-07-11 19:12:48 +02:00
|
|
|
test_one_noutc("now");
|
2015-10-15 01:56:57 +02:00
|
|
|
test_one_noutc("+2d");
|
|
|
|
test_one_noutc("+2y 4d");
|
|
|
|
test_one_noutc("5months ago");
|
|
|
|
test_one_noutc("@1395716396");
|
2017-02-02 18:30:29 +01:00
|
|
|
test_should_parse("1970-1-1 UTC");
|
2017-07-11 19:12:48 +02:00
|
|
|
test_should_pass("1970-1-1 00:00:01 UTC");
|
|
|
|
test_should_fail("1969-12-31 UTC");
|
|
|
|
test_should_fail("-100y");
|
|
|
|
test_should_fail("today UTC UTC");
|
2017-09-06 11:56:36 +02:00
|
|
|
test_should_fail("now Asia/Seoul");
|
|
|
|
test_should_fail("+2d Asia/Seoul");
|
|
|
|
test_should_fail("@1395716396 Asia/Seoul");
|
2017-02-02 18:30:29 +01:00
|
|
|
#if SIZEOF_TIME_T == 8
|
2017-07-08 21:59:07 +02:00
|
|
|
test_should_pass("9999-12-30 23:59:59 UTC");
|
2017-02-02 18:30:29 +01:00
|
|
|
test_should_fail("9999-12-31 00:00:00 UTC");
|
|
|
|
test_should_fail("10000-01-01 00:00:00 UTC");
|
|
|
|
#elif SIZEOF_TIME_T == 4
|
2017-07-08 21:59:07 +02:00
|
|
|
test_should_pass("2038-01-19 03:14:07 UTC");
|
|
|
|
test_should_fail("2038-01-19 03:14:08 UTC");
|
2017-02-02 18:30:29 +01:00
|
|
|
#endif
|
2012-11-23 22:20:15 +01:00
|
|
|
|
2012-10-11 16:42:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|