Files

58 lines
1.8 KiB
C++

#include "timezone_helper.h"
#include <gtest/gtest.h>
#include <ctime>
using namespace radiantwave::timezone;
TEST(TimeZoneTest, GetStandardTimeZonesNonEmpty) {
const auto& countries = getStandardTimeZones();
EXPECT_FALSE(countries.empty());
bool found_us = false;
bool found_utc = false;
for (const auto& c : countries) {
if (c.country_name == "United States") found_us = true;
if (c.country_name == "Coordinated Universal Time") found_utc = true;
}
EXPECT_TRUE(found_us);
EXPECT_TRUE(found_utc);
}
TEST(TimeZoneTest, UtcTimestampFormat) {
std::string iso = getUtcTimestampIso();
EXPECT_FALSE(iso.empty());
EXPECT_NE(iso.find("UTC"), std::string::npos);
}
TEST(TimeZoneTest, CurrentLocalMinutesInRangeAndMatchesUtc) {
int minutes = currentLocalMinutes("UTC");
EXPECT_GE(minutes, 0);
EXPECT_LT(minutes, 1440);
// Cross-check against the UTC wall clock. The two calls happen within the
// same second, so a minute-boundary rollover is vanishingly unlikely.
time_t now = std::time(nullptr);
struct tm gmt_buf;
gmtime_r(&now, &gmt_buf);
EXPECT_EQ(minutes, gmt_buf.tm_hour * 60 + gmt_buf.tm_min);
// A timezone that is known to differ from UTC must still be in range.
int ny = currentLocalMinutes("America/New_York");
EXPECT_GE(ny, 0);
EXPECT_LT(ny, 1440);
}
TEST(TimeZoneTest, FormatUtcToTimezone) {
// 2026-07-24 12:00:00 UTC
// Fixed epoch timestamp: 1784808000
time_t fixed_utc = 1784808000;
std::string formatted_ny = formatUtcToTimezone(fixed_utc, "America/New_York", "%Y-%m-%d %H:%M:%S");
std::string formatted_utc = formatUtcToTimezone(fixed_utc, "UTC", "%Y-%m-%d %H:%M:%S");
EXPECT_FALSE(formatted_ny.empty());
EXPECT_EQ(formatted_utc, "2026-07-23 12:00:00");
}