Skip to content

Commit

Permalink
Fallback gracefully when zoned time processing fails
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaulat committed Jun 26, 2024
1 parent 7161a5a commit 570fedc
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions wxdata/source/scwx/util/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <scwx/util/time.hpp>
#include <scwx/util/enum.hpp>
#include <scwx/util/logger.hpp>

#include <sstream>
#include <unordered_map>
Expand All @@ -25,6 +26,9 @@ namespace scwx
namespace util
{

static const std::string logPrefix_ = "scwx::util::time";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);

static const std::unordered_map<ClockFormat, std::string> clockFormatName_ {
{ClockFormat::_12Hour, "12-hour"},
{ClockFormat::_24Hour, "24-hour"},
Expand Down Expand Up @@ -71,18 +75,35 @@ std::string TimeString(std::chrono::system_clock::time_point time,
{
if (timeZone != nullptr)
{
zoned_time zt = {timeZone, timeInSeconds};

if (clockFormat == ClockFormat::_24Hour)
try
{
os << format(FORMAT_STRING_24_HOUR, zt);
zoned_time zt = {timeZone, timeInSeconds};

if (clockFormat == ClockFormat::_24Hour)
{
os << format(FORMAT_STRING_24_HOUR, zt);
}
else
{
os << format(FORMAT_STRING_12_HOUR, zt);
}
}
else
catch (const std::exception& ex)
{
os << format(FORMAT_STRING_12_HOUR, zt);
static bool firstException = true;
if (firstException)
{
logger_->warn("Zoned time error: {}", ex.what());
firstException = false;
}

// In the case where there is a time zone error (e.g., timezone
// database, unicode issue, etc.), fall back to UTC
timeZone = nullptr;
}
}
else

if (timeZone == nullptr)
{
if (clockFormat == ClockFormat::_24Hour)
{
Expand Down

0 comments on commit 570fedc

Please sign in to comment.