1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 07:07:45 +00:00

LibC: Cache the time zone name found by tzset()

The time zone name will be needed for TZDB lookups in various time.h
functions. Cache the value found by tzset(), defaulting to the system-
wide default of UTC.

This also moves the time.h global definitions to the top of the file.
The cached time zone name will be needed above where these variables are
defined, so this is just to keep them all together.
This commit is contained in:
Timothy Flynn 2022-08-01 14:39:41 -04:00 committed by Linus Groh
parent de9452e14b
commit 91e3a2aaca

View file

@ -25,6 +25,16 @@
extern "C" {
static constexpr char const* __utc = "UTC";
static StringView __tzname { __utc, __builtin_strlen(__utc) };
static char __tzname_standard[TZNAME_MAX];
static char __tzname_daylight[TZNAME_MAX];
long timezone = 0;
long altzone = 0;
char* tzname[2] = { const_cast<char*>(__utc), const_cast<char*>(__utc) };
int daylight = 0;
time_t time(time_t* tloc)
{
struct timeval tv;
@ -362,35 +372,25 @@ size_t strftime(char* destination, size_t max_size, char const* format, const st
return fits ? str.length() : 0;
}
static char __tzname_standard[TZNAME_MAX];
static char __tzname_daylight[TZNAME_MAX];
constexpr char const* __utc = "UTC";
long timezone = 0;
long altzone = 0;
char* tzname[2] = { const_cast<char*>(__utc), const_cast<char*>(__utc) };
int daylight = 0;
void tzset()
{
// FIXME: Actually parse the TZ environment variable, described here:
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08
StringView time_zone;
if (char* tz = getenv("TZ"); tz != nullptr)
time_zone = { tz, strlen(tz) };
__tzname = { tz, strlen(tz) };
else
time_zone = TimeZone::system_time_zone();
__tzname = TimeZone::system_time_zone();
auto set_default_values = []() {
timezone = 0;
altzone = 0;
daylight = 0;
__tzname = StringView { __utc, __builtin_strlen(__utc) };
tzname[0] = const_cast<char*>(__utc);
tzname[1] = const_cast<char*>(__utc);
};
if (auto offsets = TimeZone::get_named_time_zone_offsets(time_zone, AK::Time::now_realtime()); offsets.has_value()) {
if (auto offsets = TimeZone::get_named_time_zone_offsets(__tzname, AK::Time::now_realtime()); offsets.has_value()) {
if (!offsets->at(0).name.copy_characters_to_buffer(__tzname_standard, TZNAME_MAX))
return set_default_values();
if (!offsets->at(1).name.copy_characters_to_buffer(__tzname_daylight, TZNAME_MAX))