mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 18:30:06 +00:00
Fallback gracefully when zoned time processing fails
This commit is contained in:
parent
7161a5a3b2
commit
570fedca33
1 changed files with 28 additions and 7 deletions
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
#include <scwx/util/time.hpp>
|
#include <scwx/util/time.hpp>
|
||||||
#include <scwx/util/enum.hpp>
|
#include <scwx/util/enum.hpp>
|
||||||
|
#include <scwx/util/logger.hpp>
|
||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
@ -25,6 +26,9 @@ namespace scwx
|
||||||
namespace util
|
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_ {
|
static const std::unordered_map<ClockFormat, std::string> clockFormatName_ {
|
||||||
{ClockFormat::_12Hour, "12-hour"},
|
{ClockFormat::_12Hour, "12-hour"},
|
||||||
{ClockFormat::_24Hour, "24-hour"},
|
{ClockFormat::_24Hour, "24-hour"},
|
||||||
|
|
@ -70,6 +74,8 @@ std::string TimeString(std::chrono::system_clock::time_point time,
|
||||||
if (epochValid || time.time_since_epoch().count() != 0)
|
if (epochValid || time.time_since_epoch().count() != 0)
|
||||||
{
|
{
|
||||||
if (timeZone != nullptr)
|
if (timeZone != nullptr)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
zoned_time zt = {timeZone, timeInSeconds};
|
zoned_time zt = {timeZone, timeInSeconds};
|
||||||
|
|
||||||
|
|
@ -82,7 +88,22 @@ std::string TimeString(std::chrono::system_clock::time_point time,
|
||||||
os << format(FORMAT_STRING_12_HOUR, zt);
|
os << format(FORMAT_STRING_12_HOUR, zt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (const std::exception& ex)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timeZone == nullptr)
|
||||||
{
|
{
|
||||||
if (clockFormat == ClockFormat::_24Hour)
|
if (clockFormat == ClockFormat::_24Hour)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue