Commit cfbc22ab authored by Lennart Poettering's avatar Lennart Poettering

journalctl: implement --since= and --until for filtering by time

parent fb5d873c
/test-date
/install-tree
/systemd-journal-gatewayd
/test-mmap-cache
......
......@@ -1157,14 +1157,16 @@ noinst_PROGRAMS += \
test-watchdog \
test-unit-name \
test-log \
test-unit-file
test-unit-file \
test-date
TESTS += \
test-job-type \
test-env-replace \
test-strv \
test-unit-name \
test-unit-file
test-unit-file \
test-data
test_engine_SOURCES = \
src/test/test-engine.c
......@@ -1226,6 +1228,12 @@ test_log_SOURCES = \
test_log_LDADD = \
libsystemd-core.la
test_date_SOURCES = \
src/test/test-date.c
test_date_LDADD = \
libsystemd-core.la
test_daemon_SOURCES = \
src/test/test-daemon.c
......
......@@ -19,6 +19,8 @@ F18:
Features:
* _SOURCE_MONOTONIC_TIMESTAMP entries from the kernel seem to be off by 1000000
* document unit_name_mangle()
* add new command to systemctl: "systemctl system-reexec" which reexecs as many daemons as virtually possible
......
......@@ -264,11 +264,42 @@
<term><option>--cursor=</option></term>
<term><option>-c</option></term>
<listitem><para>Jump to the location
in the journal specified by the passed
<listitem><para>Start showing entries
from the location in the journal
specified by the passed
cursor.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--since=</option></term>
<term><option>--until=</option></term>
<listitem><para>Start showing entries
newer or of the specified date,
resp. older or of the specified
date. Date specifications should be of
the format "2012-10-30 18:17:16". If
the time part is omitted, 00:00:00 is
assumed. If only the seconds component
is omitted, :00 is assumed. If the
date component is ommitted, the
current day is assumed. Alternatively
the strings
<literal>yesterday</literal>,
<literal>today</literal>,
<literal>tomorrow</literal> are
understood, which refer to 00:00:00 of
the day before the current day, the
current day, resp the day after the
current day. <literal>now</literal>
refers to the current time. Finally,
relative times may be specified,
prefixed with <literal>-</literal> or
<literal>+</literal>, referring to
times before resp. after the current
time.</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--directory=</option></term>
<term><option>-D</option></term>
......
This diff is collapsed.
......@@ -2733,16 +2733,31 @@ int parse_usec(const char *t, usec_t *usec) {
const char *suffix;
usec_t usec;
} table[] = {
{ "seconds", USEC_PER_SEC },
{ "second", USEC_PER_SEC },
{ "sec", USEC_PER_SEC },
{ "s", USEC_PER_SEC },
{ "minutes", USEC_PER_MINUTE },
{ "minute", USEC_PER_MINUTE },
{ "min", USEC_PER_MINUTE },
{ "months", USEC_PER_MONTH },
{ "month", USEC_PER_MONTH },
{ "msec", USEC_PER_MSEC },
{ "ms", USEC_PER_MSEC },
{ "m", USEC_PER_MINUTE },
{ "hours", USEC_PER_HOUR },
{ "hour", USEC_PER_HOUR },
{ "hr", USEC_PER_HOUR },
{ "h", USEC_PER_HOUR },
{ "days", USEC_PER_DAY },
{ "day", USEC_PER_DAY },
{ "d", USEC_PER_DAY },
{ "weeks", USEC_PER_WEEK },
{ "week", USEC_PER_WEEK },
{ "w", USEC_PER_WEEK },
{ "msec", USEC_PER_MSEC },
{ "ms", USEC_PER_MSEC },
{ "m", USEC_PER_MINUTE },
{ "years", USEC_PER_YEAR },
{ "year", USEC_PER_YEAR },
{ "y", USEC_PER_YEAR },
{ "usec", 1ULL },
{ "us", 1ULL },
{ "", USEC_PER_SEC }, /* default is sec */
......@@ -2796,16 +2811,31 @@ int parse_nsec(const char *t, nsec_t *nsec) {
const char *suffix;
nsec_t nsec;
} table[] = {
{ "seconds", NSEC_PER_SEC },
{ "second", NSEC_PER_SEC },
{ "sec", NSEC_PER_SEC },
{ "s", NSEC_PER_SEC },
{ "minutes", NSEC_PER_MINUTE },
{ "minute", NSEC_PER_MINUTE },
{ "min", NSEC_PER_MINUTE },
{ "months", NSEC_PER_MONTH },
{ "month", NSEC_PER_MONTH },
{ "msec", NSEC_PER_MSEC },
{ "ms", NSEC_PER_MSEC },
{ "m", NSEC_PER_MINUTE },
{ "hours", NSEC_PER_HOUR },
{ "hour", NSEC_PER_HOUR },
{ "hr", NSEC_PER_HOUR },
{ "h", NSEC_PER_HOUR },
{ "days", NSEC_PER_DAY },
{ "day", NSEC_PER_DAY },
{ "d", NSEC_PER_DAY },
{ "weeks", NSEC_PER_WEEK },
{ "week", NSEC_PER_WEEK },
{ "w", NSEC_PER_WEEK },
{ "msec", NSEC_PER_MSEC },
{ "ms", NSEC_PER_MSEC },
{ "m", NSEC_PER_MINUTE },
{ "years", NSEC_PER_YEAR },
{ "year", NSEC_PER_YEAR },
{ "y", NSEC_PER_YEAR },
{ "usec", NSEC_PER_USEC },
{ "us", NSEC_PER_USEC },
{ "nsec", 1ULL },
......@@ -5888,3 +5918,136 @@ bool string_is_safe(const char *p) {
return true;
}
int parse_timestamp(const char *t, usec_t *usec) {
const char *k;
struct tm tm, copy;
time_t x;
usec_t plus = 0, minus = 0, ret;
int r;
/*
* Allowed syntaxes:
*
* 2012-09-22 16:34:22
* 2012-09-22 16:34 (seconds will be set to 0)
* 2012-09-22 (time will be set to 00:00:00)
* 16:34:22 (date will be set to today)
* 16:34 (date will be set to today, seconds to 0)
* now
* yesterday (time is set to 00:00:00)
* today (time is set to 00:00:00)
* tomorrow (time is set to 00:00:00)
* +5min
* -5days
*
*/
assert(t);
assert(usec);
x = time(NULL);
assert_se(localtime_r(&x, &tm));
if (streq(t, "now"))
goto finish;
else if (streq(t, "today")) {
tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
goto finish;
} else if (streq(t, "yesterday")) {
tm.tm_mday --;
tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
goto finish;
} else if (streq(t, "tomorrow")) {
tm.tm_mday ++;
tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
goto finish;
} else if (t[0] == '+') {
r = parse_usec(t+1, &plus);
if (r < 0)
return r;
goto finish;
} else if (t[0] == '-') {
r = parse_usec(t+1, &minus);
if (r < 0)
return r;
goto finish;
}
copy = tm;
k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
if (k && *k == 0)
goto finish;
tm = copy;
k = strptime(t, "%Y-%m-%d %H:%M:%S", &tm);
if (k && *k == 0)
goto finish;
tm = copy;
k = strptime(t, "%y-%m-%d %H:%M", &tm);
if (k && *k == 0) {
tm.tm_sec = 0;
goto finish;
}
tm = copy;
k = strptime(t, "%Y-%m-%d %H:%M", &tm);
if (k && *k == 0) {
tm.tm_sec = 0;
goto finish;
}
tm = copy;
k = strptime(t, "%y-%m-%d", &tm);
if (k && *k == 0) {
tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
goto finish;
}
tm = copy;
k = strptime(t, "%Y-%m-%d", &tm);
if (k && *k == 0) {
tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
goto finish;
}
tm = copy;
k = strptime(t, "%H:%M:%S", &tm);
if (k && *k == 0)
goto finish;
tm = copy;
k = strptime(t, "%H:%M", &tm);
if (k && *k == 0) {
tm.tm_sec = 0;
goto finish;
}
return -EINVAL;
finish:
x = mktime(&tm);
if (x == (time_t) -1)
return -EINVAL;
ret = (usec_t) x * USEC_PER_SEC;
ret += plus;
if (ret > minus)
ret -= minus;
else
ret = 0;
*usec = ret;
return 0;
}
......@@ -561,3 +561,5 @@ _malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b)
bool filename_is_safe(const char *p);
bool string_is_safe(const char *p);
int parse_timestamp(const char *t, usec_t *usec);
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "util.h"
int main(int argc, char *argv[]) {
usec_t t;
char buf[FORMAT_TIMESTAMP_MAX];
assert_se(parse_timestamp("17:41", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("18:42:44", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("12-10-02 12:13:14", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("12-10-2 12:13:14", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("12-10-03 12:13", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("2012-12-30 18:42", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("2012-10-02", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("now", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("yesterday", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("today", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("tomorrow", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("+2d", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
assert_se(parse_timestamp("+2y 4d", &t) >= 0);
log_info("%s", format_timestamp(buf, sizeof(buf), t));
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment