diff --git a/modules/listsockets.cpp b/modules/listsockets.cpp index ea204560..917dd235 100644 --- a/modules/listsockets.cpp +++ b/modules/listsockets.cpp @@ -153,9 +153,21 @@ class CListSockets : public CModule { CString GetCreatedTime(const Csock* pSocket) { unsigned long long iStartTime = pSocket->GetStartTime(); - timeval tv; - tv.tv_sec = iStartTime / 1000; - tv.tv_usec = iStartTime % 1000 * 1000; + unsigned long long iNow = CUtils::GetMillTime(); + + // TODO This code would look simpler with std::chrono instead. + // Especially if socket API is changed to use that. + timeval tv = CUtils::GetTime(); + unsigned long long iAgeUs = (iNow - iStartTime) * 1000; + + tv.tv_sec -= iAgeUs / 1000000; + long long usec = tv.tv_usec - (long long)(iAgeUs % 1000000); + if (usec < 0) { + tv.tv_sec--; + usec += 1000000; + } + tv.tv_usec = usec; + return CUtils::FormatTime(tv, "%Y-%m-%d %H:%M:%S.%f", GetUser()->GetTimezone()); } diff --git a/test/integration/tests/modules.cpp b/test/integration/tests/modules.cpp index 6bbe49c7..fe315901 100644 --- a/test/integration/tests/modules.cpp +++ b/test/integration/tests/modules.cpp @@ -1214,5 +1214,20 @@ TEST_F(ZNCTest, AwayStoreModule) { // End 'away' bug test } +// https://github.com/znc/znc/issues/1910 +TEST_F(ZNCTest, ListSocketsModule) { + auto znc = Run(); + auto ircd = ConnectIRCd(); + auto client = LoginClient(); + client.Write("znc loadmod listsockets"); + client.ReadUntil("Loaded module"); + client.Write("PRIVMSG *listsockets :list"); + int year = QDate::currentDate().year(); + ASSERT_THAT(client.ReadRemainder().toStdString(), + AnyOf(HasSubstr(std::to_string(year - 1) + "-"), + HasSubstr(std::to_string(year) + "-"), + HasSubstr(std::to_string(year + 1) + "-"))); +} + } // namespace } // namespace znc_inttest