diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index 28abc43e..d224066f 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -625,3 +625,19 @@ Mu::locale_workaround() return false; } + +bool +Mu::timezone_available(const std::string& tz) +{ + const auto old_tz = g_getenv("TZ"); + + g_setenv("TZ", tz.c_str(), TRUE); + + auto tzone = g_time_zone_new_local (); + bool have_tz = g_strcmp0(g_time_zone_get_identifier(tzone), tz.c_str()) == 0; + g_time_zone_unref (tzone); + + g_setenv("TZ", old_tz, TRUE); + + return have_tz; +} diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh index 8018a495..1842c7d2 100644 --- a/lib/utils/mu-utils.hh +++ b/lib/utils/mu-utils.hh @@ -155,6 +155,17 @@ std::string time_to_string(const std::string& frm, time_t t, bool utc = false) G bool locale_workaround(); +/** + * Is the given timezone available? For tests + * + * @param tz a timezone, such as Europe/Helsinki + * + * @return true or false + */ +bool timezone_available(const std::string& tz); + + + // https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member template struct deleter_from_fn { diff --git a/lib/utils/tests/test-utils.cc b/lib/utils/tests/test-utils.cc index 78cee854..e6f721ee 100644 --- a/lib/utils/tests/test-utils.cc +++ b/lib/utils/tests/test-utils.cc @@ -56,22 +56,15 @@ test_cases(const CaseVec& cases, ProcFunc proc) static void test_date_basic() { - // ensure we have the needed TZ or skip the test. const auto hki = "Europe/Helsinki"; - g_setenv("TZ", hki, TRUE); - { - auto tz = g_time_zone_new_local (); - bool have_hki = g_strcmp0(g_time_zone_get_identifier(tz), hki) == 0; - g_time_zone_unref (tz); - - if (!have_hki) { - g_test_skip("timezone Europe/Helsinki not available"); - return; - } + // ensure we have the needed TZ or skip the test. + if (!timezone_available(hki)) { + g_test_skip("timezone Europe/Helsinki not available"); + return; } - + g_setenv("TZ", hki, TRUE); constexpr std::array, 9> cases = {{ {"2015-09-18T09:10:23", true, 1442556623}, {"1972-12-14T09:10:23", true, 93165023}, diff --git a/mu/tests/test-mu-query.cc b/mu/tests/test-mu-query.cc index 781d3ce7..6eed6f7f 100644 --- a/mu/tests/test-mu-query.cc +++ b/mu/tests/test-mu-query.cc @@ -323,6 +323,12 @@ test_mu_query_wildcards(void) static void test_mu_query_dates_helsinki(void) { + const auto hki = "Europe/Helsinki"; + if (!timezone_available(hki)) { + g_test_skip("timezone not available"); + return; + } + int i; const char* old_tz; @@ -332,7 +338,7 @@ test_mu_query_dates_helsinki(void) {"date:200808110803..today", 7}, {"date:200808110801..now", 7}}; - old_tz = set_tz("Europe/Helsinki"); + old_tz = set_tz(hki); const auto xpath{make_database(MU_TESTMAILDIR)}; g_assert_false(xpath.empty()); @@ -348,6 +354,12 @@ test_mu_query_dates_helsinki(void) static void test_mu_query_dates_sydney(void) { + const auto syd = "Australia/Sydney"; + if (!timezone_available(syd)) { + g_test_skip("timezone not available"); + return; + } + int i; const char* old_tz; QResults queries[] = {{"date:20080731..20080804", 5}, @@ -355,8 +367,7 @@ test_mu_query_dates_sydney(void) {"date:200808110803..now", 7}, {"date:200808110803..today", 7}, {"date:200808110801..now", 7}}; - - old_tz = set_tz("Australia/Sydney"); + old_tz = set_tz(syd); const auto xpath{make_database(MU_TESTMAILDIR)}; g_assert_false(xpath.empty()); @@ -371,6 +382,12 @@ test_mu_query_dates_sydney(void) static void test_mu_query_dates_la(void) { + const auto la = "America/Los_Angeles"; + if (!timezone_available(la)) { + g_test_skip("timezone not available"); + return; + } + int i; const char* old_tz; @@ -381,8 +398,7 @@ test_mu_query_dates_la(void) {"date:200808110803..now", 6}, {"date:200808110803..today", 6}, {"date:200808110801..now", 6}}; - - old_tz = set_tz("America/Los_Angeles"); + old_tz = set_tz(la); const auto xpath = make_database(MU_TESTMAILDIR); g_assert_false(xpath.empty()); @@ -605,7 +621,7 @@ main(int argc, char* argv[]) setlocale(LC_ALL, ""); - g_test_init(&argc, &argv, NULL); + g_test_init(&argc, &argv, nullptr); DB_PATH1 = make_database(MU_TESTMAILDIR); g_assert_false(DB_PATH1.empty());