Misc: Add error checking to LocalTime()

pull/3582/head
Stenzek 3 weeks ago
parent 6cb727f383
commit 21c83440b3
No known key found for this signature in database

@ -4,18 +4,22 @@
#pragma once
#include <ctime>
#include <optional>
namespace Common {
inline std::tm LocalTime(std::time_t tvalue)
inline std::optional<std::tm> LocalTime(std::time_t tvalue)
{
std::tm ttime;
std::optional<std::tm> ret;
ret.emplace();
#ifdef _MSC_VER
localtime_s(&ttime, &tvalue);
if (localtime_s(&ret.value(), &tvalue) != 0)
ret.reset();
#else
localtime_r(&tvalue, &ttime);
if (!localtime_r(&tvalue, &ret.value()))
ret.reset();
#endif
return ttime;
return ret;
}
} // namespace Common

@ -1594,14 +1594,15 @@ std::string GameList::FormatTimestamp(std::time_t timestamp)
}
else
{
const std::tm ctime = Common::LocalTime(std::time(nullptr));
const std::tm ttime = Common::LocalTime(timestamp);
if (ctime.tm_year == ttime.tm_year && ctime.tm_yday == ttime.tm_yday)
const std::optional<std::tm> ctime = Common::LocalTime(std::time(nullptr));
const std::optional<std::tm> ttime = Common::LocalTime(timestamp);
if (ctime.has_value() && ttime.has_value() && ctime->tm_year == ttime->tm_year && ctime->tm_yday == ttime->tm_yday)
{
ret = TRANSLATE_STR("GameList", "Today");
}
else if ((ctime.tm_year == ttime.tm_year && ctime.tm_yday == (ttime.tm_yday + 1)) ||
(ctime.tm_yday == 0 && (ctime.tm_year - 1) == ttime.tm_year))
else if (ctime.has_value() && ttime.has_value() &&
((ctime->tm_year == ttime->tm_year && ctime->tm_yday == (ttime->tm_yday + 1)) ||
(ctime->tm_yday == 0 && (ctime->tm_year - 1) == ttime->tm_year)))
{
ret = TRANSLATE_STR("GameList", "Yesterday");
}

@ -1158,7 +1158,7 @@ void SaveStateSelectorUI::InitializeListEntry(ListEntry* li, ExtendedSaveStateIn
li->game_details = fmt::format(TRANSLATE_FS("SaveStateSelectorUI", "{} ({})"), ssi->title, ssi->serial);
li->summary = fmt::format(TRANSLATE_FS("SaveStateSelectorUI", DATE_TIME_FORMAT),
Common::LocalTime(static_cast<std::time_t>(ssi->timestamp)));
Common::LocalTime(static_cast<std::time_t>(ssi->timestamp)).value_or(std::tm{}));
li->filename = Path::GetFileName(path);
li->slot = slot;
li->global = global;
@ -1458,9 +1458,14 @@ void SaveStateSelectorUI::ShowSlotOSDMessage()
FILESYSTEM_STAT_DATA sd;
std::string date;
if (!path.empty() && FileSystem::StatFile(path.c_str(), &sd))
date = fmt::format(TRANSLATE_FS("SaveStateSelectorUI", DATE_TIME_FORMAT), Common::LocalTime(sd.ModificationTime));
{
date = fmt::format(TRANSLATE_FS("SaveStateSelectorUI", DATE_TIME_FORMAT),
Common::LocalTime(sd.ModificationTime).value_or(std::tm{}));
}
else
{
date = TRANSLATE_STR("SaveStateSelectorUI", "no save yet");
}
Host::AddIconOSDMessage(
"ShowSlotOSDMessage", ICON_EMOJI_MAGNIFIYING_GLASS_TILTED_LEFT,

@ -353,7 +353,7 @@ static StateVars s_state;
static TinyString GetTimestampStringForFileName()
{
return TinyString::from_format("{:%Y-%m-%d-%H-%M-%S}", Common::LocalTime(std::time(nullptr)));
return TinyString::from_format("{:%Y-%m-%d-%H-%M-%S}", Common::LocalTime(std::time(nullptr)).value_or(std::tm{}));
}
bool System::PerformEarlyHardwareChecks(Error* error)

@ -1416,10 +1416,12 @@ std::string Host::FormatNumber(NumberFormatType type, s64 value)
DefaultCaseIsUnreachable();
}
char buf[128];
const std::tm ttime = Common::LocalTime(static_cast<std::time_t>(value));
std::strftime(buf, std::size(buf), format, &ttime);
ret.assign(buf);
ret.resize(128);
if (const std::optional<std::tm> ltime = Common::LocalTime(static_cast<std::time_t>(value)))
ret.resize(std::strftime(ret.data(), ret.size(), format, &ltime.value()));
else
ret = "Invalid";
}
else
{

@ -605,10 +605,12 @@ std::string Host::FormatNumber(NumberFormatType type, s64 value)
DefaultCaseIsUnreachable();
}
char buf[128];
const std::tm ttime = Common::LocalTime(static_cast<std::time_t>(value));
std::strftime(buf, std::size(buf), format, &ttime);
ret.assign(buf);
ret.resize(128);
if (const std::optional<std::tm> ltime = Common::LocalTime(static_cast<std::time_t>(value)))
ret.resize(std::strftime(ret.data(), ret.size(), format, &ltime.value()));
else
ret = "Invalid";
}
else
{

@ -573,8 +573,13 @@ std::string IsoReader::ISODirectoryEntryDateTime::GetFormattedTime() const
const s32 uts_offset = static_cast<s32>(gmt_offset) * 3600;
const time_t uts = std::mktime(&utime) + uts_offset;
char buf[128];
const std::tm ltime = Common::LocalTime(uts);
const size_t len = std::strftime(buf, std::size(buf), "%c", &ltime);
return std::string(buf, len);
std::string ret;
ret.resize(128);
if (const std::optional<std::tm> ltime = Common::LocalTime(uts))
ret.resize(std::strftime(ret.data(), ret.size(), "%c", &ltime.value()));
else
ret = "Invalid";
return ret;
}

Loading…
Cancel
Save