From c11df752257e4c5bc100951f24a2b74cf52b17a0 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Tue, 27 Oct 2026 21:39:31 +0100 Subject: [PATCH] Fix: Report the real runtime instead of a timezone skew in host info The host info popup showed a constant "7h" runtime for every Linux host. szStartTime carries no timezone marker and the two sides disagree: the Windows client sends its own wall clock (client/LoginServer.cpp getProcessTime), while the Linux and Android clients send Beijing time unconditionally (linux/main.cpp and common/commands.h ToPekingTimeAsString hardcode UTC+8). GetElapsedTime then built the base time with CTime as local time and subtracted it from the local clock, so on any server that is not at UTC+8 the start time landed in the future; the absolute value turned that skew into a plausible-looking duration (UTC+1 gives exactly 7h). GetElapsedTime now picks the reference "now" by client type: LNX and APK are compared against the current Beijing time, everything else keeps using the local clock. A start time in the future now clamps to 0 instead of being mirrored, so a clock disagreement shows as unknown rather than as a fake runtime. The Linux client also reported the wrong thing entirely: it sent the current time at login, not the process start time, so the value was always about zero whenever the timezones happened to agree. It now records its own wall clock once at load and reports Beijing time built from it, matching the semantics of the Windows client. An upgrade forks a new process, so the counter restarts after an upgrade as expected. The first implementation read /proc/self/stat starttime plus /proc/stat btime, which looked like the exact equivalent of the Windows call, but btime is computed from the wall clock at boot and never corrected when NTP later steps the clock, so on WSL2 it drifted by 1.8h. Recording the value in the process itself has no such failure mode. Verified against a Debug server with a WSL Ubuntu 24.04 client and a Windows client: the popup now reports "2.3m" for a Linux process started 2.3 minutes earlier (previously 7h), the startup timestamp is the process load time in Beijing time, and the Windows host still reports 14s right after it connects. Co-Authored-By: deepseek-flash --- linux/main.cpp | 21 +++++++++++++++++++-- server/2015Remote/2015RemoteDlg.cpp | 25 +++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/linux/main.cpp b/linux/main.cpp index 8dd7b47..2c41f7d 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -599,6 +600,21 @@ std::string getLinuxDistro() return "Linux"; } +// 进程启动时刻(墙钟)。在程序加载时记录一次:升级走 fork+execv 会重新加载本进程 +// 镜像,于是升级后重新计时,与 Windows 的 GetProcessTimes(创建时间) 语义一致。 +// 刻意不用 /proc/self/stat 的 starttime + /proc/stat 的 btime:btime 是开机时按当时 +// 墙钟算的,之后 NTP 校时/休眠都不会修正它,算出来的启动时刻可能偏出几小时 +// (WSL2 实测偏 1.8h)。这里是本进程自己读到的墙钟,不受此影响。 +static const std::time_t g_processStartTime = std::time(nullptr); + +// 进程启动时间("YYYY-MM-DD HH:MM:SS",北京时间):与 Windows 客户端的 +// getProcessTime() 一致,是进程创建时刻而非本次登录时刻。 +std::string getProcessStartTime() +{ + // 服务端按北京时间解释这个字符串(见服务端 GetElapsedTime),沿用既有约定 + return ToPekingTimeAsString(&g_processStartTime); +} + // 获取 CPU 核心数 int getCPUCores() { @@ -1086,8 +1102,9 @@ int main(int argc, char* argv[]) strncpy(logInfo.OsVerInfoEx, distro.c_str(), sizeof(logInfo.OsVerInfoEx) - 1); logInfo.OsVerInfoEx[sizeof(logInfo.OsVerInfoEx) - 1] = '\0'; - // 启动时间 - strncpy(logInfo.szStartTime, ToPekingTimeAsString(nullptr).c_str(), sizeof(logInfo.szStartTime) - 1); + // 启动时间:进程创建时刻(北京时间),非本次登录时刻 + std::string procStart = getProcessStartTime(); + strncpy(logInfo.szStartTime, procStart.c_str(), sizeof(logInfo.szStartTime) - 1); logInfo.szStartTime[sizeof(logInfo.szStartTime) - 1] = '\0'; // CPU 主频 diff --git a/server/2015Remote/2015RemoteDlg.cpp b/server/2015Remote/2015RemoteDlg.cpp index 229c5e7..0c8c33b 100644 --- a/server/2015Remote/2015RemoteDlg.cpp +++ b/server/2015Remote/2015RemoteDlg.cpp @@ -9277,7 +9277,13 @@ CString FormatValue(double dValue, LPCTSTR szUnit) return str; } -CString GetElapsedTime(LPCTSTR szBaseTime) +// 计算「启动信息」里的运行时长。客户端上报的时间字符串没有时区标记:Windows 客户端 +// 发的是本机本地时间(client/LoginServer.cpp getProcessTime),Linux/Android 发的是 +// 北京时间(linux/main.cpp 与 common/commands.h 的 ToPekingTimeAsString,恒为 UTC+8)。 +// 因此基准「现在」必须按客户端类型取:LNX/APK 用北京时间的当前时刻,其余用本地时间。 +// 否则服务端不在 UTC+8 时,北京时间会被当成本地时间,得到的启动时间「在未来」——早先 +// 取绝对值后就成了恒定的时区差(UTC+1 服务端上 Linux 主机永远显示 7h)。 +CString GetElapsedTime(LPCTSTR szBaseTime, bool pekingBase) { int nYear, nMonth, nDay, nHour, nMin, nSec; if (_stscanf_s(szBaseTime, _T("%d-%d-%d %d:%d:%d"), @@ -9286,9 +9292,18 @@ CString GetElapsedTime(LPCTSTR szBaseTime) } CTime tmBase(nYear, nMonth, nDay, nHour, nMin, nSec); - CTime tmNow = CTime::GetCurrentTime(); + CTime tmNow; + if (pekingBase) { + auto t = ToPekingTime(nullptr); + tmNow = CTime(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, + t.tm_hour, t.tm_min, t.tm_sec); + } else { + tmNow = CTime::GetCurrentTime(); + } __int64 nSeconds = (tmNow - tmBase).GetTotalSeconds(); - if (nSeconds < 0) nSeconds = -nSeconds; + // 启动时间「在未来」只可能是时区约定/客户端时钟不一致(如老客户端),此时运行时长 + // 不可知,钳成 0 而不是取绝对值,免得把时钟偏差当成运行时长显示出来。 + if (nSeconds < 0) nSeconds = 0; if (nSeconds < 60) return FormatValue((double)nSeconds, _T("s")); @@ -9353,10 +9368,12 @@ void CMy2015RemoteDlg::OnListClick(NMHDR* pNMHDR, LRESULT* pResult) CString strText; std::string expired = res[RES_EXPIRED_DATE]; expired = expired.empty() ? "" : " Expired on " + expired; + // LNX/APK 客户端上报的启动时间是北京时间,基准时刻按此选(见 GetElapsedTime) + const bool pekingBase = (res[RES_CLIENT_TYPE] == "LNX" || res[RES_CLIENT_TYPE] == "APK"); strText.FormatL(_T("文件路径: %s%s %s%s\r\n系统信息: %s 位 %s 核心 %s GB %s\r\n启动信息: %s %s %s%s %s\r\n上线信息: %s %d %s Master-%s\r\n客户信息: %s"), res[RES_PROGRAM_BITS].IsEmpty() ? "" : res[RES_PROGRAM_BITS] + _L(" 位 "), res[RES_FILE_PATH], res[RES_EXE_VERSION], processInfo, res[RES_SYSTEM_BITS], res[RES_SYSTEM_CPU], res[RES_SYSTEM_MEM], res[RES_RESOLUTION], startTime, expired.c_str(), - res[RES_USERNAME], res[RES_ISADMIN] == "1" ? _L("[管理员]") : res[RES_ISADMIN].IsEmpty() ? "" : _L("[非管理员]"), GetElapsedTime(startTime), + res[RES_USERNAME], res[RES_ISADMIN] == "1" ? _L("[管理员]") : res[RES_ISADMIN].IsEmpty() ? "" : _L("[非管理员]"), GetElapsedTime(startTime, pekingBase), ctx->GetProtocol().c_str(), ctx->GetServerPort(), typMap[type].c_str(), ctx->GetMasterID().c_str(), res[RES_CLIENT_ID]); // 地理信息只在加载了本地 IP 库(QQWry/ip2region)时才查询并展示。 // 未装库时 m_IPConverter 是网络版 GetGeoLocation(同步外网 HTTP,可达数秒),