Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to read timezone file from $CONDA_PREFIX dir before trying the system dir #15214

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions cpp/src/datetime/timezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,38 @@ struct dst_transition_s {
};
#pragma pack(pop)

[[nodiscard]] std::pair<std::ifstream, size_t> open_tz_file(
std::optional<std::string_view> tzif_dir, std::string_view timezone_name)
{
using std::ios_base;

std::vector<std::filesystem::path> search_paths;
if (tzif_dir.has_value()) {
search_paths.push_back(tzif_dir.value());
} else {
auto const conda_prefix = std::getenv("CONDA_PREFIX");
if (conda_prefix != nullptr) {
search_paths.push_back(std::filesystem::path{conda_prefix} / "share" / "zoneinfo");
}
search_paths.push_back(tzif_system_directory);
}

for (auto const& path : search_paths) {
auto const tz_filename = path / timezone_name;
if (std::filesystem::exists(tz_filename)) {
std::ifstream fin;
fin.open(tz_filename, ios_base::in | ios_base::binary | ios_base::ate);
if (fin.is_open()) {
auto const file_size = fin.tellg();
fin.seekg(0);
return {std::move(fin), file_size};
}
}
}

CUDF_FAIL("Timezone file not found.");
}

struct timezone_file {
timezone_file_header header;
bool is_header_from_64bit = false;
Expand Down Expand Up @@ -130,16 +162,7 @@ struct timezone_file {

timezone_file(std::optional<std::string_view> tzif_dir, std::string_view timezone_name)
{
using std::ios_base;

// Open the input file
auto const tz_filename =
std::filesystem::path{tzif_dir.value_or(tzif_system_directory)} / timezone_name;
std::ifstream fin;
fin.open(tz_filename, ios_base::in | ios_base::binary | ios_base::ate);
CUDF_EXPECTS(fin, "Failed to open the timezone file.");
auto const file_size = fin.tellg();
fin.seekg(0);
auto [fin, file_size] = open_tz_file(tzif_dir, timezone_name);

read_header(fin, file_size);

Expand Down Expand Up @@ -173,7 +196,7 @@ struct timezone_file {
// Read posix TZ string
fin.seekg(header.charcnt + header.leapcnt * leap_second_rec_size(is_header_from_64bit) +
header.isstdcnt + header.isutccnt,
ios_base::cur);
std::ios_base::cur);
auto const file_pos = fin.tellg();
if (file_size - file_pos > 1) {
posix_tz_string.resize(file_size - file_pos);
Expand Down
Loading