confine locale weirdness to windows only (#317)

This commit is contained in:
ouwou 2024-06-25 00:25:17 -04:00
parent 8fc4d0334f
commit e3e5b700df
4 changed files with 33 additions and 20 deletions

View File

@ -1154,9 +1154,14 @@ void Abaddon::on_window_hide() {
}
int main(int argc, char **argv) {
if (std::getenv("ABADDON_NO_FC") == nullptr)
if (std::getenv("ABADDON_NO_FC") == nullptr) {
Platform::SetupFonts();
}
// windows doesnt have langinfo.h so some localization falls back to translation strings
// i dont like the default translation so this lets us use strftime
#ifdef _WIN32
char *systemLocale = std::setlocale(LC_ALL, "");
try {
if (systemLocale != nullptr) {
@ -1170,6 +1175,7 @@ int main(int argc, char **argv) {
}
} catch (...) {}
}
#endif
#if defined(_WIN32) && defined(_MSC_VER)
TCHAR buf[2] { 0 };

View File

@ -6,6 +6,8 @@
#include "util.hpp"
#include <glibmm/datetime.h>
constexpr static uint64_t DiscordEpochSeconds = 1420070400;
const Snowflake Snowflake::Invalid = -1ULL;
@ -56,11 +58,8 @@ bool Snowflake::IsValid() const {
}
Glib::ustring Snowflake::GetLocalTimestamp() const {
const time_t secs_since_epoch = (m_num / SecondsInterval) + DiscordEpochSeconds;
const std::tm tm = *localtime(&secs_since_epoch);
std::array<char, 256> tmp {};
std::strftime(tmp.data(), sizeof(tmp), "%X %x", &tm);
return tmp.data();
const gint64 secs_since_epoch = (m_num / SecondsInterval) + DiscordEpochSeconds;
return FormatUnixEpoch(secs_since_epoch);
}
uint64_t Snowflake::GetUnixMilliseconds() const noexcept {

View File

@ -63,27 +63,33 @@ int GetTimezoneOffset() {
return static_cast<int>(local_secs - gmt_secs);
}
std::string FormatISO8601(const std::string &in, int extra_offset, const std::string &fmt) {
int yr, mon, day, hr, min, sec, tzhr, tzmin;
float milli;
std::sscanf(in.c_str(), "%d-%d-%dT%d:%d:%d%f+%d:%d",
&yr, &mon, &day, &hr, &min, &sec, &milli, &tzhr, &tzmin);
Glib::ustring FormatUnixEpoch(gint64 time, const std::string &fmt) {
auto dt = Glib::wrap(g_date_time_new_from_unix_utc(time));
#ifdef _WIN32
std::tm tm {};
tm.tm_year = yr - 1900;
tm.tm_mon = mon - 1;
tm.tm_mday = day;
tm.tm_hour = hr;
tm.tm_min = min;
tm.tm_sec = sec;
tm.tm_year = dt.get_year() - 1900;
tm.tm_mon = dt.get_month() - 1;
tm.tm_mday = dt.get_day_of_month();
tm.tm_hour = dt.get_hour() + 1;
tm.tm_min = dt.get_minute();
tm.tm_sec = dt.get_second();
tm.tm_wday = 0;
tm.tm_yday = 0;
tm.tm_isdst = -1;
int offset = GetTimezoneOffset();
tm.tm_sec += offset + extra_offset;
tm.tm_sec += GetTimezoneOffset();
mktime(&tm);
std::array<char, 512> tmp {};
std::strftime(tmp.data(), sizeof(tmp), fmt.c_str(), &tm);
return tmp.data();
#else
return dt.format(fmt);
#endif
}
Glib::ustring FormatISO8601(const std::string &in, int extra_offset, const std::string &fmt) {
const auto epoch = Glib::DateTime::create_from_iso8601(in).add_seconds(extra_offset).to_unix();
return FormatUnixEpoch(epoch);
}
void ScrollListBoxToSelected(Gtk::ListBox &list) {

View File

@ -11,6 +11,7 @@
#include <regex>
#include <mutex>
#include <condition_variable>
#include <glibconfig.h>
#include <optional>
#include <type_traits>
@ -50,7 +51,8 @@ std::string GetExtension(std::string url);
bool IsURLViewableImage(const std::string &url);
std::vector<uint8_t> ReadWholeFile(const std::string &path);
std::string HumanReadableBytes(uint64_t bytes);
std::string FormatISO8601(const std::string &in, int extra_offset = 0, const std::string &fmt = "%x %X");
Glib::ustring FormatUnixEpoch(gint64 time, const std::string &fmt = "%x %X");
Glib::ustring FormatISO8601(const std::string &in, int extra_offset = 0, const std::string &fmt = "%x %X");
void AddPointerCursor(Gtk::Widget &widget);
template<typename T>