|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#include <cudf_test/base_fixture.hpp> |
| 7 | +#include <cudf_test/table_utilities.hpp> |
| 8 | +#include <cudf_test/testing_main.hpp> |
| 9 | + |
| 10 | +#include <cudf/table/table.hpp> |
| 11 | +#include <cudf/timezone.hpp> |
| 12 | +#include <cudf/utilities/error.hpp> |
| 13 | + |
| 14 | +#include <array> |
| 15 | +#include <cerrno> |
| 16 | +#include <cstdlib> |
| 17 | +#include <cstring> |
| 18 | +#include <filesystem> |
| 19 | +#include <fstream> |
| 20 | +#include <optional> |
| 21 | +#include <string> |
| 22 | +#include <string_view> |
| 23 | +#include <system_error> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +constexpr std::string_view canonical_zone_name = "America/Los_Angeles"; |
| 29 | + |
| 30 | +// Candidate locations for the system TZif database, probed in order. |
| 31 | +constexpr std::array<std::string_view, 3> candidate_tz_dirs{ |
| 32 | + "/usr/share/zoneinfo", |
| 33 | + "/usr/lib/zoneinfo", |
| 34 | + "/etc/zoneinfo", |
| 35 | +}; |
| 36 | + |
| 37 | +std::optional<std::filesystem::path> find_system_tz_dir() |
| 38 | +{ |
| 39 | + static std::optional<std::filesystem::path> const cached = [] { |
| 40 | + namespace fs = std::filesystem; |
| 41 | + auto const usable = [](fs::path const& dir) { |
| 42 | + std::error_code ec; |
| 43 | + return fs::is_regular_file(dir / canonical_zone_name, ec); |
| 44 | + }; |
| 45 | + if (auto const* env = std::getenv("TZDIR")) { |
| 46 | + if (fs::path const d{env}; usable(d)) { return std::optional{d}; } |
| 47 | + } |
| 48 | + for (auto const sv : candidate_tz_dirs) { |
| 49 | + if (fs::path const d{sv}; usable(d)) { return std::optional{d}; } |
| 50 | + } |
| 51 | + return std::optional<fs::path>{}; |
| 52 | + }(); |
| 53 | + return cached; |
| 54 | +} |
| 55 | + |
| 56 | +} // namespace |
| 57 | + |
| 58 | +class TimezoneTransitionTableTest : public cudf::test::BaseFixture {}; |
| 59 | + |
| 60 | +TEST_F(TimezoneTransitionTableTest, UtcShortCircuitsWithoutReadingFile) |
| 61 | +{ |
| 62 | + auto const table = cudf::make_timezone_transition_table(std::nullopt, "UTC"); |
| 63 | + EXPECT_EQ(table->num_rows(), 0); |
| 64 | + EXPECT_EQ(table->num_columns(), 0); |
| 65 | +} |
| 66 | + |
| 67 | +TEST_F(TimezoneTransitionTableTest, EmptyZoneNameShortCircuitsWithoutReadingFile) |
| 68 | +{ |
| 69 | + auto const table = cudf::make_timezone_transition_table(std::nullopt, ""); |
| 70 | + EXPECT_EQ(table->num_rows(), 0); |
| 71 | + EXPECT_EQ(table->num_columns(), 0); |
| 72 | +} |
| 73 | + |
| 74 | +TEST_F(TimezoneTransitionTableTest, CanonicalZoneProducesTwoColumnTable) |
| 75 | +{ |
| 76 | + auto const tz_dir = find_system_tz_dir(); |
| 77 | + if (!tz_dir) { GTEST_SKIP() << "No system zoneinfo directory with " << canonical_zone_name; } |
| 78 | + |
| 79 | + auto const table = cudf::make_timezone_transition_table(tz_dir->string(), canonical_zone_name); |
| 80 | + ASSERT_EQ(table->num_columns(), 2); |
| 81 | + // Sanity: the future cycle dominates the row count, so we expect hundreds of rows. |
| 82 | + EXPECT_GT(table->num_rows(), 100); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(TimezoneTransitionTableTest, UnknownZoneThrows) |
| 86 | +{ |
| 87 | + auto const tz_dir = find_system_tz_dir(); |
| 88 | + if (!tz_dir) { GTEST_SKIP() << "No system zoneinfo directory with " << canonical_zone_name; } |
| 89 | + |
| 90 | + EXPECT_THROW(cudf::make_timezone_transition_table(tz_dir->string(), "Not_A/Real_Zone_bXYZ"), |
| 91 | + cudf::logic_error); |
| 92 | +} |
| 93 | + |
| 94 | +class TimezoneAliasResolutionTest : public cudf::test::BaseFixture { |
| 95 | + protected: |
| 96 | + void SetUp() override |
| 97 | + { |
| 98 | + // make the directory name process-unique |
| 99 | + auto const tmpl = |
| 100 | + (std::filesystem::temp_directory_path() / (std::string{"cudf_tz_alias_test_"} + ".XXXXXX")) |
| 101 | + .string(); |
| 102 | + std::vector<char> buf(tmpl.begin(), tmpl.end()); |
| 103 | + buf.push_back('\0'); |
| 104 | + ASSERT_NE(::mkdtemp(buf.data()), nullptr) << "mkdtemp failed: " << std::strerror(errno); |
| 105 | + tz_dir_ = buf.data(); |
| 106 | + } |
| 107 | + |
| 108 | + void TearDown() override |
| 109 | + { |
| 110 | + std::error_code ec; |
| 111 | + std::filesystem::remove_all(tz_dir_, ec); |
| 112 | + } |
| 113 | + |
| 114 | + [[nodiscard]] bool install_zone(std::string_view zone_name) const |
| 115 | + { |
| 116 | + auto const src_dir = find_system_tz_dir(); |
| 117 | + if (!src_dir) { return false; } |
| 118 | + |
| 119 | + std::error_code ec; |
| 120 | + auto const dst = tz_dir_ / zone_name; |
| 121 | + std::filesystem::create_directories(dst.parent_path(), ec); |
| 122 | + if (ec) { |
| 123 | + ADD_FAILURE() << "create_directories(" << dst.parent_path() << ") failed: " << ec.message(); |
| 124 | + return false; |
| 125 | + } |
| 126 | + std::filesystem::copy_file( |
| 127 | + *src_dir / canonical_zone_name, dst, std::filesystem::copy_options::overwrite_existing, ec); |
| 128 | + if (ec) { |
| 129 | + ADD_FAILURE() << "copy_file(" << (*src_dir / canonical_zone_name) << " -> " << dst |
| 130 | + << ") failed: " << ec.message(); |
| 131 | + return false; |
| 132 | + } |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + void write_tzdata_zi(std::string_view contents) const |
| 137 | + { |
| 138 | + std::ofstream{tz_dir_ / "tzdata.zi"} << contents; |
| 139 | + } |
| 140 | + |
| 141 | + [[nodiscard]] std::string dir() const { return tz_dir_.string(); } |
| 142 | + |
| 143 | + private: |
| 144 | + std::filesystem::path tz_dir_; |
| 145 | +}; |
| 146 | + |
| 147 | +TEST_F(TimezoneAliasResolutionTest, DirectLookupUnaffectedByNewFallback) |
| 148 | +{ |
| 149 | + if (!install_zone(canonical_zone_name)) { |
| 150 | + GTEST_SKIP() << "No system zoneinfo directory with " << canonical_zone_name; |
| 151 | + } |
| 152 | + |
| 153 | + auto const table = cudf::make_timezone_transition_table(dir(), canonical_zone_name); |
| 154 | + EXPECT_GT(table->num_rows(), 0); |
| 155 | + EXPECT_EQ(table->num_columns(), 2); |
| 156 | +} |
| 157 | + |
| 158 | +TEST_F(TimezoneAliasResolutionTest, ResolvesShortFormLinkFromTzdataZi) |
| 159 | +{ |
| 160 | + if (!install_zone(canonical_zone_name)) { |
| 161 | + GTEST_SKIP() << "No system zoneinfo directory with " << canonical_zone_name; |
| 162 | + } |
| 163 | + write_tzdata_zi( |
| 164 | + "# synthetic tzdata.zi for libcudf tests\n" |
| 165 | + "L America/Los_Angeles US/Pacific\n"); |
| 166 | + |
| 167 | + auto const via_canonical = cudf::make_timezone_transition_table(dir(), canonical_zone_name); |
| 168 | + auto const via_alias = cudf::make_timezone_transition_table(dir(), "US/Pacific"); |
| 169 | + CUDF_TEST_EXPECT_TABLES_EQUAL(via_canonical->view(), via_alias->view()); |
| 170 | +} |
| 171 | + |
| 172 | +TEST_F(TimezoneAliasResolutionTest, ResolvesLongFormLinkFromTzdataZi) |
| 173 | +{ |
| 174 | + if (!install_zone(canonical_zone_name)) { |
| 175 | + GTEST_SKIP() << "No system zoneinfo directory with " << canonical_zone_name; |
| 176 | + } |
| 177 | + write_tzdata_zi("Link America/Los_Angeles US/Pacific\n"); |
| 178 | + |
| 179 | + auto const via_canonical = cudf::make_timezone_transition_table(dir(), canonical_zone_name); |
| 180 | + auto const via_alias = cudf::make_timezone_transition_table(dir(), "US/Pacific"); |
| 181 | + CUDF_TEST_EXPECT_TABLES_EQUAL(via_canonical->view(), via_alias->view()); |
| 182 | +} |
| 183 | + |
| 184 | +TEST_F(TimezoneAliasResolutionTest, ResolvesChainedLinks) |
| 185 | +{ |
| 186 | + if (!install_zone(canonical_zone_name)) { |
| 187 | + GTEST_SKIP() << "No system zoneinfo directory with " << canonical_zone_name; |
| 188 | + } |
| 189 | + // Neither "intermediate" nor "US/Pacific" exists on disk. The resolver must traverse |
| 190 | + // US/Pacific -> intermediate -> America/Los_Angeles to find a real file. |
| 191 | + write_tzdata_zi( |
| 192 | + "L America/Los_Angeles intermediate\n" |
| 193 | + "L intermediate US/Pacific\n"); |
| 194 | + |
| 195 | + auto const via_canonical = cudf::make_timezone_transition_table(dir(), canonical_zone_name); |
| 196 | + auto const via_alias = cudf::make_timezone_transition_table(dir(), "US/Pacific"); |
| 197 | + CUDF_TEST_EXPECT_TABLES_EQUAL(via_canonical->view(), via_alias->view()); |
| 198 | +} |
| 199 | + |
| 200 | +TEST_F(TimezoneAliasResolutionTest, ThrowsWhenLinkTargetIsAlsoMissing) |
| 201 | +{ |
| 202 | + // No zone files installed in tz_dir_. |
| 203 | + write_tzdata_zi("L Also/Missing US/Pacific\n"); |
| 204 | + |
| 205 | + EXPECT_THROW(cudf::make_timezone_transition_table(dir(), "US/Pacific"), cudf::logic_error); |
| 206 | +} |
| 207 | + |
| 208 | +TEST_F(TimezoneAliasResolutionTest, ThrowsWhenNoTzdataZiPresent) |
| 209 | +{ |
| 210 | + EXPECT_THROW(cudf::make_timezone_transition_table(dir(), "US/Pacific"), cudf::logic_error); |
| 211 | +} |
| 212 | + |
| 213 | +TEST_F(TimezoneAliasResolutionTest, IgnoresCommentsAndNonLinkDirectives) |
| 214 | +{ |
| 215 | + if (!install_zone(canonical_zone_name)) { |
| 216 | + GTEST_SKIP() << "No system zoneinfo directory with " << canonical_zone_name; |
| 217 | + } |
| 218 | + write_tzdata_zi( |
| 219 | + "# a leading comment\n" |
| 220 | + "\n" |
| 221 | + "R SomeRule 1970 o - Jan 1 0 0 S\n" // `Rule` entry, must be ignored |
| 222 | + "Z Fake/Zone 0 - LMT\n" // `Zone` entry, must be ignored |
| 223 | + " L America/Los_Angeles US/Pacific\n" // link with leading whitespace |
| 224 | + "# trailing comment\n"); |
| 225 | + |
| 226 | + auto const via_canonical = cudf::make_timezone_transition_table(dir(), canonical_zone_name); |
| 227 | + auto const via_alias = cudf::make_timezone_transition_table(dir(), "US/Pacific"); |
| 228 | + CUDF_TEST_EXPECT_TABLES_EQUAL(via_canonical->view(), via_alias->view()); |
| 229 | +} |
| 230 | + |
| 231 | +CUDF_TEST_PROGRAM_MAIN() |
0 commit comments