Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions include/opencattus/services/timezone.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

#include <list>
#include <map>
#include <optional>
#include <string>
#include <utility>
#include <vector>

/**
Expand Down Expand Up @@ -70,6 +72,37 @@ class Timezone {
*/
std::multimap<std::string, std::string> fetchAvailableTimezones();

#ifdef BUILD_TESTING
// Test-only seam: when set, fetchAvailableTimezones() parses the override
// lines as the zone1970.tab contents instead of reading the host's
// /usr/share/zoneinfo/zone1970.tab. An empty override still exercises the
// timedatectl fallback through the runner singleton. Bare CI hosts carry
// real tzdata state that would otherwise leak into tests expecting
// synthetic timezones. Use Timezone::ScopedTestZone1970Tab for RAII
// teardown rather than poking this directly.
static std::optional<std::vector<std::string>> s_testZone1970Override;

class ScopedTestZone1970Tab {
public:
explicit ScopedTestZone1970Tab(std::vector<std::string> lines)
: m_previous(std::move(s_testZone1970Override))
{
s_testZone1970Override = std::move(lines);
}
ScopedTestZone1970Tab(const ScopedTestZone1970Tab&) = delete;
ScopedTestZone1970Tab& operator=(const ScopedTestZone1970Tab&) = delete;
ScopedTestZone1970Tab(ScopedTestZone1970Tab&&) = delete;
ScopedTestZone1970Tab& operator=(ScopedTestZone1970Tab&&) = delete;
~ScopedTestZone1970Tab()
{
s_testZone1970Override = std::move(m_previous);
}

private:
std::optional<std::vector<std::string>> m_previous;
};
#endif

void setTimezoneArea(std::string_view);
std::string_view getTimezoneArea() const;

Expand Down
22 changes: 10 additions & 12 deletions src/services/timezone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <cstdlib>
#include <filesystem>
#include <fmt/format.h>
#include <fstream>
Expand All @@ -20,7 +19,6 @@
using namespace opencattus;

namespace {
constexpr std::string_view zone1970TabEnv = "OPENCATTUS_ZONE1970_TAB";
constexpr std::string_view zone1970TabPath = "/usr/share/zoneinfo/zone1970.tab";
constexpr std::string_view timedatectlTimezoneCommand
= "timedatectl list-timezones --no-pager";
Expand Down Expand Up @@ -68,19 +66,15 @@ void insertTimezone(
timezones.insert({ tz.substr(0, slash), tz.substr(slash + 1) });
}

std::filesystem::path systemZone1970TabPath()
std::vector<std::string> readZone1970Tab()
{
if (const auto* overridePath = std::getenv(zone1970TabEnv.data());
overridePath != nullptr && std::string_view(overridePath).size() > 0) {
return overridePath;
#ifdef BUILD_TESTING
if (Timezone::s_testZone1970Override.has_value()) {
return parseZone1970Tab(*Timezone::s_testZone1970Override);
}
#endif

return zone1970TabPath;
}

std::vector<std::string> readZone1970Tab()
{
const auto path = systemZone1970TabPath();
const std::filesystem::path path { zone1970TabPath };
std::ifstream file(path);
if (!file.is_open()) {
LOG_DEBUG(
Expand All @@ -103,6 +97,10 @@ std::vector<std::string> readZone1970Tab()
}
}

#ifdef BUILD_TESTING
std::optional<std::vector<std::string>> Timezone::s_testZone1970Override;
#endif

Timezone::Timezone()
: m_availableTimezones { fetchAvailableTimezones() }
{
Expand Down
Loading