Permalink
Browse files

util-lib: make timestamp generation and parsing reversible (#3869)

This patch improves parsing and generation of timestamps and calendar
specifications in two ways:

- The week day is now always printed in the abbreviated English form, instead
  of the locale's setting. This makes sure we can always parse the week day
  again, even if the locale is changed. Given that we don't follow locale
  settings for printing timestamps in any other way either (for example, we
  always use 24h syntax in order to make uniform parsing possible), it only
  makes sense to also stick to a generic, non-localized form for the timestamp,
  too.

- When parsing a timestamp, the local timezone (in its DST or non-DST name)
  may be specified, in addition to "UTC". Other timezones are still not
  supported however (not because we wouldn't want to, but mostly because libc
  offers no nice API for that). In itself this brings no new features, however
  it ensures that any locally formatted timestamp's timezone is also parsable
  again.

These two changes ensure that the output of format_timestamp() may always be
passed to parse_timestamp() and results in the original input. The related
flavours for usec/UTC also work accordingly. Calendar specifications are
extended in a similar way.

The man page is updated accordingly, in particular this removes the claim that
timestamps systemd prints wouldn't be parsable by systemd. They are now.

The man page previously showed invalid timestamps as examples. This has been
removed, as the man page shouldn't be a unit test, where such negative examples
would be useful. The man page also no longer mentions the names of internal
functions, such as format_timestamp_us() or UNIX error codes such as EINVAL.
  • Loading branch information...
poettering authored and keszybz committed Aug 3, 2016
1 parent 7f5da8b commit 21b3a0fcd1fc4e4c668c4d34115e2e411dc0dceb
Showing with 307 additions and 104 deletions.
  1. +72 βˆ’78 man/systemd.time.xml
  2. +41 βˆ’2 src/basic/calendarspec.c
  3. +1 βˆ’0 src/basic/calendarspec.h
  4. +123 βˆ’23 src/basic/time-util.c
  5. +3 βˆ’1 src/basic/time-util.h
  6. +23 βˆ’0 src/test/test-calendarspec.c
  7. +44 βˆ’0 src/test/test-time.c
View
@@ -57,14 +57,13 @@
<refsect1>
<title>Displaying Time Spans</title>
<para>Time spans refer to time durations. On display, systemd will
present time spans as a space-separated series of time values each
suffixed by a time unit.</para>
<para>Time spans refer to time durations. On display, systemd will present time spans as a space-separated series
of time values each suffixed by a time unit. Example:</para>
<programlisting>2h 30min</programlisting>
<para>All specified time values are meant to be added up. The
above hence refers to 150 minutes.</para>
<para>All specified time values are meant to be added up. The above hence refers to 150 minutes. Display is
locale-independent, only English names for the time units are used.</para>
</refsect1>
<refsect1>
@@ -83,13 +82,13 @@
<listitem><para>days, day, d</para></listitem>
<listitem><para>weeks, week, w</para></listitem>
<listitem><para>months, month, M (defined as 30.44 days)</para></listitem>
<listitem><para>years, year, y (define as 365.25 days)</para></listitem>
<listitem><para>years, year, y (defined as 365.25 days)</para></listitem>
</itemizedlist>
<para>If no time unit is specified, generally seconds are assumed,
but some exceptions exist and are marked as such. In a few cases
<literal>ns</literal>, <literal>nsec</literal> is accepted too,
where the granularity of the time span allows for this.</para>
<para>If no time unit is specified, generally seconds are assumed, but some exceptions exist and are marked as
such. In a few cases <literal>ns</literal>, <literal>nsec</literal> is accepted too, where the granularity of the
time span permits this. Parsing is generally locale-independent, non-English names for the time units are not
accepted.</para>
<para>Examples for valid time span specifications:</para>
@@ -110,30 +109,29 @@
<programlisting>Fri 2012-11-23 23:02:15 CET</programlisting>
<para>The weekday is printed according to the locale choice of the
user.</para>
<para>The weekday is printed in the abbreviated English language form. The formatting is locale-independent.</para>
<para>In some cases timestamps are shown in the UTC timezone instead of the local timezone, which is indicated via
the <literal>UTC</literal> timezone specifier in the output.</para>
<para>In some cases timestamps are shown with microsecond granularity. In this case the sub-second remainder is
separated by a full stop from the seconds component.</para>
</refsect1>
<refsect1>
<title>Parsing Timestamps</title>
<para>When parsing, systemd will accept a similar syntax, but
expects no timezone specification, unless it is given as the
literal string "UTC". In this case, the time is considered in UTC,
otherwise in the local timezone. The weekday specification is
optional, but when the weekday is specified, it must either be in
the abbreviated (<literal>Wed</literal>) or non-abbreviated
(<literal>Wednesday</literal>) English language form (case does
not matter), and is not subject to the locale choice of the user.
Either the date, or the time part may be omitted, in which case
the current date or 00:00:00, respectively, is assumed. The seconds
component of the time may also be omitted, in which case ":00" is
assumed. Year numbers may be specified in full or may be
abbreviated (omitting the century).</para>
<para>A timestamp is considered invalid if a weekday is specified
and the date does not actually match the specified day of the
week.</para>
<para>When parsing, systemd will accept a similar syntax, but expects no timezone specification, unless it is given
as the literal string <literal>UTC</literal> (for the UTC timezone) or is specified to be the locally configured
timezone. Other timezones than the local and UTC are not supported. The weekday specification is optional, but when
the weekday is specified, it must either be in the abbreviated (<literal>Wed</literal>) or non-abbreviated
(<literal>Wednesday</literal>) English language form (case does not matter), and is not subject to the locale
choice of the user. Either the date, or the time part may be omitted, in which case the current date or 00:00:00,
respectively, is assumed. The seconds component of the time may also be omitted, in which case ":00" is
assumed. Year numbers may be specified in full or may be abbreviated (omitting the century).</para>
<para>A timestamp is considered invalid if a weekday is specified and the date does not match the specified day of
the week.</para>
<para>When parsing, systemd will also accept a few special
placeholders instead of timestamps: <literal>now</literal> may be
@@ -167,37 +165,32 @@
2012-11-23 β†’ Fri 2012-11-23 00:00:00
12-11-23 β†’ Fri 2012-11-23 00:00:00
11:12:13 β†’ Fri 2012-11-23 11:12:13
11:12:13.9900009 β†’ Fri 2012-11-23 11:12:13
format_timestamp_us: Fri 2012-11-23 11:12:13.990000
11:12 β†’ Fri 2012-11-23 11:12:00
now β†’ Fri 2012-11-23 18:15:22
today β†’ Fri 2012-11-23 00:00:00
today UTC β†’ Fri 2012-11-23 16:00:00
yesterday β†’ Fri 2012-11-22 00:00:00
tomorrow β†’ Fri 2012-11-24 00:00:00
+3h30min β†’ Fri 2012-11-23 21:45:22
+3h30min UTC β†’ -EINVAL
-5s β†’ Fri 2012-11-23 18:15:17
11min ago β†’ Fri 2012-11-23 18:04:22
11min ago UTC β†’ -EINVAL
@1395716396 β†’ Tue 2014-03-25 03:59:56</programlisting>
<para>Note that timestamps printed by systemd will not be parsed
correctly by systemd, as the timezone specification is not
accepted, and printing timestamps is subject to locale settings
for the weekday, while parsing only accepts English weekday
names.</para>
<para>Note that timestamps displayed by remote systems with a non-matching timezone are usually not parsable
locally, as the timezone component is not understood (unless it happens to be <literal>UTC</literal>).</para>
<para>In some cases, systemd will display a relative timestamp
(relative to the current time, or the time of invocation of the
command) instead or in addition to an absolute timestamp as
described above. A relative timestamp is formatted as
follows:</para>
<para>Timestamps may also be specified with microsecond granularity. The sub-second remainder is expected separated
by a full stop from the seconds component. Example:</para>
<programlisting>2014-03-25 03:59:56.654563</programlisting>
<para>In some cases, systemd will display a relative timestamp (relative to the current time, or the time of
invocation of the command) instead of or in addition to an absolute timestamp as described above. A relative
timestamp is formatted as follows:</para>
<para>2 months 5 days ago</para>
<programlisting>2 months 5 days ago</programlisting>
<para>Note that any relative timestamp will also parse correctly
where a timestamp is expected. (see above)</para>
<para>Note that a relative timestamp is also accepted where a timestamp is expected (see above).</para>
</refsect1>
<refsect1>
@@ -239,8 +232,9 @@
second component is not specified, <literal>:00</literal> is
assumed.</para>
<para>A timezone specification is not expected, unless it is given
as the literal string "UTC", similarly to timestamps.</para>
<para>A timezone specification is not expected, unless it is given as the literal string <literal>UTC</literal>, or
the local timezone, similar to the supported syntax of timestamps (see above). Non-local timezones except for UTC
are not supported.</para>
<para>The special expressions
<literal>minutely</literal>,
@@ -263,38 +257,38 @@
<para>Examples for valid timestamps and their
normalized form:</para>
<programlisting> Sat,Thu,Mon..Wed,Sat..Sun β†’ Mon..Thu,Sat,Sun *-*-* 00:00:00
Mon,Sun 12-*-* 2,1:23 β†’ Mon,Sun 2012-*-* 01,02:23:00
Wed *-1 β†’ Wed *-*-01 00:00:00
<programlisting> Sat,Thu,Mon..Wed,Sat..Sun β†’ Mon..Thu,Sat,Sun *-*-* 00:00:00
Mon,Sun 12-*-* 2,1:23 β†’ Mon,Sun 2012-*-* 01,02:23:00
Wed *-1 β†’ Wed *-*-01 00:00:00
Wed..Wed,Wed *-1 β†’ Wed *-*-01 00:00:00
Wed, 17:48 β†’ Wed *-*-* 17:48:00
Wed, 17:48 β†’ Wed *-*-* 17:48:00
Wed..Sat,Tue 12-10-15 1:2:3 β†’ Tue..Sat 2012-10-15 01:02:03
*-*-7 0:0:0 β†’ *-*-07 00:00:00
10-15 β†’ *-10-15 00:00:00
monday *-12-* 17:00 β†’ Mon *-12-* 17:00:00
Mon,Fri *-*-3,1,2 *:30:45 β†’ Mon,Fri *-*-01,02,03 *:30:45
12,14,13,12:20,10,30 β†’ *-*-* 12,13,14:10,20,30:00
12..14:10,20,30 β†’ *-*-* 12,13,14:10,20,30:00
mon,fri *-1/2-1,3 *:30:45 β†’ Mon,Fri *-01/2-01,03 *:30:45
03-05 08:05:40 β†’ *-03-05 08:05:40
08:05:40 β†’ *-*-* 08:05:40
05:40 β†’ *-*-* 05:40:00
Sat,Sun 12-05 08:05:40 β†’ Sat,Sun *-12-05 08:05:40
Sat,Sun 08:05:40 β†’ Sat,Sun *-*-* 08:05:40
2003-03-05 05:40 β†’ 2003-03-05 05:40:00
05:40:23.4200004/3.1700005 β†’ 05:40:23.420000/3.170001
2003-02..04-05 β†’ 2003-02,03,04-05 00:00:00
2003-03-05 05:40 UTC β†’ 2003-03-05 05:40:00 UTC
2003-03-05 β†’ 2003-03-05 00:00:00
03-05 β†’ *-03-05 00:00:00
hourly β†’ *-*-* *:00:00
daily β†’ *-*-* 00:00:00
daily UTC β†’ *-*-* 00:00:00 UTC
monthly β†’ *-*-01 00:00:00
weekly β†’ Mon *-*-* 00:00:00
yearly β†’ *-01-01 00:00:00
annually β†’ *-01-01 00:00:00
*:2/3 β†’ *-*-* *:02/3:00</programlisting>
*-*-7 0:0:0 β†’ *-*-07 00:00:00
10-15 β†’ *-10-15 00:00:00
monday *-12-* 17:00 β†’ Mon *-12-* 17:00:00
Mon,Fri *-*-3,1,2 *:30:45 β†’ Mon,Fri *-*-01,02,03 *:30:45
12,14,13,12:20,10,30 β†’ *-*-* 12,13,14:10,20,30:00
12..14:10,20,30 β†’ *-*-* 12,13,14:10,20,30:00
mon,fri *-1/2-1,3 *:30:45 β†’ Mon,Fri *-01/2-01,03 *:30:45
03-05 08:05:40 β†’ *-03-05 08:05:40
08:05:40 β†’ *-*-* 08:05:40
05:40 β†’ *-*-* 05:40:00
Sat,Sun 12-05 08:05:40 β†’ Sat,Sun *-12-05 08:05:40
Sat,Sun 08:05:40 β†’ Sat,Sun *-*-* 08:05:40
2003-03-05 05:40 β†’ 2003-03-05 05:40:00
05:40:23.4200004/3.1700005 β†’ 05:40:23.420000/3.170001
2003-02..04-05 β†’ 2003-02,03,04-05 00:00:00
2003-03-05 05:40 UTC β†’ 2003-03-05 05:40:00 UTC
2003-03-05 β†’ 2003-03-05 00:00:00
03-05 β†’ *-03-05 00:00:00
hourly β†’ *-*-* *:00:00
daily β†’ *-*-* 00:00:00
daily UTC β†’ *-*-* 00:00:00 UTC
monthly β†’ *-*-01 00:00:00
weekly β†’ Mon *-*-* 00:00:00
yearly β†’ *-01-01 00:00:00
annually β†’ *-01-01 00:00:00
*:2/3 β†’ *-*-* *:02/3:00</programlisting>
<para>Calendar events are used by timer units, see
<citerefentry><refentrytitle>systemd.timer</refentrytitle><manvolnum>5</manvolnum></citerefentry>
View
@@ -302,6 +302,17 @@ int calendar_spec_to_string(const CalendarSpec *c, char **p) {
if (c->utc)
fputs(" UTC", f);
else if (IN_SET(c->dst, 0, 1)) {
/* If daylight saving is explicitly on or off, let's show the used timezone. */
tzset();
if (!isempty(tzname[c->dst])) {
fputc(' ', f);
fputs(tzname[c->dst], f);
}
}
r = fflush_and_check(f);
if (r < 0) {
@@ -747,9 +758,9 @@ static int parse_calendar_time(const char **p, CalendarSpec *c) {
}
int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
const char *utc;
CalendarSpec *c;
int r;
const char *utc;
assert(p);
assert(spec);
@@ -760,11 +771,39 @@ int calendar_spec_from_string(const char *p, CalendarSpec **spec) {
c = new0(CalendarSpec, 1);
if (!c)
return -ENOMEM;
c->dst = -1;
utc = endswith_no_case(p, " UTC");
if (utc) {
c->utc = true;
p = strndupa(p, utc - p);
} else {
const char *e = NULL;
int j;
tzset();
/* Check if the local timezone was specified? */
for (j = 0; j <= 1; j++) {
if (isempty(tzname[j]))
continue;
e = endswith_no_case(p, tzname[j]);
if(!e)
continue;
if (e == p)
continue;
if (e[-1] != ' ')
continue;
break;
}
/* Found one of the two timezones specified? */
if (IN_SET(j, 0, 1)) {
p = strndupa(p, e - p - 1);
c->dst = j;
}
}
if (strcaseeq(p, "minutely")) {
@@ -1017,7 +1056,7 @@ static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
for (;;) {
/* Normalize the current date */
(void) mktime_or_timegm(&c, spec->utc);
c.tm_isdst = -1;
c.tm_isdst = spec->dst;
c.tm_year += 1900;
r = find_matching_component(spec->year, &c.tm_year);
View
@@ -37,6 +37,7 @@ typedef struct CalendarComponent {
typedef struct CalendarSpec {
int weekdays_bits;
bool utc;
int dst;
CalendarComponent *year;
CalendarComponent *month;
Oops, something went wrong.

0 comments on commit 21b3a0f

Please sign in to comment.