char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
header_time[64],
header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
- time_t t;
struct tm tm;
+ int r;
if (syslog_fd < 0)
return 0;
xsprintf(header_priority, "<%i>", level);
- t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
- if (!localtime_r(&t, &tm))
- return -EINVAL;
+ r = localtime_or_gmtime_usec(now(CLOCK_REALTIME), /* utc= */ false, &tm);
+ if (r < 0)
+ return r;
if (strftime(header_time, sizeof(header_time), "%h %e %T ", &tm) <= 0)
return -EINVAL;
support_end = _support_end_alloc;
}
- if (isempty(support_end)) /* An empty string is a explicit way to say "no EOL exists" */
+ if (isempty(support_end)) { /* An empty string is a explicit way to say "no EOL exists" */
+ if (ret_eol)
+ *ret_eol = USEC_INFINITY;
+
return false; /* no end date defined */
+ }
struct tm tm = {};
const char *k = strptime(support_end, "%Y-%m-%d", &tm);
if (!k || *k)
return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, SYNTHETIC_ERRNO(EINVAL),
- "Failed to parse SUPPORT_END= in os-release file, ignoring: %m");
+ "Failed to parse SUPPORT_END= from os-release file, ignoring: %s", support_end);
- time_t eol = timegm(&tm);
- if (eol == (time_t) -1)
- return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, SYNTHETIC_ERRNO(EINVAL),
- "Failed to convert SUPPORT_END= in os-release file, ignoring: %m");
+ usec_t eol;
+ r = mktime_or_timegm_usec(&tm, /* utc= */ true, &eol);
+ if (r < 0)
+ return log_full_errno(quiet ? LOG_DEBUG : LOG_WARNING, r,
+ "Failed to convert SUPPORT_END= time from os-release file, ignoring: %m");
if (ret_eol)
- *ret_eol = eol * USEC_PER_SEC;
+ *ret_eol = eol;
- return DIV_ROUND_UP(now(CLOCK_REALTIME), USEC_PER_SEC) > (usec_t) eol;
+ return now(CLOCK_REALTIME) > eol;
}
const char* os_release_pretty_name(const char *pretty_name, const char *name) {
struct tm tm;
bool utc, us;
- time_t sec;
size_t n;
assert(buf);
return strcpy(buf, xxx[style]);
}
- sec = (time_t) (t / USEC_PER_SEC); /* Round down */
-
- if (!localtime_or_gmtime_r(&sec, &tm, utc))
+ if (localtime_or_gmtime_usec(t, utc, &tm) < 0)
return NULL;
/* Start with the week day */
unsigned fractional = 0;
const char *k;
struct tm tm, copy;
- time_t sec;
/* Allowed syntaxes:
*
}
}
- sec = (time_t) (usec / USEC_PER_SEC);
-
- if (!localtime_or_gmtime_r(&sec, &tm, utc))
- return -EINVAL;
+ r = localtime_or_gmtime_usec(usec, utc, &tm);
+ if (r < 0)
+ return r;
tm.tm_isdst = isdst;
} else
minus = gmtoff * USEC_PER_SEC;
- sec = mktime_or_timegm(&tm, utc);
- if (sec < 0)
- return -EINVAL;
+ r = mktime_or_timegm_usec(&tm, utc, &usec);
+ if (r < 0)
+ return r;
- usec = usec_add(sec * USEC_PER_SEC, fractional);
+ usec = usec_add(usec, fractional);
finish:
usec = usec_add(usec, plus);
return strdup_to(ret, e);
}
-time_t mktime_or_timegm(struct tm *tm, bool utc) {
+int mktime_or_timegm_usec(
+ struct tm *tm, /* input + normalized output */
+ bool utc,
+ usec_t *ret) {
+
+ time_t t;
+
assert(tm);
- return utc ? timegm(tm) : mktime(tm);
+ if (tm->tm_year < 69) /* early check for negative (i.e. before 1970) time_t (Note that in some timezones the epoch is in the year 1969!)*/
+ return -ERANGE;
+ if ((usec_t) tm->tm_year > CONST_MIN(USEC_INFINITY / USEC_PER_YEAR, (usec_t) TIME_T_MAX / (365U * 24U * 60U * 60U)) - 1900) /* early check for possible overrun of usec_t or time_t */
+ return -ERANGE;
+
+ /* timegm()/mktime() is a bit weird to use, since it returns -1 in two cases: on error as well as a
+ * valid time indicating one second before the UNIX epoch. Let's treat both cases the same here, and
+ * return -ERANGE for anything negative, since usec_t is unsigned, and we can thus not express
+ * negative times anyway. */
+
+ t = utc ? timegm(tm) : mktime(tm);
+ if (t < 0) /* Refuse negative times and errors */
+ return -ERANGE;
+ if ((usec_t) t >= USEC_INFINITY / USEC_PER_SEC) /* Never return USEC_INFINITY by accident (or overflow) */
+ return -ERANGE;
+
+ if (ret)
+ *ret = (usec_t) t * USEC_PER_SEC;
+ return 0;
}
-struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc) {
- assert(t);
- assert(tm);
+int localtime_or_gmtime_usec(
+ usec_t t,
+ bool utc,
+ struct tm *ret) {
- return utc ? gmtime_r(t, tm) : localtime_r(t, tm);
+ t /= USEC_PER_SEC; /* Round down */
+ if (t > (usec_t) TIME_T_MAX)
+ return -ERANGE;
+ time_t sec = (time_t) t;
+
+ struct tm buf = {};
+ if (!(utc ? gmtime_r(&sec, &buf) : localtime_r(&sec, &buf)))
+ return -EINVAL;
+
+ if (ret)
+ *ret = buf;
+
+ return 0;
}
static uint32_t sysconf_clock_ticks_cached(void) {
int get_timezone(char **ret);
-time_t mktime_or_timegm(struct tm *tm, bool utc);
-struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc);
+int mktime_or_timegm_usec(struct tm *tm, bool utc, usec_t *ret);
+int localtime_or_gmtime_usec(usec_t t, bool utc, struct tm *ret);
uint32_t usec_to_jiffies(usec_t usec);
usec_t jiffies_to_usec(uint32_t jiffies);
.tm_mon = m,
.tm_year = y,
};
- time_t v = timegm(&tm);
- if (v == (time_t) -1)
- return -errno;
+
+ usec_t v;
+ r = mktime_or_timegm_usec(&tm, /* utc= */ true, &v);
+ if (r < 0)
+ return r;
if (tm.tm_mday != (int) d || tm.tm_mon != (int) m || tm.tm_year != (int) y)
return -EINVAL; /* date was not normalized? (e.g. "30th of feb") */
- *ret = (usec_t) v * USEC_PER_SEC;
-
+ *ret = v;
return 0;
}
_cleanup_(freelocalep) locale_t loc = (locale_t) 0;
const char *e;
struct tm tm;
- time_t v;
assert(t);
assert(ret);
if (!e || *e != 0)
return -EINVAL;
- v = timegm(&tm);
- if (v == (time_t) -1)
- return -EINVAL;
-
- *ret = (usec_t) v * USEC_PER_SEC;
- return 0;
+ return mktime_or_timegm_usec(&tm, /* usec= */ true, ret);
}
char header_priority[DECIMAL_STR_MAX(priority) + 3], header_time[64],
header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t) + 1];
int n = 0;
- time_t t;
struct tm tm;
_cleanup_free_ char *ident_buf = NULL;
iovec[n++] = IOVEC_MAKE_STRING(header_priority);
/* Second: timestamp */
- t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
- if (!localtime_r(&t, &tm))
+ if (localtime_or_gmtime_usec(tv ? tv->tv_sec * USEC_PER_SEC : now(CLOCK_REALTIME), /* utc= */ false, &tm) < 0)
return;
if (strftime(header_time, sizeof(header_time), "%h %e %T ", &tm) <= 0)
return;
static int format_timestamp_dns(char *buf, size_t l, time_t sec) {
struct tm tm;
+ int r;
assert(buf);
assert(l > STRLEN("YYYYMMDDHHmmSS"));
- if (!gmtime_r(&sec, &tm))
- return -EINVAL;
+ r = localtime_or_gmtime_usec(sec * USEC_PER_SEC, /* utc= */ true, &tm);
+ if (r < 0)
+ return r;
if (strftime(buf, l, "%Y%m%d%H%M%S", &tm) <= 0)
return -EINVAL;
struct tm tm;
int r;
- if (!gmtime_r(&time, &tm))
+ if ((usec_t) time > USEC_INFINITY / USEC_PER_SEC)
return -ERANGE;
+ r = localtime_or_gmtime_usec((usec_t) time * USEC_PER_SEC, /* utc= */ true, &tm);
+ if (r < 0)
+ return r;
+
if (tm.tm_year > INT_MAX - 1900)
return -ERANGE;
}
static int find_end_of_month(const struct tm *tm, bool utc, int day) {
- struct tm t = *tm;
+ struct tm t = *ASSERT_PTR(tm);
t.tm_mon++;
t.tm_mday = 1 - day;
- if (mktime_or_timegm(&t, utc) < 0 ||
+ if (mktime_or_timegm_usec(&t, utc, /* ret= */ NULL) < 0 ||
t.tm_mon != tm->tm_mon)
return -1;
}
static int tm_within_bounds(struct tm *tm, bool utc) {
- struct tm t;
- int cmp;
+ int r;
+
assert(tm);
/*
if (tm->tm_year + 1900 > MAX_YEAR)
return -ERANGE;
- t = *tm;
- if (mktime_or_timegm(&t, utc) < 0)
- return negative_errno();
+ struct tm t = *tm;
+ r = mktime_or_timegm_usec(&t, utc, /* ret= */ NULL);
+ if (r < 0)
+ return r;
/*
* Did any normalization take place? If so, it was out of bounds before.
* out of bounds. Normalization has occurred implies find_matching_component() > 0,
* other sub time units are already reset in find_next().
*/
+ int cmp;
if ((cmp = CMP(t.tm_year, tm->tm_year)) != 0)
t.tm_mon = 0;
else if ((cmp = CMP(t.tm_mon, tm->tm_mon)) != 0)
return true;
t = *tm;
- if (mktime_or_timegm(&t, utc) < 0)
+ if (mktime_or_timegm_usec(&t, utc, /* ret= */ NULL) < 0)
return false;
k = t.tm_wday == 0 ? 6 : t.tm_wday - 1;
for (unsigned iteration = 0; iteration < MAX_CALENDAR_ITERATIONS; iteration++) {
/* Normalize the current date */
- (void) mktime_or_timegm(&c, spec->utc);
+ (void) mktime_or_timegm_usec(&c, spec->utc, /* ret= */ NULL);
c.tm_isdst = spec->dst;
c.tm_year += 1900;
}
static int calendar_spec_next_usec_impl(const CalendarSpec *spec, usec_t usec, usec_t *ret_next) {
+ usec_t tm_usec;
struct tm tm;
- time_t t;
int r;
- usec_t tm_usec;
assert(spec);
return -EINVAL;
usec++;
- t = (time_t) (usec / USEC_PER_SEC);
- assert_se(localtime_or_gmtime_r(&t, &tm, spec->utc));
+ r = localtime_or_gmtime_usec(usec, spec->utc, &tm);
+ if (r < 0)
+ return r;
tm_usec = usec % USEC_PER_SEC;
r = find_next(spec, &tm, &tm_usec);
if (r < 0)
return r;
- t = mktime_or_timegm(&tm, spec->utc);
- if (t < 0)
- return -EINVAL;
+ usec_t t;
+ r = mktime_or_timegm_usec(&tm, spec->utc, &t);
+ if (r < 0)
+ return r;
if (ret_next)
- *ret_next = (usec_t) t * USEC_PER_SEC + tm_usec;
+ *ret_next = t + tm_usec;
return 0;
}
}
int clock_set_timezone(int *ret_minutesdelta) {
- struct timespec ts;
struct tm tm;
+ int r;
+
+ r = localtime_or_gmtime_usec(now(CLOCK_REALTIME), /* utc= */ false, &tm);
+ if (r < 0)
+ return r;
- assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
- assert_se(localtime_r(&ts.tv_sec, &tm));
int minutesdelta = tm.tm_gmtoff / 60;
struct timezone tz = {
usec_t usec) {
char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, 64U)];
+ int r;
assert(f);
assert(j);
if (!VALID_REALTIME(usec))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No valid realtime timestamp available.");
- if (IN_SET(mode, OUTPUT_SHORT_FULL, OUTPUT_WITH_UNIT)) {
- const char *k;
+ switch (mode) {
- if (flags & OUTPUT_UTC)
- k = format_timestamp_style(buf, sizeof(buf), usec, TIMESTAMP_UTC);
- else
- k = format_timestamp(buf, sizeof(buf), usec);
- if (!k)
+ case OUTPUT_SHORT_FULL:
+ case OUTPUT_WITH_UNIT: {
+ if (!format_timestamp_style(buf, sizeof(buf), usec, flags & OUTPUT_UTC ? TIMESTAMP_UTC : TIMESTAMP_PRETTY))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Failed to format timestamp: %" PRIu64, usec);
+ break;
+ }
- } else {
- struct tm tm;
- time_t t;
+ case OUTPUT_SHORT_UNIX:
+ xsprintf(buf, "%10" PRI_USEC ".%06" PRI_USEC, usec / USEC_PER_SEC, usec % USEC_PER_SEC);
+ break;
- t = (time_t) (usec / USEC_PER_SEC);
+ case OUTPUT_SHORT:
+ case OUTPUT_SHORT_PRECISE:
+ case OUTPUT_SHORT_ISO:
+ case OUTPUT_SHORT_ISO_PRECISE: {
+ struct tm tm;
+ size_t tail = 0;
- switch (mode) {
+ r = localtime_or_gmtime_usec(usec, flags & OUTPUT_UTC, &tm);
+ if (r < 0)
+ log_debug_errno(r, "Failed to convert timestamp to calendar time, generating fallback timestamp: %m");
+ else {
+ tail = strftime(
+ buf, sizeof(buf),
+ IN_SET(mode, OUTPUT_SHORT_ISO, OUTPUT_SHORT_ISO_PRECISE) ? "%Y-%m-%dT%H:%M:%S" : "%b %d %H:%M:%S",
+ &tm);
+ if (tail <= 0)
+ log_debug("Failed to format calendar time, generating fallback timestamp.");
+ }
- case OUTPUT_SHORT_UNIX:
- xsprintf(buf, "%10"PRI_TIME".%06"PRIu64, t, usec % USEC_PER_SEC);
- break;
+ if (tail <= 0) {
+ /* Generate fallback timestamp if regular formatting didn't work. (This might happen on systems where time_t is 32bit) */
- case OUTPUT_SHORT_ISO:
- case OUTPUT_SHORT_ISO_PRECISE: {
- size_t tail = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S",
- localtime_or_gmtime_r(&t, &tm, flags & OUTPUT_UTC));
- if (tail == 0)
- return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format ISO time.");
-
- /* No usec in strftime, need to append */
- if (mode == OUTPUT_SHORT_ISO_PRECISE) {
- assert_se(snprintf_ok(buf + tail, ELEMENTSOF(buf) - tail, ".%06"PRI_USEC, usec % USEC_PER_SEC));
- tail += 7;
- }
+ static const char *const xxx[_OUTPUT_MODE_MAX] = {
+ [OUTPUT_SHORT] = "XXX XX XX:XX:XX",
+ [OUTPUT_SHORT_PRECISE] = "XXX XX XX:XX:XX.XXXXXX",
+ [OUTPUT_SHORT_ISO] = "XXXX-XX-XXTXX:XX:XX+XX:XX",
+ [OUTPUT_SHORT_ISO_PRECISE] = "XXXX-XX-XXTXX:XX:XX.XXXXXX+XX:XX",
+ };
- int h = tm.tm_gmtoff / 60 / 60;
- int m = labs((tm.tm_gmtoff / 60) % 60);
- snprintf(buf + tail, ELEMENTSOF(buf) - tail, "%+03d:%02d", h, m);
- break;
+ fputs(xxx[mode], f);
+ return strlen(xxx[mode]);
}
- case OUTPUT_SHORT:
- case OUTPUT_SHORT_PRECISE:
+ assert(tail <= sizeof(buf));
- if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S",
- localtime_or_gmtime_r(&t, &tm, flags & OUTPUT_UTC)) <= 0)
- return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format syslog time.");
+ /* No usec in strftime, need to append */
+ if (IN_SET(mode, OUTPUT_SHORT_ISO_PRECISE, OUTPUT_SHORT_PRECISE)) {
+ assert_se(snprintf_ok(buf + tail, sizeof(buf) - tail, ".%06" PRI_USEC, usec % USEC_PER_SEC));
- if (mode == OUTPUT_SHORT_PRECISE) {
- assert(sizeof(buf) > strlen(buf));
- if (!snprintf_ok(buf + strlen(buf), sizeof(buf) - strlen(buf), ".%06"PRIu64, usec % USEC_PER_SEC))
- return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format precise time.");
- }
- break;
+ tail += 7;
+
+ assert(tail <= sizeof(buf));
+ }
+
+ if (IN_SET(mode, OUTPUT_SHORT_ISO, OUTPUT_SHORT_ISO_PRECISE)) {
+ int h = tm.tm_gmtoff / 60 / 60,
+ m = abs((int) ((tm.tm_gmtoff / 60) % 60));
- default:
- assert_not_reached();
+ assert_se(snprintf_ok(buf + tail, sizeof(buf) - tail, "%+03d:%02d", h, m));
}
+
+ break;
+ }
+
+ default:
+ assert_not_reached();
}
fputs(buf, f);
}
int parse_shutdown_time_spec(const char *t, usec_t *ret) {
+ int r;
+
assert(t);
assert(ret);
} else {
char *e = NULL;
long hour, minute;
- struct tm tm = {};
- time_t s;
- usec_t n;
errno = 0;
hour = strtol(t, &e, 10);
if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
return -EINVAL;
- n = now(CLOCK_REALTIME);
- s = (time_t) (n / USEC_PER_SEC);
+ usec_t n = now(CLOCK_REALTIME);
+ struct tm tm = {};
- assert_se(localtime_r(&s, &tm));
+ r = localtime_or_gmtime_usec(n, /* utc= */ false, &tm);
+ if (r < 0)
+ return r;
tm.tm_hour = (int) hour;
tm.tm_min = (int) minute;
tm.tm_sec = 0;
- s = mktime(&tm);
- assert(s >= 0);
+ usec_t s;
+ r = mktime_or_timegm_usec(&tm, /* utc= */ false, &s);
+ if (r < 0)
+ return r;
- *ret = (usec_t) s * USEC_PER_SEC;
+ while (s <= n)
+ s += USEC_PER_DAY;
- while (*ret <= n)
- *ret += USEC_PER_DAY;
+ *ret = s;
}
return 0;
test_parse_timestamp_one("1996-12-20T00:39:57Z", 0, 851042397 * USEC_PER_SEC + 000000);
test_parse_timestamp_one("1990-12-31T23:59:60Z", 0, 662688000 * USEC_PER_SEC + 000000);
test_parse_timestamp_one("1990-12-31T15:59:60-08:00", 0, 662688000 * USEC_PER_SEC + 000000);
- assert_se(parse_timestamp("1937-01-01T12:00:27.87+00:20", NULL) == -EINVAL); /* we don't support pre-epoch timestamps */
+ assert_se(parse_timestamp("1937-01-01T12:00:27.87+00:20", NULL) == -ERANGE); /* we don't support pre-epoch timestamps */
/* We accept timestamps without seconds as well */
test_parse_timestamp_one("1996-12-20T00:39Z", 0, (851042397 - 57) * USEC_PER_SEC + 000000);
test_parse_timestamp_one("1990-12-31T15:59-08:00", 0, (662688000-60) * USEC_PER_SEC + 000000);
tzset();
}
+static usec_t absdiff(usec_t a, usec_t b) {
+ return a > b ? a - b : b - a;
+}
+
+TEST(mktime_or_timegm_usec) {
+
+ usec_t n = now(CLOCK_REALTIME), m;
+ struct tm tm;
+
+ assert_se(localtime_or_gmtime_usec(n, /* utc= */ false, &tm) >= 0);
+ assert_se(mktime_or_timegm_usec(&tm, /* utc= */ false, &m) >= 0);
+ assert_se(absdiff(n, m) < 2 * USEC_PER_DAY);
+
+ assert_se(localtime_or_gmtime_usec(n, /* utc= */ true, &tm) >= 0);
+ assert_se(mktime_or_timegm_usec(&tm, /* utc= */ true, &m) >= 0);
+ assert_se(absdiff(n, m) < USEC_PER_SEC);
+
+ /* This definitely should fail, because we refuse dates before the UNIX epoch */
+ tm = (struct tm) {
+ .tm_mday = 15,
+ .tm_mon = 11,
+ .tm_year = 1969 - 1900,
+ };
+
+ assert_se(mktime_or_timegm_usec(&tm, /* utc= */ true, NULL) == -ERANGE);
+}
+
static int intro(void) {
/* Tests have hard-coded results that do not expect a specific timezone to be set by the caller */
assert_se(unsetenv("TZ") >= 0);
char a[LINE_MAX];
TableCell *cell;
struct tm tm;
- time_t sec;
+ usec_t t;
size_t n;
int r;
tzset();
if (i->time != 0) {
- sec = (time_t) (i->time / USEC_PER_SEC);
+ t = i->time;
have_time = true;
} else if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) {
- sec = time(NULL);
+ t = now(CLOCK_REALTIME);
have_time = true;
} else
log_warning("Could not get time from timedated and not operating locally, ignoring.");
- n = have_time ? strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) : 0;
+ if (have_time) {
+ r = localtime_or_gmtime_usec(t, /* utc= */ false, &tm);
+ if (r < 0) {
+ log_warning_errno(r, "Failed to convert system time to local time, ignoring: %m");
+ n = 0;
+ } else
+ n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", &tm);
+ } else
+ n = 0;
r = table_add_many(table,
TABLE_FIELD, "Local time",
TABLE_STRING, n > 0 ? a : "n/a");
if (r < 0)
return table_log_add_error(r);
- n = have_time ? strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) : 0;
+ if (have_time) {
+ r = localtime_or_gmtime_usec(t, /* utc= */ true, &tm);
+ if (r < 0) {
+ log_warning_errno(r, "Failed to convert system time to universal time, ignoring: %m");
+ n = 0;
+ } else
+ n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", &tm);
+ } else
+ n = 0;
r = table_add_many(table,
TABLE_FIELD, "Universal time",
TABLE_STRING, n > 0 ? a : "n/a");
return table_log_add_error(r);
if (i->rtc_time > 0) {
- time_t rtc_sec;
-
- rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC);
- n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
+ r = localtime_or_gmtime_usec(i->rtc_time, /* utc= */ true, &tm);
+ if (r < 0) {
+ log_warning_errno(r, "Failed to convert RTC time to universal time, ignoring: %m");
+ n = 0;
+ } else
+ n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S", &tm);
} else
n = 0;
r = table_add_many(table,
r = table_add_cell(table, NULL, TABLE_FIELD, "Time zone");
if (r < 0)
return table_log_add_error(r);
-
- n = have_time ? strftime(a, sizeof a, "%Z, %z", localtime_r(&sec, &tm)) : 0;
+ if (have_time) {
+ r = localtime_or_gmtime_usec(t, /* utc= */ false, &tm);
+ if (r < 0) {
+ log_warning_errno(r, "Failed to determine timezone from system time, ignoring: %m");
+ n = 0;
+ } else
+ n = strftime(a, sizeof a, "%Z, %z", &tm);
+ } else
+ n = 0;
r = table_add_cell_stringf(table, NULL, "%s (%s)", strna(i->timezone), n > 0 ? a : "n/a");
if (r < 0)
return table_log_add_error(r);
log_debug("/dev/rtc has no valid time, power loss probably occurred?");
else if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to read RTC: %m");
- else
- t = (usec_t) timegm(&tm) * USEC_PER_SEC;
+ else {
+ r = mktime_or_timegm_usec(&tm, /* utc= */ true, &t);
+ if (r < 0)
+ log_warning_errno(r, "Failed to convert RTC time to UNIX time, ignoring: %m");
+ }
return sd_bus_message_append(reply, "t", t);
}
log_debug_errno(r, "Failed to tell kernel about timezone, ignoring: %m");
if (c->local_rtc) {
- struct timespec ts;
struct tm tm;
/* 4. Sync RTC from system clock, with the new delta */
- assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
- assert_se(localtime_r(&ts.tv_sec, &tm));
-
- r = hwclock_set(&tm);
+ r = localtime_or_gmtime_usec(now(CLOCK_REALTIME), /* utc= */ false, &tm);
if (r < 0)
- log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
+ log_debug_errno(r, "Failed to convert system time to calendar time, ignoring: %m");
+ else {
+ r = hwclock_set(&tm);
+ if (r < 0)
+ log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
+ }
}
log_struct(LOG_INFO,
assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
if (fix_system) {
- struct tm tm;
+ struct tm tm = {
+ .tm_isdst = -1,
+ };
/* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
- localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc);
+ r = localtime_or_gmtime_usec(timespec_load(&ts), !c->local_rtc, &tm);
+ if (r < 0)
+ log_debug_errno(r, "Failed to determine current timezone, ignoring: %m");
/* Override the main fields of struct tm, but not the timezone fields */
r = hwclock_get(&tm);
if (r < 0)
log_debug_errno(r, "Failed to get hardware clock, ignoring: %m");
else {
+ usec_t t;
/* And set the system clock with this */
- ts.tv_sec = mktime_or_timegm(&tm, !c->local_rtc);
- if (clock_settime(CLOCK_REALTIME, &ts) < 0)
- log_debug_errno(errno, "Failed to update system clock, ignoring: %m");
- }
+ r = mktime_or_timegm_usec(&tm, !c->local_rtc, &t);
+ if (r < 0)
+ log_debug_errno(r, "Failed to convert calendar time to system time, ignoring: %m");
+ else {
+ /* We leave the subsecond offset as is! */
+ ts.tv_sec = t / USEC_PER_SEC;
+ if (clock_settime(CLOCK_REALTIME, &ts) < 0)
+ log_debug_errno(errno, "Failed to update system clock, ignoring: %m");
+ }
+ }
} else {
struct tm tm;
/* Sync RTC from system clock */
- localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc);
-
- r = hwclock_set(&tm);
+ r = localtime_or_gmtime_usec(timespec_load(&ts), !c->local_rtc, &tm);
if (r < 0)
- log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
+ log_debug_errno(r, "Failed to convert time to calendar time, ignoring: %m");
+ else {
+ r = hwclock_set(&tm);
+ if (r < 0)
+ log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
+ }
}
log_info("RTC configured to %s time.", c->local_rtc ? "local" : "UTC");
static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *error) {
sd_bus *bus = sd_bus_message_get_bus(m);
- char buf[FORMAT_TIMESTAMP_MAX];
int relative, interactive, r;
Context *c = ASSERT_PTR(userdata);
int64_t utc;
}
/* Sync down to RTC */
- localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc);
-
- r = hwclock_set(&tm);
+ r = localtime_or_gmtime_usec(timespec_load(&ts), !c->local_rtc, &tm);
if (r < 0)
- log_debug_errno(r, "Failed to update hardware clock, ignoring: %m");
+ log_debug_errno(r, "Failed to convert timestamp to calendar time, ignoring: %m");
+ else {
+ r = hwclock_set(&tm);
+ if (r < 0)
+ log_debug_errno(r, "Failed to update hardware clock, ignoring: %m");
+ }
log_struct(LOG_INFO,
"MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR,
"REALTIME="USEC_FMT, timespec_load(&ts),
- LOG_MESSAGE("Changed local time to %s", strnull(format_timestamp(buf, sizeof(buf), timespec_load(&ts)))));
+ LOG_MESSAGE("Changed local time to %s", strnull(FORMAT_TIMESTAMP(timespec_load(&ts)))));
return sd_bus_reply_method_return(m, NULL);
}