[systemd-devel] [PATCH] time-util: accept epoch timetamps prefixed with @
Dave Reisner
d at falconindy.com
Sun Mar 23 10:05:15 PDT 2014
On Sun, Mar 23, 2014 at 05:27:08PM +0100, Zbigniew Jędrzejewski-Szmek wrote:
> On Sun, Mar 23, 2014 at 11:06:47AM -0400, Dave Reisner wrote:
> > Also adds a few tests for the absolute cases of parse_timestamp.
> Yeah, that looks useful.
>
> You don't test negative values. Maybe you could an example with a negative
> value to the documentation and tests?
Negative epoch values? What would this represent?
> Zbyszek
>
>
> > Suggested by: Mantas Mikulėnas <grawity at 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 at lists.freedesktop.org
> > http://lists.freedesktop.org/mailman/listinfo/systemd-devel
More information about the systemd-devel
mailing list