mirror of
https://github.com/gabime/spdlog.git
synced 2026-04-10 11:34:29 +08:00
Synchronize v2.x With Changes from v1.x (#3581)
* docs(tasks): v1.x inventory, triage template, merge report; update task checklist - Record merge-base and export v1-only commits (v2..v1) - Document failed merge attempt: v2 file tree diverges from v1 - Mark completed checklist items 0.x, 1.x, merge report draft (5.2) Made-with: Cursor * fix(pattern): %z with UTC pattern time shows +00:00 (port v109a674b7) - z_formatter takes pattern_time_type; emit +00:00 when pattern uses UTC - Track port in tasks/commits-ported.txt and merge-report; update checklist Made-with: Cursor * fix(os): Windows utc_minutes_offset via mktime/_mkgmtime (port v1b656d1ce) - Replace GetTimeZoneInformation-based offset with v1.x mktime/_mkgmtime approach - Add tests/test_timezone.cpp and extend pattern_formatter tests; update os.h comment Made-with: Cursor * test(timezone): POSIX TZ with DST rules; include fcntl in tcp_client_unix (v1 ports) -0f7562a0: EST5EDT / IST-2IDT macros for POSIX vs Windows -d2100d5d: fcntl.h for Unix tcp client header (v2 path) Made-with: Cursor * feat(tcp): connect timeouts and socket IO timeouts; ci: checkout@v6 (v1 ports) -9ecdf5c8: connect_socket_with_timeout on Unix/Windows; tcp_sink timeout_ms -3c61b051: actions/checkout@v6 on all workflows - tcp_sink: remove duplicate #pragma once Made-with: Cursor * feat(dup_filter_sink): ctor with sink list (v145b67eee); add full v1 SHA triage table - tasks/v1-triage-complete.md: 245 commits PORTED/PENDING/N/A for PRD 3A tracking - Merge report and task checklist updated Made-with: Cursor * feat(level): case-insensitive level_from_str; cmake: BUILD_TYPE only if top-level (v1 ports) -566b2d14: common.cpp + test_misc (SUPERSEDED note ford5af52d9in triage) -dd3ca04a: guard default CMAKE_BUILD_TYPE for add_subdirectory consumers - Update v1-triage-complete.md counts and statuses Made-with: Cursor * v1 parity: MSVC UTF-8, ansicolor/syslog/os, getenv, triage docs - CMake: SPDLOG_MSVC_UTF8 + /utf-8 for real MSVC only - ansicolor: protected target_file_; fix set_color_mode_ lock nesting - os: drop redundant fileapi.h; spdlog::details::os::getenv via std::getenv - tests: stopwatch waits 500ms; includes/triage/commits-ported/merge-report updates - Reclassify superseded v1 SHAs (Sep, syslog, stopwatch ms, utf8 tests, etc.) Made-with: Cursor * parity: ringbuffer zero capacity, utf8 assert; v1 triage sync - ringbuffer_sink: throw if n_items==0 (#3436); test expects spdlog_ex - utf8_to_wstrbuf: assert compares int to static_cast<int>(target.size()) (#3479) - Triage: PORTEDad725d34getenv,677a2d93stopwatch,3f7e5028, a6215527; SUPERSEDEDa45c9390,eeb22c13,5673e9e5,fe4f9952,287333ee- tasks: 27 PORTED / 26 SUPERSEDED / 78 PENDING; merge-report + commits-ported Made-with: Cursor * docs(tasks): note regular commits and push after parity ports Made-with: Cursor * parity: dup_filter_sink notification level from last duplicate (#3390) - Remove notification_level ctor arg; track skipped_msg_log_level_ on duplicate skips - Test: skipped summary line uses same short level as duplicated messages - Triage:847db337PORTED; 28/26/77 counts; merge-report + commits-ported Made-with: Cursor * parity: UWP getenv (WINAPI_FAMILY); triage fmt/cfg/no-except - os_windows: detect non-desktop/UWP for empty getenv (#3489) - Triage: PORTED 8806ca65; SUPERSEDEDe3f5a4fee655dbb6; N/Aae1de0dc548b2642- Counts 29/28/116/72; merge-report + commits-ported Made-with: Cursor * parity: qt_sinks sign casts (#3487); triage9c582574superseded - qt_color_sink: qsizetype for UTF-8 color range lengths; index colors_ with size_t - SUPERSEDED:9c582574— os_unix utc_minutes_offset already matches #3366 - Counts 30/29/70; merge-report + commits-ported Made-with: Cursor * parity: SPDLOG_NO_TZ_OFFSET (#3483); triage #3360 superseded - CMake: option SPDLOG_NO_TZ_OFFSET; PUBLIC compile definition when ON - z_formatter: +??:?? when macro; else keep UTC +00:00 and local offset - utc_minutes_offset: stub on Unix/Windows when macro - test_pattern_formatter: %z UTC case matches placeholder when NO_TZ - SUPERSEDED10320184(ScopedPadder / %D already in v2) - Triage 31/30/68; merge-report + commits-ported Made-with: Cursor * triage: supersede5931a3d6ba50805747b7e7c7(already on v2 tree) Made-with: Cursor * tasks: sync 5.1 triage counts (33 superseded, 65 pending) Made-with: Cursor * parity: MSVC/clang hygiene #3515–#3519 #3521; triage batch - dup_filter_sink: filter_ const, const filter_duration (#3515) - logger: should_flush uses flush_level() (#3516) - daily_file_sink: new_filename locals (#3516) - spdlog::should_log(level log_level) (#3519) - example my_type value_ / ctor param (#3521) - Triage: PORTED1774e700309204d5f2a9dec0472945ba; N/Ad299603e57505989; SUPERSEDED1ef8d3ce8cfd4a7e; fixba508057row Made-with: Cursor * parity: udp_sink const udp_sink_config& (#3520); triagefc7e9c871685e694- dist_sink already used std::move(sinks) - SUPERSEDED: no common-inl.h on v2; log_with_format_ avoids fmt copy path (#3541) Made-with: Cursor * parity: README fmt::format_to (#3259); triage2670f47dd276069a951c5b99- Document ambiguous format_to fix in README user-defined type example - SUPERSEDED: z_formatter warning, fmt11 const formatter, rotate_now + test Made-with: Cursor * parity: lock rotate_now mutex (#3281); triagea2b42620f355b3d5d276069a- rotating_file_sink::rotate_now matches #3281 (sync with sink_it_) - SUPERSEDED: CMake 3.10..3.21 (v2 uses 3.23); daily test fmt::format; fmt11 const row Made-with: Cursor * parity: basic_file_sink::truncate (#3280); triage fwrite/fmt/test batch - truncate(): lock + file_helper::reopen(true) - test basic_file_sink_truncate - SUPERSEDED:b7e0e2c271925ca3fa6605dc885b547396c9a62b1e6250e1d7155530Made-with: Cursor * parity: test_sink/callback iterator cast (#3315); triage Catch2 #3038 - difference_type cast for formatted.end() - eol_len (ad0f31c0) - SUPERSEDED:c1569a3dCatch2 v3.5.0,73e2e02bwstr_to_utf8buf bounds Made-with: Cursor * parity: SPDLOG_WCHAR_CONSOLE WriteConsoleW path (#3092); triageb6da5944- WIN32 option SPDLOG_WCHAR_CONSOLE; SPDLOG_UTF8_TO_WCHAR_CONSOLE in private defs - wincolor print_range_: utf8_to_wstrbuf + WriteConsoleW when defined - N/A: async_msg flush_callback move-assign (no v1 shape on v2) Made-with: Cursor * triage: v1 async_logger/thread_pool + fmt 11.1 batch (15 SHAs) - N/A:fe79bfcc6725584easync tests16e0d2e763d18842d8e0ad461e7d7e073c23c27d- SUPERSEDED:faa0a7a985bdab0c276ee5f57f8060d596a8f625- Counts: 58 SUPERSEDED, 129 N/A, 17 PENDING; merge-report subsection Made-with: Cursor * Port v1 TSAN CMake (#3237); close 3A triage (MDC N/A, fmt 5A) Made-with: Cursor * docs(triage): note 3A table complete Made-with: Cursor * 5A: bundle fmt 12.1.0 (match v1.x), FMT_INSTALL, MSVC /wd4834, find_dependency(fmt 12) Made-with: Cursor * docs(5.4): migration and release notes for v1 parity integration branch Made-with: Cursor * docs: cross-link migration notes; refresh PRD/tasks/merge-report for completed 5A/3A Made-with: Cursor * revert: remove README and PRD cross-links to migration notes Made-with: Cursor * docs(merge-report): audit — add9fe79692to ports table; fix6004e3d1paths Made-with: Cursor * Remove tasks * PR comment fixes * Fix small random issues * Fix PR comments - un-remove the comment and allow any fmt library version --------- Co-authored-by: Jan Moravec <jan.moravec@hidglobal.com>
This commit is contained in:
@@ -52,6 +52,7 @@ set(SPDLOG_UTESTS_SOURCES
|
||||
test_log_level.cpp
|
||||
test_include_sinks.cpp
|
||||
test_bin_to_hex.cpp
|
||||
test_timezone.cpp
|
||||
test_errors.cpp)
|
||||
|
||||
if(WIN32)
|
||||
|
||||
@@ -25,6 +25,9 @@
|
||||
|
||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
|
||||
|
||||
#undef SPDLOG_LEVEL_NAMES
|
||||
#undef SPDLOG_SHORT_LEVEL_NAMES
|
||||
|
||||
#include "spdlog/details/fmt_helper.h"
|
||||
#include "spdlog/pattern_formatter.h"
|
||||
#include "spdlog/sinks/null_sink.h"
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
* https://raw.githubusercontent.com/gabime/spdlog/v2.x/LICENSE
|
||||
*/
|
||||
#include "includes.h"
|
||||
|
||||
#include <iterator>
|
||||
|
||||
#include "spdlog/common.h"
|
||||
#include "spdlog/sinks/callback_sink.h"
|
||||
#include "test_sink.h"
|
||||
@@ -14,7 +17,8 @@ TEST_CASE("custom_callback_logger", "[custom_callback_logger]") {
|
||||
spdlog::memory_buf_t formatted;
|
||||
formatter.format(msg, formatted);
|
||||
auto eol_len = strlen(spdlog::details::os::default_eol);
|
||||
lines.emplace_back(formatted.begin(), formatted.end() - eol_len);
|
||||
using diff_t = typename std::iterator_traits<decltype(formatted.end())>::difference_type;
|
||||
lines.emplace_back(formatted.begin(), formatted.end() - static_cast<diff_t>(eol_len));
|
||||
});
|
||||
std::shared_ptr<spdlog::sinks::test_sink_st> test_sink(new spdlog::sinks::test_sink_st);
|
||||
|
||||
|
||||
@@ -80,3 +80,41 @@ TEST_CASE("dup_filter_test5", "[dup_filter_sink]") {
|
||||
REQUIRE(test_sink->msg_counter() == 3); // skip 2 messages but log the "skipped.." message before message2
|
||||
REQUIRE(test_sink->lines()[1] == "Skipped 2 duplicate messages..");
|
||||
}
|
||||
|
||||
TEST_CASE("dup_filter_skipped_notification_uses_last_duplicate_level", "[dup_filter_sink]") {
|
||||
using spdlog::sinks::dup_filter_sink_st;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
dup_filter_sink_st dup_sink{std::chrono::seconds{5}};
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
test_sink->set_pattern("%L");
|
||||
dup_sink.add_sink(test_sink);
|
||||
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "diff"});
|
||||
|
||||
REQUIRE(test_sink->lines().size() == 3);
|
||||
REQUIRE(test_sink->lines()[0] == "W");
|
||||
REQUIRE(test_sink->lines()[1] == "W");
|
||||
REQUIRE(test_sink->lines()[2] == "I");
|
||||
}
|
||||
|
||||
TEST_CASE("dup_filter_skipped_notification_fixed_level", "[dup_filter_sink]") {
|
||||
using spdlog::sinks::dup_filter_sink_st;
|
||||
using spdlog::sinks::test_sink_mt;
|
||||
|
||||
dup_filter_sink_st dup_sink{std::chrono::seconds{5}, spdlog::level::info};
|
||||
auto test_sink = std::make_shared<test_sink_mt>();
|
||||
test_sink->set_pattern("%L");
|
||||
dup_sink.add_sink(test_sink);
|
||||
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::warn, "same"});
|
||||
dup_sink.log(spdlog::details::log_msg{"test", spdlog::level::info, "diff"});
|
||||
|
||||
REQUIRE(test_sink->lines().size() == 3);
|
||||
REQUIRE(test_sink->lines()[0] == "W");
|
||||
REQUIRE(test_sink->lines()[1] == "I");
|
||||
REQUIRE(test_sink->lines()[2] == "I");
|
||||
}
|
||||
|
||||
@@ -41,6 +41,26 @@ TEST_CASE("flush_on", "[flush_on]") {
|
||||
default_eol, default_eol, default_eol));
|
||||
}
|
||||
|
||||
TEST_CASE("basic_file_sink_truncate", "[truncate]") {
|
||||
prepare_logdir();
|
||||
const spdlog::filename_t filename = SPDLOG_FILENAME_T(SIMPLE_LOG);
|
||||
const bool truncate = true;
|
||||
const auto sink = std::make_shared<basic_file_sink_mt>(filename, truncate);
|
||||
const auto logger = std::make_shared<spdlog::logger>("simple_file_logger", sink);
|
||||
|
||||
logger->info("Test message {}", 3.14);
|
||||
logger->info("Test message {}", 2.71);
|
||||
logger->flush();
|
||||
REQUIRE(count_lines(SIMPLE_LOG) == 2);
|
||||
|
||||
sink->truncate();
|
||||
REQUIRE(count_lines(SIMPLE_LOG) == 0);
|
||||
|
||||
logger->info("Test message {}", 6.28);
|
||||
logger->flush();
|
||||
REQUIRE(count_lines(SIMPLE_LOG) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]") {
|
||||
prepare_logdir();
|
||||
size_t max_size = 1024 * 10;
|
||||
|
||||
@@ -72,6 +72,17 @@ TEST_CASE("to_level_enum", "[convert_to_level_enum]") {
|
||||
REQUIRE(spdlog::level_from_str("critical") == spdlog::level::critical);
|
||||
REQUIRE(spdlog::level_from_str("off") == spdlog::level::off);
|
||||
REQUIRE(spdlog::level_from_str("null") == spdlog::level::off);
|
||||
REQUIRE(spdlog::level_from_str("TRACE") == spdlog::level::trace);
|
||||
REQUIRE(spdlog::level_from_str("DEBUG") == spdlog::level::debug);
|
||||
REQUIRE(spdlog::level_from_str("INFO") == spdlog::level::info);
|
||||
REQUIRE(spdlog::level_from_str("WARNING") == spdlog::level::warn);
|
||||
REQUIRE(spdlog::level_from_str("WARN") == spdlog::level::warn);
|
||||
REQUIRE(spdlog::level_from_str("ERROR") == spdlog::level::err);
|
||||
REQUIRE(spdlog::level_from_str("ERR") == spdlog::level::err);
|
||||
REQUIRE(spdlog::level_from_str("CRITICAL") == spdlog::level::critical);
|
||||
REQUIRE(spdlog::level_from_str("OFF") == spdlog::level::off);
|
||||
REQUIRE(spdlog::level_from_str("TrAcE") == spdlog::level::trace);
|
||||
REQUIRE(spdlog::level_from_str("DeBuG") == spdlog::level::debug);
|
||||
}
|
||||
|
||||
TEST_CASE("copy_ctor", "[copy_ctor]") {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "test_sink.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <regex>
|
||||
|
||||
using spdlog::memory_buf_t;
|
||||
|
||||
@@ -69,13 +70,34 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
|
||||
REQUIRE(log_to_str("Some message", "%D %v", spdlog::pattern_time_type::local, "\n") == oss.str());
|
||||
}
|
||||
|
||||
TEST_CASE("GMT offset ", "[pattern_formatter]") {
|
||||
TEST_CASE("%z with UTC pattern time", "[pattern_formatter]") {
|
||||
using namespace std::chrono_literals;
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
const auto yesterday = now - 24h;
|
||||
|
||||
#ifndef SPDLOG_NO_TZ_OFFSET
|
||||
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc, "\n") ==
|
||||
"+00:00\n");
|
||||
#else
|
||||
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc, "\n") ==
|
||||
"+??:??\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
// see test_timezone.cpp for actual UTC offset calculation tests
|
||||
TEST_CASE("UTC offset", "[pattern_formatter]") {
|
||||
using namespace std::chrono_literals;
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
std::string result =
|
||||
log_to_str_with_time(now, "Some message", "%z", spdlog::pattern_time_type::local, "\n");
|
||||
|
||||
#ifndef SPDLOG_NO_TZ_OFFSET
|
||||
// Match format: +HH:MM or -HH:MM
|
||||
std::regex re(R"([+-]\d{2}:[0-5]\d\n)");
|
||||
REQUIRE(std::regex_match(result, re));
|
||||
#else
|
||||
REQUIRE(result == "+??:??\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST_CASE("color range test1", "[pattern_formatter]") {
|
||||
|
||||
@@ -57,15 +57,5 @@ TEST_CASE("test_empty", "[ringbuffer_sink]") {
|
||||
}
|
||||
|
||||
TEST_CASE("test_empty_size", "[ringbuffer_sink]") {
|
||||
const size_t sink_size = 0;
|
||||
auto sink = std::make_shared<spdlog::sinks::ringbuffer_sink_mt>(sink_size);
|
||||
spdlog::logger l("logger", sink);
|
||||
|
||||
for (size_t i = 0; i < sink_size + 1; ++i) {
|
||||
l.info("{}", i);
|
||||
}
|
||||
|
||||
sink->drain([&](std::string_view) {
|
||||
REQUIRE_FALSE(true); // should not be called since the sink size is 0
|
||||
});
|
||||
REQUIRE_THROWS_AS((void)spdlog::sinks::ringbuffer_sink_mt(0), spdlog::spdlog_ex);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
@@ -57,7 +58,8 @@ protected:
|
||||
// save the line without the eol
|
||||
auto eol_len = strlen(details::os::default_eol);
|
||||
if (lines_.size() < lines_to_save) {
|
||||
lines_.emplace_back(formatted.begin(), formatted.end() - eol_len);
|
||||
using diff_t = typename std::iterator_traits<decltype(formatted.end())>::difference_type;
|
||||
lines_.emplace_back(formatted.begin(), formatted.end() - static_cast<diff_t>(eol_len));
|
||||
}
|
||||
msg_counter_++;
|
||||
std::this_thread::sleep_for(delay_);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
TEST_CASE("stopwatch1", "[stopwatch]") {
|
||||
using std::chrono::milliseconds;
|
||||
using clock = std::chrono::steady_clock;
|
||||
milliseconds wait_ms(200);
|
||||
milliseconds wait_ms(500);
|
||||
milliseconds tolerance_ms(250);
|
||||
auto start = clock::now();
|
||||
spdlog::stopwatch sw;
|
||||
@@ -22,7 +22,7 @@ TEST_CASE("stopwatch2", "[stopwatch]") {
|
||||
using std::chrono::milliseconds;
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
clock::duration wait_duration(milliseconds(200));
|
||||
clock::duration wait_duration(milliseconds(500));
|
||||
clock::duration tolerance_duration(milliseconds(250));
|
||||
|
||||
auto test_sink = std::make_shared<test_sink_st>();
|
||||
|
||||
194
tests/test_timezone.cpp
Normal file
194
tests/test_timezone.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
#ifndef SPDLOG_NO_TZ_OFFSET
|
||||
|
||||
#include "includes.h"
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
// Helper to construct a simple std::tm from components
|
||||
std::tm make_tm(int year, int month, int day, int hour, int minute) {
|
||||
std::tm t;
|
||||
std::memset(&t, 0, sizeof(t));
|
||||
t.tm_year = year - 1900;
|
||||
t.tm_mon = month - 1;
|
||||
t.tm_mday = day;
|
||||
t.tm_hour = hour;
|
||||
t.tm_min = minute;
|
||||
t.tm_sec = 0;
|
||||
t.tm_isdst = -1;
|
||||
std::mktime(&t);
|
||||
return t;
|
||||
}
|
||||
|
||||
// Cross-platform RAII Helper to safely set/restore process timezone
|
||||
class ScopedTZ {
|
||||
std::string original_tz_;
|
||||
bool has_original_ = false;
|
||||
|
||||
public:
|
||||
explicit ScopedTZ(const std::string &tz_name) {
|
||||
// save current TZ
|
||||
#ifdef _WIN32
|
||||
char *buf = nullptr;
|
||||
size_t len = 0;
|
||||
if (_dupenv_s(&buf, &len, "TZ") == 0 && buf != nullptr) {
|
||||
original_tz_ = std::string(buf);
|
||||
has_original_ = true;
|
||||
free(buf);
|
||||
}
|
||||
#else
|
||||
const char *tz = std::getenv("TZ");
|
||||
if (tz) {
|
||||
original_tz_ = tz;
|
||||
has_original_ = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
// set new TZ
|
||||
#ifdef _WIN32
|
||||
_putenv_s("TZ", tz_name.c_str());
|
||||
_tzset();
|
||||
#else
|
||||
setenv("TZ", tz_name.c_str(), 1);
|
||||
tzset();
|
||||
#endif
|
||||
}
|
||||
|
||||
~ScopedTZ() {
|
||||
// restore original TZ
|
||||
#ifdef _WIN32
|
||||
if (has_original_) {
|
||||
_putenv_s("TZ", original_tz_.c_str());
|
||||
} else {
|
||||
_putenv_s("TZ", "");
|
||||
}
|
||||
_tzset();
|
||||
#else
|
||||
if (has_original_) {
|
||||
setenv("TZ", original_tz_.c_str(), 1);
|
||||
} else {
|
||||
unsetenv("TZ");
|
||||
}
|
||||
tzset();
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
using spdlog::details::os::utc_minutes_offset;
|
||||
|
||||
/*
|
||||
* POSIX 2024 defines three formats for the TZ environment variable,
|
||||
*
|
||||
* 1. Implementation defined format which always starts with a colon:
|
||||
* ":characters".
|
||||
* 2. A specifier which fully describes the timezone rule in format
|
||||
* "stdoffset[dst[offset][,start[/time],end[/time]]]". Note the
|
||||
* offset and start/end part could be omitted, in which case one hour
|
||||
* is implied, or it's considered implementation-defined when changing
|
||||
* to and from Daylight Saving Time occurs.
|
||||
* 3. Geographical or special timezone from an implementation-defined
|
||||
* timezone database.
|
||||
*
|
||||
* On POSIX-compilant systems, we prefer format 2, and explicitly specify the
|
||||
* DST rules to avoid implementation-defined behavior.
|
||||
*
|
||||
* See also IEEE 1003.1-2024 8.3 Other Environment Variables.
|
||||
*/
|
||||
#ifndef _WIN32
|
||||
/*
|
||||
* Standard time is UTC-5 ("EST"), DST time is UTC-4 ("EDT"). DST is active
|
||||
* from 2:00 on the 2nd Sunday in March, to 2:00 on 1st Sunday in November.
|
||||
*/
|
||||
#define EST5EDT "EST5EDT,M3.2.0,M11.1.0"
|
||||
/*
|
||||
* Standard time is UTC+2 ("IST"), DST time is UTC+3 ("IDT"). DST is active
|
||||
* from 2:00 on following day of the 4th Thursday in March, to 2:00 on the
|
||||
* last Sunday in October.
|
||||
*/
|
||||
#define IST_MINUS2_IDT "IST-2IDT,M3.4.4/26,M10.5.0"
|
||||
#else
|
||||
/*
|
||||
* However, Windows doesn't follow the POSIX rules and only accept a TZ
|
||||
* environment variable in format
|
||||
*
|
||||
* tzn [+|-]hh[:mm[:ss] ][dzn]
|
||||
*
|
||||
* thus we couldn't specify the DST rules. Luckily, Windows C runtime library
|
||||
* assumes the United State's rules for implementing the calculation of DST,
|
||||
* which is fine for our test cases.
|
||||
*
|
||||
* See also https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/tzset?view=msvc-170
|
||||
*/
|
||||
#define EST5EDT "EST5EDT"
|
||||
#define IST_MINUS2_IDT "IST-2IDT"
|
||||
#endif
|
||||
|
||||
TEST_CASE("UTC Offset - Western Hemisphere (USA - Standard Time)", "[timezone][west]") {
|
||||
// EST5EDT: Eastern Standard Time (UTC-5)
|
||||
ScopedTZ tz(EST5EDT);
|
||||
|
||||
// Jan 15th (Winter)
|
||||
auto tm = make_tm(2023, 1, 15, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||
}
|
||||
|
||||
TEST_CASE("UTC Offset - Eastern Hemisphere (Europe/Israel - Standard Time)", "[timezone][east]") {
|
||||
// IST-2IDT: Israel Standard Time (UTC+2)
|
||||
ScopedTZ tz(IST_MINUS2_IDT);
|
||||
|
||||
// Jan 15th (Winter)
|
||||
auto tm = make_tm(2023, 1, 15, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm) == 120);
|
||||
}
|
||||
|
||||
TEST_CASE("UTC Offset - Zero Offset (UTC/GMT)", "[timezone][utc]") {
|
||||
ScopedTZ tz("GMT0");
|
||||
|
||||
// Check Winter
|
||||
auto tm_winter = make_tm(2023, 1, 15, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm_winter) == 0);
|
||||
|
||||
// Check Summer (GMT never shifts, so this should also be 0)
|
||||
auto tm_summer = make_tm(2023, 7, 15, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm_summer) == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("UTC Offset - Non-Integer Hour Offsets (India)", "[timezone][partial]") {
|
||||
// IST-5:30: India Standard Time (UTC+5:30)
|
||||
ScopedTZ tz("IST-5:30");
|
||||
|
||||
auto tm = make_tm(2023, 1, 15, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm) == 330);
|
||||
}
|
||||
|
||||
TEST_CASE("UTC Offset - Edge Case: Negative Offset Crossing Midnight", "[timezone][edge]") {
|
||||
ScopedTZ tz(EST5EDT);
|
||||
// Late night Dec 31st, 2023
|
||||
auto tm = make_tm(2023, 12, 31, 23, 59);
|
||||
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||
}
|
||||
|
||||
TEST_CASE("UTC Offset - Edge Case: Leap Year", "[timezone][edge]") {
|
||||
ScopedTZ tz(EST5EDT);
|
||||
// Feb 29, 2024 (Leap Day) - Winter
|
||||
auto tm = make_tm(2024, 2, 29, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||
}
|
||||
|
||||
TEST_CASE("UTC Offset - Edge Case: Invalid Date (Pre-Epoch)", "[timezone][edge]") {
|
||||
#ifdef _WIN32
|
||||
// Windows mktime returns -1 for dates before 1970.
|
||||
// We expect the function to safely return 0 (fallback).
|
||||
auto tm = make_tm(1960, 1, 1, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm) == 0);
|
||||
#else
|
||||
// Unix mktime handles pre-1970 dates correctly.
|
||||
// We expect the actual historical offset (EST was UTC-5 in 1960).
|
||||
ScopedTZ tz(EST5EDT);
|
||||
auto tm = make_tm(1960, 1, 1, 12, 0);
|
||||
REQUIRE(utc_minutes_offset(tm) == -300);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // !SPDLOG_NO_TZ_OFFSET
|
||||
|
||||
Reference in New Issue
Block a user