time-util.c revision 7568345034f2890af745747783c5abfbf6eccf0f
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering This file is part of systemd.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering Copyright 2010 Lennart Poettering
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering (at your option) any later version.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering systemd is distributed in the hope that it will be useful, but
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering Lesser General Public License for more details.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering assert_se(clock_gettime(clock_id, &ts) == 0);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poetteringdual_timestamp* dual_timestamp_get(dual_timestamp *ts) {
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poetteringdual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) {
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering ts->realtime = ts->monotonic = (usec_t) -1;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering delta = (int64_t) now(CLOCK_REALTIME) - (int64_t) u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringdual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering ts->realtime = ts->monotonic = (usec_t) -1;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringusec_t timespec_load(const struct timespec *ts) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if ((usec_t) ts->tv_sec > (UINT64_MAX - (ts->tv_nsec / NSEC_PER_USEC)) / USEC_PER_SEC)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstruct timespec *timespec_store(struct timespec *ts, usec_t u) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering ts->tv_sec = (time_t) (u / USEC_PER_SEC);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering ts->tv_nsec = (long int) ((u % USEC_PER_SEC) * NSEC_PER_USEC);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringusec_t timeval_load(const struct timeval *tv) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if ((usec_t) tv->tv_sec > (UINT64_MAX - tv->tv_usec) / USEC_PER_SEC)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringstruct timeval *timeval_store(struct timeval *tv, usec_t u) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering tv->tv_sec = (time_t) (u / USEC_PER_SEC);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering tv->tv_usec = (suseconds_t) (u % USEC_PER_SEC);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringchar *format_timestamp(char *buf, size_t l, usec_t t) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) <= 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringchar *format_timestamp_us(char *buf, size_t l, usec_t t) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm) <= 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC));
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0)
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poetteringchar *format_timestamp_relative(char *buf, size_t l, usec_t t) {
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering const char *s;
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering snprintf(buf, l, USEC_FMT " years " USEC_FMT " months %s",
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering (d % USEC_PER_YEAR) / USEC_PER_MONTH, s);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering snprintf(buf, l, USEC_FMT " months " USEC_FMT " days %s",
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering snprintf(buf, l, USEC_FMT " weeks " USEC_FMT " days %s",
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering snprintf(buf, l, USEC_FMT " days %s", d / USEC_PER_DAY, s);
d0a7c5f69207b6719bab94893035fc8f5f6f87cbLennart Poettering snprintf(buf, l, "1 day " USEC_FMT "h %s",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering snprintf(buf, l, USEC_FMT "h " USEC_FMT "min %s",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering (d % USEC_PER_HOUR) / USEC_PER_MINUTE, s);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering snprintf(buf, l, USEC_FMT "min " USEC_FMT "s %s",
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering (d % USEC_PER_MINUTE) / USEC_PER_SEC, s);
78f22b973fa2c9b09bd974680836df17163d9ee0Lennart Poettering else if (d > 0)
return buf;
const char *suffix;
} table[] = {
char *p = buf;
bool something = false;
assert(l > 0);
return NULL;
size_t n;
bool done = false;
usec_t a, b;
if (t < USEC_PER_MINUTE && b > 0) {
k = snprintf(p, l,
done = true;
if (!done) {
k = snprintf(p, l,
something = true;
return buf;
assert(f);
assert(t);
if (!dual_timestamp_is_set(t))
name,
t->realtime,
t->monotonic);
assert(t);
t->realtime = a;
t->monotonic = b;
const char *name;
const int nr;
} day_nr[] = {
time_t x;
assert(t);
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
_cleanup_free_ char *z;
return -ENOMEM;
goto finish;
_cleanup_free_ char *z;
return -ENOMEM;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
goto finish;
return -EINVAL;
return -EINVAL;
return -EINVAL;
ret = 0;
const char *suffix;
} table[] = {
usec_t r = 0;
bool something = false;
assert(t);
if (!something)
return -EINVAL;
errno = 0;
if (errno > 0)
return -errno;
return -ERANGE;
errno = 0;
if (errno > 0)
return -errno;
return -ERANGE;
return -EINVAL;
return -EINVAL;
something = true;
return -EINVAL;
*usec = r;
const char *suffix;
} table[] = {
nsec_t r = 0;
bool something = false;
assert(t);
if (!something)
return -EINVAL;
errno = 0;
if (errno > 0)
return -errno;
return -ERANGE;
errno = 0;
if (errno > 0)
return -errno;
return -ERANGE;
return -EINVAL;
return -EINVAL;
something = true;
return -EINVAL;
*nsec = r;
bool ntp_synced(void) {
if (!zones)
return -ENOMEM;
char l[LINE_MAX];
size_t k;
p = strstrip(l);
w = strndup(p, k);
return -ENOMEM;
free(w);
return -ENOMEM;
return -errno;
bool slash = false;
for (p = name; *p; p++) {
if (slash)
slash = true;
slash = false;
if (slash)