Skip to content

Commit

Permalink
Merge pull request #12521 from lioncash/reent
Browse files Browse the repository at this point in the history
Core: Make use of reentrant time utilities where applicable
  • Loading branch information
AdmiralCurtiss committed Jan 26, 2024
2 parents 460ab60 + 6a86b35 commit b09b59c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
14 changes: 8 additions & 6 deletions Source/Core/Core/IOS/Network/KD/NetKDTime.cpp
Expand Up @@ -5,6 +5,8 @@

#include <string>

#include <fmt/chrono.h>

#include "Common/CommonTypes.h"
#include "Core/HW/EXI/EXI_DeviceIPL.h"
#include "Core/HW/Memmap.h"
Expand Down Expand Up @@ -93,10 +95,10 @@ u64 NetKDTimeDevice::GetAdjustedUTC() const

time_t dst_diff{};
const time_t current_time = CEXIIPL::GetEmulatedTime(GetSystem(), CEXIIPL::UNIX_EPOCH);
tm* const gm_time = gmtime(&current_time);
tm gm_time = fmt::gmtime(current_time);

const u32 emulated_time = mktime(gm_time);
if (gm_time->tm_isdst == 1)
const u32 emulated_time = mktime(&gm_time);
if (gm_time.tm_isdst == 1)
dst_diff = 3600;

return u64(s64(emulated_time) + utcdiff - dst_diff);
Expand All @@ -108,10 +110,10 @@ void NetKDTimeDevice::SetAdjustedUTC(u64 wii_utc)

time_t dst_diff{};
const time_t current_time = CEXIIPL::GetEmulatedTime(GetSystem(), CEXIIPL::UNIX_EPOCH);
tm* const gm_time = gmtime(&current_time);
tm gm_time = fmt::gmtime(current_time);

const u32 emulated_time = mktime(gm_time);
if (gm_time->tm_isdst == 1)
const u32 emulated_time = mktime(&gm_time);
if (gm_time.tm_isdst == 1)
dst_diff = 3600;

utcdiff = s64(emulated_time - wii_utc - dst_diff);
Expand Down
17 changes: 8 additions & 9 deletions Source/Core/Core/Movie.cpp
Expand Up @@ -7,8 +7,8 @@
#include <array>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <iterator>
#include <locale>
#include <mbedtls/config.h>
#include <mbedtls/md.h>
#include <mutex>
Expand All @@ -18,6 +18,7 @@
#include <variant>
#include <vector>

#include <fmt/chrono.h>
#include <fmt/format.h>

#include "Common/Assert.h"
Expand Down Expand Up @@ -160,21 +161,19 @@ std::string MovieManager::GetInputDisplay()
}

// NOTE: GPU Thread
std::string MovieManager::GetRTCDisplay()
std::string MovieManager::GetRTCDisplay() const
{
using ExpansionInterface::CEXIIPL;

const time_t current_time =
CEXIIPL::GetEmulatedTime(Core::System::GetInstance(), CEXIIPL::UNIX_EPOCH);
const tm* const gm_time = gmtime(&current_time);
const time_t current_time = CEXIIPL::GetEmulatedTime(m_system, CEXIIPL::UNIX_EPOCH);
const tm gm_time = fmt::gmtime(current_time);

std::ostringstream format_time;
format_time << std::put_time(gm_time, "Date/Time: %c\n");
return format_time.str();
// Use current locale for formatting time, as fmt is locale-agnostic by default.
return fmt::format(std::locale{""}, "Date/Time: {:%c}", gm_time);
}

// NOTE: GPU Thread
std::string MovieManager::GetRerecords()
std::string MovieManager::GetRerecords() const
{
if (IsMovieActive())
return fmt::format("Rerecords: {}", m_rerecords);
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Movie.h
Expand Up @@ -222,8 +222,8 @@ class MovieManager
WiimoteEmu::ExtensionNumber ext, const WiimoteEmu::EncryptionKey& key);

std::string GetInputDisplay();
std::string GetRTCDisplay();
std::string GetRerecords();
std::string GetRTCDisplay() const;
std::string GetRerecords() const;

private:
void GetSettings();
Expand Down
22 changes: 7 additions & 15 deletions Source/Core/Core/State.cpp
Expand Up @@ -7,6 +7,7 @@
#include <atomic>
#include <condition_variable>
#include <filesystem>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
Expand All @@ -15,6 +16,7 @@
#include <utility>
#include <vector>

#include <fmt/chrono.h>
#include <fmt/format.h>

#include <lz4.h>
Expand Down Expand Up @@ -278,21 +280,11 @@ static double GetSystemTimeAsDouble()
static std::string SystemTimeAsDoubleToString(double time)
{
// revert adjustments from GetSystemTimeAsDouble() to get a normal Unix timestamp again
time_t seconds = static_cast<time_t>(time) + DOUBLE_TIME_OFFSET;
errno = 0;
tm* local_time = localtime(&seconds);
if (errno != 0 || !local_time)
return "";

#ifdef _WIN32
wchar_t tmp[32] = {};
wcsftime(tmp, std::size(tmp), L"%x %X", local_time);
return WStringToUTF8(tmp);
#else
char tmp[32] = {};
strftime(tmp, sizeof(tmp), "%x %X", local_time);
return tmp;
#endif
const time_t seconds = static_cast<time_t>(time) + DOUBLE_TIME_OFFSET;
const tm local_time = fmt::localtime(seconds);

// fmt is locale agnostic by default, so explicitly use current locale.
return fmt::format(std::locale{""}, "{:%x %X}", local_time);
}

static std::string MakeStateFilename(int number);
Expand Down

0 comments on commit b09b59c

Please sign in to comment.