Skip to content

Commit

Permalink
fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
lzcunt committed Mar 1, 2025
1 parent 59928fe commit ab951b1
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions options/ansi/generic/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ bool parse_tz(const char *tz, char *tz_name, char *tz_name_dst, size_t tz_name_m
return false;
}

void parse_tzfile(const char *tz, char *tz_name, char *tz_name_dst, size_t tz_name_max) {
void parse_tzfile(const char *tz, size_t tz_name_max) {
// POSIX defines :*characters* as a valid but implementation-defined format.
// This was originally introduced as a way to support geographical
// timezones in the format :Area/Location, but the colon was dropped in POSIX.
Expand Down Expand Up @@ -614,10 +614,11 @@ void parse_tzfile(const char *tz, char *tz_name, char *tz_name_dst, size_t tz_na
}

mlibc::infoLogger() << "mlibc: reading TZ from " << path << frg::endlog;
file_window window {path};

// TODO(geert): we can probably cache this somehow
tzfile tzfile_time;
memcpy(&tzfile_time, reinterpret_cast<char *>(get_localtime_window()->get()), sizeof(tzfile));
memcpy(&tzfile_time, reinterpret_cast<char *>(window.get()), sizeof(tzfile));
tzfile_time.tzh_ttisgmtcnt = mlibc::bit_util<uint32_t>::byteswap(tzfile_time.tzh_ttisgmtcnt);
tzfile_time.tzh_ttisstdcnt = mlibc::bit_util<uint32_t>::byteswap(tzfile_time.tzh_ttisstdcnt);
tzfile_time.tzh_leapcnt = mlibc::bit_util<uint32_t>::byteswap(tzfile_time.tzh_leapcnt);
Expand All @@ -627,12 +628,12 @@ void parse_tzfile(const char *tz, char *tz_name, char *tz_name_dst, size_t tz_na

if(tzfile_time.magic[0] != 'T' || tzfile_time.magic[1] != 'Z' || tzfile_time.magic[2] != 'i'
|| tzfile_time.magic[3] != 'f') {
mlibc::infoLogger() << "mlibc: /etc/localtime is not a valid TZinfo file" << frg::endlog;
mlibc::infoLogger() << "mlibc: " << path << " is not a valid TZinfo file" << frg::endlog;
return;
}

if(tzfile_time.version != '\0' && tzfile_time.version != '2' && tzfile_time.version != '3') {
mlibc::infoLogger() << "mlibc: /etc/localtime has an invalid TZinfo version"
mlibc::infoLogger() << "mlibc: " << path << " has an invalid TZinfo version"
<< frg::endlog;
return;
}
Expand All @@ -641,18 +642,19 @@ void parse_tzfile(const char *tz, char *tz_name, char *tz_name_dst, size_t tz_na
// TODO: If there is not, we might want to fall back to UTC, no DST (?).
__ensure(tzfile_time.tzh_typecnt);

char *abbrevs = reinterpret_cast<char *>(get_localtime_window()->get()) + sizeof(tzfile)
char *abbrevs = reinterpret_cast<char *>(window.get()) + sizeof(tzfile)
+ tzfile_time.tzh_timecnt * sizeof(int32_t)
+ tzfile_time.tzh_timecnt * sizeof(uint8_t)
+ tzfile_time.tzh_typecnt * sizeof(struct ttinfo);
// start from the last ttinfo entry, this matches the behaviour of glibc and musl
for (int i = tzfile_time.tzh_typecnt; i > 0; i--) {
ttinfo time_info;
memcpy(&time_info, reinterpret_cast<char *>(get_localtime_window()->get()) + sizeof(tzfile)
memcpy(&time_info, reinterpret_cast<char *>(window.get()) + sizeof(tzfile)
+ tzfile_time.tzh_timecnt * sizeof(int32_t)
+ tzfile_time.tzh_timecnt * sizeof(uint8_t)
+ i * sizeof(ttinfo), sizeof(ttinfo));
time_info.tt_gmtoff = mlibc::bit_util<uint32_t>::byteswap(time_info.tt_gmtoff);
// TODO: check tz_name_max
if (!time_info.tt_isdst && !tzname[0]) {
tzname[0] = abbrevs + time_info.tt_abbrind;
timezone = -time_info.tt_gmtoff;
Expand Down Expand Up @@ -695,7 +697,7 @@ void tzset(void) {
return;
}

parse_tzfile(tz, tz_name, tz_name_dst, tz_name_max);
parse_tzfile(tz, tz_name_max);
}

// POSIX extensions.
Expand Down

0 comments on commit ab951b1

Please sign in to comment.