analyze: add 'timestamp' verb

We had 'calendar' and 'timespan', but the third one was missing.
Also consistently order the verbs as calendar/timestamp/timespan in help.

The output from 'timespan' is highlighted more.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1711065.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-05-17 09:39:22 +02:00
parent b99f4d484b
commit 2cae4711f3
3 changed files with 95 additions and 9 deletions

View file

@ -93,7 +93,13 @@
<command>systemd-analyze</command>
<arg choice="opt" rep="repeat">OPTIONS</arg>
<arg choice="plain">calendar</arg>
<arg choice="plain" rep="repeat"><replaceable>SPECS</replaceable></arg>
<arg choice="plain" rep="repeat"><replaceable>SPEC</replaceable></arg>
</cmdsynopsis>
<cmdsynopsis>
<command>systemd-analyze</command>
<arg choice="opt" rep="repeat">OPTIONS</arg>
<arg choice="plain">timestamp</arg>
<arg choice="plain" rep="repeat"><replaceable>TIMESTAMP</replaceable></arg>
</cmdsynopsis>
<cmdsynopsis>
<command>systemd-analyze</command>
@ -360,7 +366,8 @@ $ eog targets.svg</programlisting>
<citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>. By
default, only the next time the calendar expression will elapse is shown; use
<option>--iterations=</option> to show the specified number of next times the expression
elapses.</para>
elapses. Each time the expression elapses forms a timestamp, see the <command>timestamp</command>
verb below.</para>
<example>
<title>Show leap days in the near future</title>
@ -382,13 +389,44 @@ Normalized form: *-02-29 00:00:00
</example>
</refsect2>
<refsect2>
<title><command>systemd-analyze timestamp <replaceable>TIMESTAMP</replaceable>...</command></title>
<para>This command parses a timestamp (i.e. a single point in time) and outputs the normalized form and
the difference between this timestamp and now. The timestamp should adhere to the syntax documented in
<citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
section "PARSING TIMESTAMPS".</para>
<example>
<title>Show parsing of timestamps</title>
<programlisting>$ systemd-analyze timestamp yesterday now tomorrow
Original form: yesterday
Normalized form: Thu 2019-05-16 00:00:00 CEST
(in UTC): Wed 2019-05-15 22:00:00 UTC
From now: 1 day 9h ago
Original form: now
Normalized form: Fri 2019-05-17 09:34:14 CEST
(in UTC): Fri 2019-05-17 07:34:14 UTC
From now: 32us ago
Original form: tomorrow
Normalized form: Sat 2019-05-18 00:00:00 CEST
(in UTC): Fri 2019-05-17 22:00:00 UTC
From now: 14h left
</programlisting>
</example>
</refsect2>
<refsect2>
<title><command>systemd-analyze timespan <replaceable>EXPRESSION</replaceable>...</command></title>
<para>This command parses a time span and outputs the normalized form and the equivalent value in
microseconds. The time span should adhere to the same syntax documented in
<citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
Values without associated magnitudes are parsed as seconds.</para>
<para>This command parses a time span (i.e. a difference between two timestamps) and outputs the
normalized form and the equivalent value in microseconds. The time span should adhere to the syntax
documented in
<citerefentry><refentrytitle>systemd.time</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
section "PARSING TIME SPANS". Values without units are parsed as seconds.</para>
<example>
<title>Show parsing of timespans</title>

View file

@ -173,6 +173,10 @@ tomorrow Pacific/Auckland → Thu 2012-11-23 19:00:00
<programlisting>2 months 5 days ago</programlisting>
<para>Note that a relative timestamp is also accepted where a timestamp is expected (see above).</para>
<para>Use the <command>timestamp</command> command of
<citerefentry><refentrytitle>systemd-analyze</refentrytitle><manvolnum>1</manvolnum></citerefentry> to
validate and normalize timestamps for testing purposes.</para>
</refsect1>
<refsect1>

View file

@ -1687,7 +1687,10 @@ static int dump_timespan(int argc, char *argv[], void *userdata) {
printf("Original: %s\n", *input_timespan);
printf(" %ss: %" PRIu64 "\n", special_glyph(SPECIAL_GLYPH_MU), output_usecs);
printf(" Human: %s\n", format_timespan(ft_buf, sizeof(ft_buf), output_usecs, usec_magnitude));
printf(" Human: %s%s%s\n",
ansi_highlight(),
format_timespan(ft_buf, sizeof(ft_buf), output_usecs, usec_magnitude),
ansi_normal());
if (input_timespan[1])
putchar('\n');
@ -1696,6 +1699,45 @@ static int dump_timespan(int argc, char *argv[], void *userdata) {
return EXIT_SUCCESS;
}
static int test_timestamp_one(const char *p) {
usec_t usec;
char buf[FORMAT_TIMESTAMP_MAX];
int r;
r = parse_timestamp(p, &usec);
if (r < 0)
return log_error_errno(r, "Failed to parse \"%s\": %m", p);
printf(" Original form: %s\n", p);
printf("Normalized form: %s%s%s\n",
ansi_highlight_blue(),
format_timestamp(buf, sizeof buf, usec),
ansi_normal());
if (!in_utc_timezone())
printf(" (in UTC): %s\n", format_timestamp_utc(buf, sizeof buf, usec));
printf(" From now: %s\n", format_timestamp_relative(buf, sizeof buf, usec));
return 0;
}
static int test_timestamp(int argc, char *argv[], void *userdata) {
int ret = 0, r;
char **p;
STRV_FOREACH(p, strv_skip(argv, 1)) {
r = test_timestamp_one(*p);
if (ret == 0 && r < 0)
ret = r;
if (*(p + 1))
putchar('\n');
}
return ret;
}
static int test_calendar_one(usec_t n, const char *p) {
_cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
_cleanup_free_ char *t = NULL;
@ -1887,8 +1929,9 @@ static int help(int argc, char *argv[], void *userdata) {
" unit-paths List load directories for units\n"
" syscall-filter [NAME...] Print list of syscalls in seccomp filter\n"
" verify FILE... Check unit files for correctness\n"
" calendar SPEC... Validate repetitive calendar time events\n"
" service-watchdogs [BOOL] Get/set service watchdog state\n"
" calendar SPEC... Validate repetitive calendar time events\n"
" timestamp TIMESTAMP... Validate a timestamp\n"
" timespan SPAN... Validate a time span\n"
" security [UNIT...] Analyze security of unit\n"
"\nSee the %s for details.\n"
@ -2089,8 +2132,9 @@ static int run(int argc, char *argv[]) {
{ "syscall-filter", VERB_ANY, VERB_ANY, 0, dump_syscall_filters },
{ "verify", 2, VERB_ANY, 0, do_verify },
{ "calendar", 2, VERB_ANY, 0, test_calendar },
{ "service-watchdogs", VERB_ANY, 2, 0, service_watchdogs },
{ "timestamp", 2, VERB_ANY, 0, test_timestamp },
{ "timespan", 2, VERB_ANY, 0, dump_timespan },
{ "service-watchdogs", VERB_ANY, 2, 0, service_watchdogs },
{ "security", VERB_ANY, VERB_ANY, 0, do_security },
{}
};