time-util.c revision fbe550738d03b178bb004a1390e74115e904118a
/*-*- 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"
#include "strv.h"
return timespec_load(&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
}
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;
if (utc)
else
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;
if (utc)
else
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;
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 (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 (t[0] == '@')
else if (endswith(t, " ago")) {
_cleanup_free_ char *z;
if (!z)
return -ENOMEM;
if (r < 0)
return r;
goto finish;
} else if (endswith(t, " left")) {
_cleanup_free_ char *z;
if (!z)
return -ENOMEM;
if (r < 0)
return r;
goto finish;
}
for (i = 0; i < ELEMENTSOF(day_nr); i++) {
continue;
if (t[skip] != ' ')
continue;
t += skip + 1;
break;
}
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;
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, *s;
usec_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;
*usec = USEC_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;
}
*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;
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;
}