Skip to content

Commit 9c58257

Browse files
gabimetoh-ableton
andauthored
Fix zformatter on Apple and POSIX.1-2024 conforming platform (#3366)
* Add test case for #3351 (wrong GMT offset in SunOS/Solaris fallback) * Fix #3352 (Missing test for Apple / POSIX.1-2024 chooses buggy workaround) Apple platforms have had the tm_gmtoff-field at least since Mac OS X 10.0, as are POSIX.1-2024 conforming systems, which are also required to support it. This has the unfortunate effect to use the SunOS/Solaris fallback, which doesn't compute the correct value if the passed value of tm isn't the current system time, i.e. localtime(::time()) (#3351). * Fixed GMT offset test --------- Co-authored-by: toh <[email protected]>
1 parent faa0a7a commit 9c58257

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

include/spdlog/details/os-inl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) {
267267

268268
#if defined(sun) || defined(__sun) || defined(_AIX) || \
269269
(defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \
270-
(!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE))
270+
(!defined(__APPLE__) && !defined(_BSD_SOURCE) && !defined(_GNU_SOURCE) && \
271+
(!defined(_POSIX_VERSION) || (_POSIX_VERSION < 202405L)))
271272
// 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris
272273
struct helper {
273274
static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(),

tests/test_pattern_formatter.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "includes.h"
22
#include "test_sink.h"
33

4+
#include <chrono>
5+
46
using spdlog::memory_buf_t;
57
using spdlog::details::to_string_view;
68

@@ -19,6 +21,21 @@ static std::string log_to_str(const std::string &msg, const Args &...args) {
1921
return oss.str();
2022
}
2123

24+
// log to str and return it with time
25+
template <typename... Args>
26+
static std::string log_to_str_with_time(spdlog::log_clock::time_point log_time, const std::string &msg, const Args &...args) {
27+
std::ostringstream oss;
28+
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
29+
spdlog::logger oss_logger("pattern_tester", oss_sink);
30+
oss_logger.set_level(spdlog::level::info);
31+
32+
oss_logger.set_formatter(
33+
std::unique_ptr<spdlog::formatter>(new spdlog::pattern_formatter(args...)));
34+
35+
oss_logger.log(log_time, {}, spdlog::level::info, msg);
36+
return oss.str();
37+
}
38+
2239
TEST_CASE("custom eol", "[pattern_formatter]") {
2340
std::string msg = "Hello custom eol test";
2441
std::string eol = ";)";
@@ -58,6 +75,15 @@ TEST_CASE("date MM/DD/YY ", "[pattern_formatter]") {
5875
oss.str());
5976
}
6077

78+
TEST_CASE("GMT offset ", "[pattern_formatter]") {
79+
using namespace std::chrono_literals;
80+
const auto now = std::chrono::system_clock::now();
81+
const auto yesterday = now - 24h;
82+
83+
REQUIRE(log_to_str_with_time(yesterday, "Some message", "%z", spdlog::pattern_time_type::utc, "\n") ==
84+
"+00:00\n");
85+
}
86+
6187
TEST_CASE("color range test1", "[pattern_formatter]") {
6288
auto formatter = std::make_shared<spdlog::pattern_formatter>(
6389
"%^%v%$", spdlog::pattern_time_type::local, "\n");

0 commit comments

Comments
 (0)