From 5dda53c71f51ab46e273a6c3033925102cbe99e7 Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Wed, 11 Oct 2023 23:43:00 +0200 Subject: Use strftime to format timestamps. +Provide visible fallbacks if either strftime or gmtime/localtime failed. Appease mplint --- src/gui/widgets/tabs/chat/chattab.cpp | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/gui/widgets/tabs/chat/chattab.cpp b/src/gui/widgets/tabs/chat/chattab.cpp index 48c0669fc..f619a881c 100644 --- a/src/gui/widgets/tabs/chat/chattab.cpp +++ b/src/gui/widgets/tabs/chat/chattab.cpp @@ -251,13 +251,27 @@ void ChatTab::chatLog(std::string line, if (timeInfo != nullptr) { - line = strprintf("%s[%02d:%02d] %s%s", lineColor.c_str(), - timeInfo->tm_hour, timeInfo->tm_min, - tmp.nick.c_str(), tmp.text.c_str()); + char timeStamp[64]; // 64 bytes should be enough for everybody?? C.. + + size_t res = strftime(timeStamp, 64, "[%H:%M]", timeInfo); + // strftime returns 0 even in case of success (empty format or %p) + // see strftime(3) for further details. + if (res > 0) + { + line = strprintf("%s%s %s%s", lineColor.c_str(), timeStamp, + tmp.nick.c_str(), tmp.text.c_str()); + } + else + { + line = strprintf("%s[(strftime error)%02d:%02d] %s%s", + lineColor.c_str(), + timeInfo->tm_hour, timeInfo->tm_min, + tmp.nick.c_str(), tmp.text.c_str()); + } } else { - line = strprintf("%s %s%s", lineColor.c_str(), + line = strprintf("(local/gm-time error)%s %s%s", lineColor.c_str(), tmp.nick.c_str(), tmp.text.c_str()); } -- cgit v1.2.3-60-g2f50