From 7563e96c5fa24df4c447aa74471205add8c8e7d6 Mon Sep 17 00:00:00 2001 From: ogfi <17439550+code_implement_bot_87c9891c7c94da37@user.noreply.gitee.com> Date: Sun, 4 Oct 2026 03:02:14 +0000 Subject: [PATCH] Fix RTC local time synchronization bug in timesyncd (Issue #34507647) * bump:255.2-ok1.16 * fix(timesync): use realtime clock instead of timex offset for RTC sync --- debian/changelog | 8 +++++++- src/timesync/timesyncd-manager.c | 29 ++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/debian/changelog b/debian/changelog index b621e3d9..06a4a093 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 8c1f27c3..33d33726 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; -- Gitee