From 766d1849ffdcd2f0ddf50dd879494829ed03734b Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 29 Jul 2023 16:39:08 +0300 Subject: [PATCH] test-utils: add TempTz, RAII temporary timezone --- lib/utils/mu-test-utils.hh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/utils/mu-test-utils.hh b/lib/utils/mu-test-utils.hh index b61a4af1..aa3aee0c 100644 --- a/lib/utils/mu-test-utils.hh +++ b/lib/utils/mu-test-utils.hh @@ -136,6 +136,28 @@ private: const bool autodelete_; }; +/** + * Temporary (RAII) timezone + */ +struct TempTz { + TempTz(const char* tz) { + if (timezone_available(tz)) + old_tz_ = set_tz(tz); + else + old_tz_ = {}; + mu_debug("timezone '{}' {}available", tz, old_tz_ ? "": "not"); + } + ~TempTz() { + if (old_tz_) { + mu_debug("reset timezone to '{}'", old_tz_); + set_tz(old_tz_); + } + } + bool available() const { return !!old_tz_; } +private: + const char *old_tz_{}; +}; + } // namepace Mu