time-util.c revision 11c3a36649e5e5e77db499c92f3cdcbd619efd3a
/*-*- 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 <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "log.h"
#include "macro.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
return timespec_load(&ts);
}
return timespec_load_nsec(&ts);
}
return ts;
}
if (u == USEC_INFINITY || u <= 0) {
return ts;
}
else
return ts;
}
if (u == USEC_INFINITY) {
return ts;
}
else
return ts;
}
if (u == USEC_INFINITY) {
return ts;
}
else
else
return ts;
}
return USEC_INFINITY;
return USEC_INFINITY;
return
}
return NSEC_INFINITY;
return
}
if (u == USEC_INFINITY) {
return ts;
}
return ts;
}
return USEC_INFINITY;
return USEC_INFINITY;
return
}
if (u == USEC_INFINITY) {
} else {
}
return tv;
}
assert(l > 0);
if (t <= 0 || t == USEC_INFINITY)
return NULL;
return NULL;
return buf;
}
return format_timestamp_internal(buf, l, t, false);
}
return format_timestamp_internal(buf, l, t, true);
}
assert(l > 0);
if (t <= 0 || t == USEC_INFINITY)
return NULL;
return NULL;
return NULL;
return buf;
}
return format_timestamp_internal_us(buf, l, t, false);
}
return format_timestamp_internal_us(buf, l, t, true);
}
const char *s;
usec_t n, d;
if (t <= 0 || t == USEC_INFINITY)
return NULL;
n = now(CLOCK_REALTIME);
if (n > t) {
d = n - t;
s = "ago";
} else {
d = t - n;
s = "left";
}
if (d >= USEC_PER_YEAR)
d / USEC_PER_YEAR,
(d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
else if (d >= USEC_PER_MONTH)
d / USEC_PER_MONTH,
(d % USEC_PER_MONTH) / USEC_PER_DAY, s);
else if (d >= USEC_PER_WEEK)
d / USEC_PER_WEEK,
(d % USEC_PER_WEEK) / USEC_PER_DAY, s);
else if (d >= 2*USEC_PER_DAY)
else if (d >= 25*USEC_PER_HOUR)
(d - USEC_PER_DAY) / USEC_PER_HOUR, s);
else if (d >= 6*USEC_PER_HOUR)
d / USEC_PER_HOUR, s);
else if (d >= USEC_PER_HOUR)
d / USEC_PER_HOUR,
(d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
else if (d >= 5*USEC_PER_MINUTE)
d / USEC_PER_MINUTE, s);
else if (d >= USEC_PER_MINUTE)
d / USEC_PER_MINUTE,
(d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
else if (d >= USEC_PER_SEC)
d / USEC_PER_SEC, s);
else if (d >= USEC_PER_MSEC)
d / USEC_PER_MSEC, s);
else if (d > 0)
d, s);
else
buf[l-1] = 0;
return buf;
}
static const struct {
const char *suffix;
} table[] = {
{ "y", USEC_PER_YEAR },
{ "month", USEC_PER_MONTH },
{ "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;
bool something = false;
assert(l > 0);
if (t == USEC_INFINITY) {
p[l-1] = 0;
return p;
}
if (t <= 0) {
p[l-1] = 0;
return p;
}
/* The result of this function can be parsed with parse_sec */
for (i = 0; i < ELEMENTSOF(table); i++) {
int k = 0;
size_t n;
bool done = false;
usec_t a, b;
if (t <= 0)
break;
break;
continue;
if (l <= 1)
break;
/* Let's see if we should shows this in dot notation */
if (t < USEC_PER_MINUTE && b > 0) {
int j;
j = 0;
j++;
b /= 10;
j--;
}
if (j > 0) {
k = snprintf(p, l,
a,
j,
(unsigned long long) b,
t = 0;
done = true;
}
}
/* No? Then let's show it normally */
if (!done) {
k = snprintf(p, l,
a,
t = b;
}
l -= n;
p += n;
something = true;
}
*p = 0;
return buf;
}
assert(f);
assert(t);
if (!dual_timestamp_is_set(t))
return;
name,
t->realtime,
t->monotonic);
}
unsigned long long a, b;
assert(t);
return -EINVAL;
}
t->realtime = a;
t->monotonic = b;
return 0;
}
static const struct {
const char *name;
const int nr;
} day_nr[] = {
{ "Sunday", 0 },
{ "Sun", 0 },
{ "Monday", 1 },
{ "Mon", 1 },
{ "Tuesday", 2 },
{ "Tue", 2 },
{ "Wednesday", 3 },
{ "Wed", 3 },
{ "Thursday", 4 },
{ "Thu", 4 },
{ "Friday", 5 },
{ "Fri", 5 },
{ "Saturday", 6 },
{ "Sat", 6 },
};
const char *k;
const char *utc;
time_t x;
int r, weekday = -1;
unsigned i;
/*
* 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
* @2147483647 (seconds since epoch)
*
*/
assert(t);
if (t[0] == '@')
if (streq(t, "now"))
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 ((k = endswith(t, " ago"))) {
t = strndupa(t, k - t);
if (r < 0)
return r;
goto finish;
} else if ((k = endswith(t, " left"))) {
t = strndupa(t, k - t);
if (r < 0)
return r;
goto finish;
}
if (utc)
x = ret / USEC_PER_SEC;
x_usec = 0;
if (streq(t, "today")) {
goto from_tm;
} else if (streq(t, "yesterday")) {
goto from_tm;
} else if (streq(t, "tomorrow")) {
goto from_tm;
}
for (i = 0; i < ELEMENTSOF(day_nr); i++) {
continue;
if (t[skip] != ' ')
continue;
t += skip + 1;
break;
}
if (k) {
if (*k == '.')
goto parse_usec;
else if (*k == 0)
goto from_tm;
}
if (k) {
if (*k == '.')
goto parse_usec;
else if (*k == 0)
goto from_tm;
}
if (k && *k == 0) {
goto from_tm;
}
if (k && *k == 0) {
goto from_tm;
}
if (k && *k == 0) {
goto from_tm;
}
if (k && *k == 0) {
goto from_tm;
}
if (k) {
if (*k == '.')
goto parse_usec;
else if (*k == 0)
goto from_tm;
}
if (k && *k == 0) {
goto from_tm;
}
return -EINVAL;
{
unsigned add;
k++;
if (r < 0)
return -EINVAL;
if (*k)
return -EINVAL;
}
if (x == (time_t) -1)
return -EINVAL;
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 },
{ "M", 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, *s;
usec_t r = 0;
bool something = false;
assert(t);
assert(default_unit > 0);
p = t;
p += strspn(p, WHITESPACE);
s = startswith(p, "infinity");
if (s) {
s += strspn(s, WHITESPACE);
if (*s != 0)
return -EINVAL;
*usec = USEC_INFINITY;
return 0;
}
for (;;) {
long long l, z = 0;
char *e;
unsigned i, n = 0;
usec_t multiplier, k;
p += strspn(p, WHITESPACE);
if (*p == 0) {
if (!something)
return -EINVAL;
break;
}
errno = 0;
l = strtoll(p, &e, 10);
if (errno > 0)
return -errno;
if (l < 0)
return -ERANGE;
if (*e == '.') {
char *b = e + 1;
errno = 0;
z = strtoll(b, &e, 10);
if (errno > 0)
return -errno;
if (z < 0)
return -ERANGE;
if (e == b)
return -EINVAL;
n = e - b;
} else if (e == p)
return -EINVAL;
e += strspn(e, WHITESPACE);
for (i = 0; i < ELEMENTSOF(table); i++)
break;
}
if (i >= ELEMENTSOF(table)) {
p = e;
}
something = true;
k = (usec_t) z * multiplier;
for (; n > 0; n--)
k /= 10;
r += (usec_t) l * multiplier + k;
}
*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, *s;
nsec_t r = 0;
bool something = false;
assert(t);
p = t;
p += strspn(p, WHITESPACE);
s = startswith(p, "infinity");
if (s) {
s += strspn(s, WHITESPACE);
if (*s != 0)
return -EINVAL;
*nsec = NSEC_INFINITY;
return 0;
}
for (;;) {
long long l, z = 0;
char *e;
unsigned i, n = 0;
p += strspn(p, WHITESPACE);
if (*p == 0) {
if (!something)
return -EINVAL;
break;
}
errno = 0;
l = strtoll(p, &e, 10);
if (errno > 0)
return -errno;
if (l < 0)
return -ERANGE;
if (*e == '.') {
char *b = e + 1;
errno = 0;
z = strtoll(b, &e, 10);
if (errno > 0)
return -errno;
if (z < 0)
return -ERANGE;
if (e == b)
return -EINVAL;
n = e - b;
} else if (e == p)
return -EINVAL;
e += strspn(e, WHITESPACE);
for (i = 0; i < ELEMENTSOF(table); i++)
for (; n > 0; n--)
k /= 10;
something = true;
break;
}
if (i >= ELEMENTSOF(table))
return -EINVAL;
}
*nsec = r;
return 0;
}
bool ntp_synced(void) {
return false;
return false;
return true;
}
int get_timezones(char ***ret) {
if (!zones)
return -ENOMEM;
n_allocated = 2;
n_zones = 1;
if (f) {
char l[LINE_MAX];
FOREACH_LINE(l, f, return -errno) {
char *p, *w;
size_t k;
p = strstrip(l);
if (isempty(p) || *p == '#')
continue;
/* Skip over country code */
p += strcspn(p, WHITESPACE);
p += strspn(p, WHITESPACE);
/* Skip over coordinates */
p += strcspn(p, WHITESPACE);
p += strspn(p, WHITESPACE);
/* Found timezone name */
k = strcspn(p, WHITESPACE);
if (k <= 0)
continue;
w = strndup(p, k);
if (!w)
return -ENOMEM;
free(w);
return -ENOMEM;
}
}
return -errno;
return 0;
}
bool timezone_is_valid(const char *name) {
bool slash = false;
const char *p, *t;
return false;
if (name[0] == '/')
return false;
for (p = name; *p; p++) {
if (!(*p >= '0' && *p <= '9') &&
!(*p >= 'a' && *p <= 'z') &&
!(*p >= 'A' && *p <= 'Z') &&
!(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
return false;
if (*p == '/') {
if (slash)
return false;
slash = true;
} else
slash = false;
}
if (slash)
return false;
return false;
return false;
return true;
}
clockid_t clock_boottime_or_monotonic(void) {
int fd;
if (clock != -1)
return clock;
if (fd < 0)
else {
safe_close(fd);
}
return clock;
}
int get_timezone(char **tz) {
_cleanup_free_ char *t = NULL;
const char *e;
char *z;
int r;
r = readlink_malloc("/etc/localtime", &t);
if (r < 0)
return r; /* returns EINVAL if not a symlink */
e = path_startswith(t, "/usr/share/zoneinfo/");
if (!e)
e = path_startswith(t, "../usr/share/zoneinfo/");
if (!e)
return -EINVAL;
if (!timezone_is_valid(e))
return -EINVAL;
z = strdup(e);
if (!z)
return -ENOMEM;
*tz = z;
return 0;
}
}
}
unsigned long usec_to_jiffies(usec_t u) {
static thread_local unsigned long hz = 0;
long r;
if (hz == 0) {
r = sysconf(_SC_CLK_TCK);
assert(r > 0);
hz = (unsigned long) r;
}
}