Also adds a few tests for the absolute cases of parse_timestamp. Suggested by: Mantas Mikulėnas <graw...@gmail.com> --- src/shared/time-util.c | 10 ++++++++++ src/test/test-time.c | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+)
diff --git a/src/shared/time-util.c b/src/shared/time-util.c index faa3418..fe43404 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -432,6 +432,7 @@ int parse_timestamp(const char *t, usec_t *usec) { * tomorrow (time is set to 00:00:00) * +5min * -5days + * @1395584178 (seconds from the epoch) * */ @@ -473,7 +474,16 @@ int parse_timestamp(const char *t, usec_t *usec) { return r; goto finish; + } else if (t[0] == '@') { + time_t epoch; + r = safe_atoli(t+1, &epoch); + if (r < 0) + return r; + + assert_se(localtime_r(&epoch, &tm)); + + goto finish; } else if (endswith(t, " ago")) { _cleanup_free_ char *z; diff --git a/src/test/test-time.c b/src/test/test-time.c index 36a3304..396111d 100644 --- a/src/test/test-time.c +++ b/src/test/test-time.c @@ -126,9 +126,30 @@ static void test_format_timespan(usec_t accuracy) { test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy); } +static void test_parse_timestamp_one(const char *timestamp, usec_t expected) { + usec_t result = 0; + + parse_timestamp(timestamp, &result); + printf("timestamp=%s, result=%" PRIu64 "\n", timestamp, result); + + assert_se(expected == result); +} + +static void test_parse_timestamp(void) { + test_parse_timestamp_one("2012-09-22 16:34:22", 1348346062000000); + test_parse_timestamp_one("2012-09-22 16:34", 1348346040000000); + test_parse_timestamp_one("2012-09-22", 1348286400000000); + test_parse_timestamp_one("2012-09", 0); + test_parse_timestamp_one("@1234567890", 1234567890000000); + test_parse_timestamp_one("@1234567890 sec", 0); + test_parse_timestamp_one("1234567890 sec", 0); + test_parse_timestamp_one("1234567890", 0); +} + int main(int argc, char *argv[]) { test_parse_sec(); test_parse_nsec(); + test_parse_timestamp(); test_format_timespan(1); test_format_timespan(USEC_PER_MSEC); test_format_timespan(USEC_PER_SEC); -- 1.9.1 _______________________________________________ systemd-devel mailing list systemd-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/systemd-devel