time-util.c revision decad9103eab4c5f24dbc55dc7d2cdabce87a302
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
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 <time.h>
#include <string.h>
#include "util.h"
#include "time-util.h"
return timespec_load(&ts);
}
return ts;
}
if (u == 0)
else {
else
}
return ts;
}
return (usec_t) -1;
return (usec_t) -1;
return
}
if (u == (usec_t) -1) {
return ts;
}
return ts;
}
return (usec_t) -1;
return (usec_t) -1;
return
}
if (u == (usec_t) -1) {
return tv;
}
return tv;
}
assert(l > 0);
if (t <= 0)
return NULL;
return NULL;
return buf;
}
usec_t n, d;
n = now(CLOCK_REALTIME);
if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t)
return NULL;
d = n - t;
if (d >= USEC_PER_YEAR)
(unsigned long long) (d / USEC_PER_YEAR),
(unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH));
else if (d >= USEC_PER_MONTH)
(unsigned long long) (d / USEC_PER_MONTH),
(unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY));
else if (d >= USEC_PER_WEEK)
(unsigned long long) (d / USEC_PER_WEEK),
(unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY));
else if (d >= 2*USEC_PER_DAY)
else if (d >= 25*USEC_PER_HOUR)
(unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR));
else if (d >= 6*USEC_PER_HOUR)
(unsigned long long) (d / USEC_PER_HOUR));
else if (d >= USEC_PER_HOUR)
(unsigned long long) (d / USEC_PER_HOUR),
(unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE));
else if (d >= 5*USEC_PER_MINUTE)
(unsigned long long) (d / USEC_PER_MINUTE));
else if (d >= USEC_PER_MINUTE)
(unsigned long long) (d / USEC_PER_MINUTE),
(unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC));
else if (d >= USEC_PER_SEC)
(unsigned long long) (d / USEC_PER_SEC));
else if (d >= USEC_PER_MSEC)
(unsigned long long) (d / USEC_PER_MSEC));
else if (d > 0)
(unsigned long long) d);
else
buf[l-1] = 0;
return buf;
}
static const struct {
const char *suffix;
} table[] = {
{ "w", USEC_PER_WEEK },
{ "d", USEC_PER_DAY },
{ "h", USEC_PER_HOUR },
{ "min", USEC_PER_MINUTE },
{ "s", USEC_PER_SEC },
{ "ms", USEC_PER_MSEC },
{ "us", 1 },
};
unsigned i;
char *p = buf;
assert(l > 0);
if (t == (usec_t) -1)
return NULL;
if (t == 0) {
snprintf(p, l, "0");
p[l-1] = 0;
return p;
}
/* The result of this function can be parsed with parse_usec */
for (i = 0; i < ELEMENTSOF(table); i++) {
int k;
size_t n;
continue;
if (l <= 1)
break;
k = snprintf(p, l, "%s%llu%s", p > buf ? " " : "", (unsigned long long) (t / table[i].usec), table[i].suffix);
l -= n;
p += n;
}
*p = 0;
return buf;
}
assert(f);
assert(t);
if (!dual_timestamp_is_set(t))
return;
fprintf(f, "%s=%llu %llu\n",
name,
(unsigned long long) t->realtime,
(unsigned long long) t->monotonic);
}
unsigned long long a, b;
assert(t);
else {
t->realtime = a;
t->monotonic = b;
}
}
const char *k;
time_t x;
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);
if (streq(t, "now"))
goto finish;
else if (streq(t, "today")) {
goto finish;
} else if (streq(t, "yesterday")) {
goto finish;
} else if (streq(t, "tomorrow")) {
goto finish;
} else if (t[0] == '+') {
if (r < 0)
return r;
goto finish;
} else if (t[0] == '-') {
if (r < 0)
return r;
goto finish;
} else if (endswith(t, " ago")) {
_cleanup_free_ char *z;
if (!z)
return -ENOMEM;
r = parse_usec(z, &minus);
if (r < 0)
return r;
goto finish;
}
if (k && *k == 0)
goto finish;
if (k && *k == 0)
goto finish;
if (k && *k == 0) {
goto finish;
}
if (k && *k == 0) {
goto finish;
}
if (k && *k == 0) {
goto finish;
}
if (k && *k == 0) {
goto finish;
}
if (k && *k == 0)
goto finish;
if (k && *k == 0) {
goto finish;
}
return -EINVAL;
if (x == (time_t) -1)
return -EINVAL;
else
ret = 0;
return 0;
}
static const struct {
const char *suffix;
} 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 },
{ "years", USEC_PER_YEAR },
{ "year", USEC_PER_YEAR },
{ "y", USEC_PER_YEAR },
{ "usec", 1ULL },
{ "us", 1ULL },
};
const char *p;
usec_t r = 0;
assert(t);
p = t;
do {
long long l;
char *e;
unsigned i;
errno = 0;
l = strtoll(p, &e, 10);
if (errno != 0)
return -errno;
if (l < 0)
return -ERANGE;
if (e == p)
return -EINVAL;
e += strspn(e, WHITESPACE);
for (i = 0; i < ELEMENTSOF(table); i++)
break;
}
if (i >= ELEMENTSOF(table))
return -EINVAL;
} while (*p != 0);
*usec = r;
return 0;
}
static const struct {
const char *suffix;
} 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 },
{ "years", NSEC_PER_YEAR },
{ "year", NSEC_PER_YEAR },
{ "y", NSEC_PER_YEAR },
{ "usec", NSEC_PER_USEC },
{ "us", NSEC_PER_USEC },
{ "nsec", 1ULL },
{ "ns", 1ULL },
{ "", 1ULL }, /* default is nsec */
};
const char *p;
nsec_t r = 0;
assert(t);
p = t;
do {
long long l;
char *e;
unsigned i;
errno = 0;
l = strtoll(p, &e, 10);
if (errno != 0)
return -errno;
if (l < 0)
return -ERANGE;
if (e == p)
return -EINVAL;
e += strspn(e, WHITESPACE);
for (i = 0; i < ELEMENTSOF(table); i++)
break;
}
if (i >= ELEMENTSOF(table))
return -EINVAL;
} while (*p != 0);
*nsec = r;
return 0;
}