I realized that when imbuing a stringstream
with a locale and then calling std::put_time
with the format %X
, which is a locale-dependent time, the time doesn’t change to be dependent on macOS. This does seem to work on Windows, however. I’ll attach a basic C++ file showing off this quirk, and I’ll put the outputs that I see below:
# macOS
Current time (US): 15:52:27
Current time (UK): 15:52:27
# Windows
Current time (US): 4:05:28 PM
Current time (UK): 16:05:28
Here’s a basic example:
#include <string>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <chrono>
std::string TimeAsString(const std::chrono::system_clock::time_point& tp, const std::string& strLocale = "")
{
const std::time_t tt = std::chrono::system_clock::to_time_t(tp);
if (tt > 0)
{
std::tm ti;
#if defined(_WIN32)
localtime_s(&ti, &tt);
#else
localtime_r(&tt, &ti);
#endif
const auto locale = std::locale(strLocale);
std::stringstream ss;
ss.imbue(locale);
ss << std::put_time(&ti, "%X");
return ss.str();
}
return "N/A";
}
int main()
{
const auto now = std::chrono::system_clock::now();
std::cout << "Current time (US): " << TimeAsString(now, "en_US") << std::endl;
std::cout << "Current time (UK): " << TimeAsString(now, "en_GB") << std::endl;
return 0;
}
Does anyone have any idea why this is happening?