diff --git a/debian/changelog b/debian/changelog index b621e3d98976b31a9ee344ddc64e44b7af1c867b..06a4a0937d68267a0fc02701854b0a5d3b5f66fd 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,8 +1,14 @@ +systemd (255.2-ok1.16) nile; urgency=medium + + * 修复了RTC 本地时间同步缺陷 + + -- gfc Sun, 04 Oct 2026 11:00:15 +0800 + systemd (255.2-ok1.15) nile; urgency=medium * mask parse-hwdb test - -- shilongzhen Tue, 18 Mar 2025 09:26:15 +0800 + -- shilongzhen Tue, 18 Mar 2025 09:26:15 +0800 systemd (255.2-ok1.14) nile; urgency=medium diff --git a/src/timesync/timesyncd-manager.c b/src/timesync/timesyncd-manager.c index 8c1f27c322231679562bc03e7d4df8dd28a8ade0..33d33726d9429002ce553a6cf99aba1773facbf9 100644 --- a/src/timesync/timesyncd-manager.c +++ b/src/timesync/timesyncd-manager.c @@ -296,17 +296,28 @@ static int manager_adjust_clock(Manager *m, double offset, int leap_sec) { if (clock_adjtime(CLOCK_REALTIME, &tmx) < 0) return -errno; - /* If RTC is in local time, sync the adjusted system time to hardware clock */ + /* If RTC is in local time, sync the adjusted system time to hardware clock. Note that + * tmx.time holds a relative offset (ADJ_SETOFFSET) or is unset (slew mode), not the wall + * clock, so read CLOCK_REALTIME here to get the actual current time. */ if (m->rtc_local_time) { + struct timespec ts; struct tm tm; - localtime_r(&tmx.time.tv_sec, &tm); - int r = clock_set_hwclock(&tm); - if (r < 0) - log_info_errno(r, "Failed to sync adjusted time to hardware clock, ignoring: %m"); - else - log_debug("Synced adjusted time to hardware clock in local time mode: %04d-%02d-%02d %02d:%02d:%02d", - tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, - tm.tm_hour, tm.tm_min, tm.tm_sec); + + if (clock_gettime(CLOCK_REALTIME, &ts) < 0) + log_info_errno(errno, "Failed to read system clock, cannot sync hardware clock, ignoring: %m"); + else { + int r; + + localtime_r(&ts.tv_sec, &tm); + + r = clock_set_hwclock(&tm); + if (r < 0) + log_info_errno(r, "Failed to sync adjusted time to hardware clock, ignoring: %m"); + else + log_debug("Synced adjusted time to hardware clock in local time mode: %04d-%02d-%02d %02d:%02d:%02d", + tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, + tm.tm_hour, tm.tm_min, tm.tm_sec); + } } m->drift_freq = tmx.freq;