From 47c88c433d17948fab1d8e1d76121a72fe5938cb Mon Sep 17 00:00:00 2001 From: "B. Stack" Date: Wed, 7 Sep 2022 14:49:22 -0400 Subject: add upstream 11.24 --- zen/format_unit.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) (limited to 'zen/format_unit.cpp') diff --git a/zen/format_unit.cpp b/zen/format_unit.cpp index 2aa6e094..8b3fccfe 100644 --- a/zen/format_unit.cpp +++ b/zen/format_unit.cpp @@ -168,12 +168,27 @@ std::wstring zen::formatNumber(int64_t n) std::wstring zen::formatUtcToLocalTime(time_t utcTime) { - auto errorMsg = [&] { return _("Error") + L" (time_t: " + numberTo(utcTime) + L')'; }; + auto fmtFallback = [utcTime] //don't take "no" for an answer! + { + if (const TimeComp tc = getUtcTime(utcTime); + tc != TimeComp()) + { + wchar_t buf[128] = {}; //the only way to format abnormally large or invalid modTime: std::strftime() will fail! + if (const int rv = std::swprintf(buf, std::size(buf), L"%d-%02d-%02d %02d:%02d:%02d GMT", tc.year, tc.month, tc.day, tc.hour, tc.minute, tc.second); + 0 < rv && rv < std::ssize(buf)) + return std::wstring(buf, rv); + } + + return L"time_t = " + numberTo(utcTime); + }; const TimeComp& loc = getLocalTime(utcTime); //returns TimeComp() on error - std::wstring dateString = utfTo(formatTime(Zstr("%x %X"), loc)); - return !dateString.empty() ? dateString : errorMsg(); + /*const*/ std::wstring dateTimeFmt = utfTo(formatTime(Zstr("%x %X"), loc)); + if (dateTimeFmt.empty()) + return fmtFallback(); + + return dateTimeFmt; } @@ -188,9 +203,9 @@ WeekDay impl::getFirstDayOfWeekImpl() //throw SysError const char* firstDay = ::nl_langinfo(_NL_TIME_FIRST_WEEKDAY); //[1-Sunday, 7-Saturday] ASSERT_SYSERROR(firstDay && 1 <= *firstDay && *firstDay <= 7); - const int weekDayStartSunday = *firstDay; - const int weekDayStartMonday = (weekDayStartSunday - 1 + 6) % 7; //+6 == -1 in Z_7 - // [0-Monday, 6-Sunday] + const int weekDayStartSunday = *firstDay; //[1-Sunday, 7-Saturday] + const int weekDayStartMonday = (weekDayStartSunday - 2 + 7) % 7; //[0-Monday, 6-Sunday] 7 == 0 in Z_7 + return static_cast(weekDayStartMonday); } -- cgit