61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
/*
|
|
PianoLibrary
|
|
Copyright (C) 2025 OmniaX-Dev
|
|
|
|
This file is part of PianoLibrary.
|
|
|
|
PianoLibrary is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
PianoLibrary is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with PianoLibrary. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <ostd/string/String.hpp>
|
|
|
|
namespace utc
|
|
{
|
|
// Portable timegm: temporarily override TZ to UTC, call mktime, restore.
|
|
// Works on POSIX, MSVC, MinGW, MSYS2 — everywhere mktime exists.
|
|
static std::time_t portable_timegm(std::tm* tm)
|
|
{
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
|
// _mkgmtime is MSVC/UCRT's timegm equivalent, also available in MSYS2 UCRT64.
|
|
return _mkgmtime(tm);
|
|
#else
|
|
return timegm(tm);
|
|
#endif
|
|
}
|
|
|
|
static String toLocal(const String& iso)
|
|
{
|
|
std::tm tm = {};
|
|
|
|
// Manual parse — avoids strptime, which MSVC lacks entirely.
|
|
// Expected format: "2026-06-20T14:00:00Z"
|
|
if (std::sscanf(iso.c_str(), "%d-%d-%dT%d:%d:%dZ",
|
|
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
|
|
&tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6)
|
|
return iso; // Unparseable -> return as-is.
|
|
|
|
tm.tm_year -= 1900;
|
|
tm.tm_mon -= 1;
|
|
tm.tm_isdst = -1;
|
|
|
|
std::time_t utc = portable_timegm(&tm);
|
|
std::tm* local = std::localtime(&utc);
|
|
|
|
char buf[64];
|
|
std::strftime(buf, sizeof(buf), "%d %b %Y, %H:%M %Z", local);
|
|
return buf;
|
|
}
|
|
}
|