diff --git a/src/libqof/qof/gnc-timezone.cpp b/src/libqof/qof/gnc-timezone.cpp index 3a8e14bed8..4a8ecff469 100644 --- a/src/libqof/qof/gnc-timezone.cpp +++ b/src/libqof/qof/gnc-timezone.cpp @@ -666,7 +666,8 @@ TimeZoneProvider::parse_file(const std::string& tzname) zone_vector.push_back(zone_from_rule(max_year, last_rule)); } -TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {} +bool +TimeZoneProvider::construct(const std::string& tzname) { try { @@ -679,29 +680,33 @@ TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {} TZ_Ptr zone(new PTZ(tzname)); zone_vector.push_back(std::make_pair(max_year, zone)); } - catch(const std::exception& err) + catch(std::exception& err) { - try - { - parse_file(getenv("TZ")); - } - catch(const std::exception& err) - { - - std::cerr << "Unable to use either provided tzname or TZ environment variable. Resorting to /etc/localtime.\n"; - try - { - parse_file("/etc/localtime"); - } - catch(const std::invalid_argument& env) - { - std::cerr << "/etc/localtime invalid, resorting to GMT."; - TZ_Ptr zone(new PTZ("UTC0")); - zone_vector.push_back(std::make_pair(max_year, zone)); - } - } + return false; } } + return true; +} + +TimeZoneProvider::TimeZoneProvider(const std::string& tzname) : zone_vector {} +{ + if(construct(tzname)) + return; + std::cerr << tzname << " invalid, trying TZ environment variable.\n"; + const char* tz_env = getenv("TZ"); + if(tz_env && construct(tz_env)) + return; + std::cerr << "No valid $TZ, resorting to /etc/localtime.\n"; + try + { + parse_file("/etc/localtime"); + } + catch(const std::invalid_argument& env) + { + std::cerr << "/etc/localtime invalid, resorting to GMT."; + TZ_Ptr zone(new PTZ("UTC0")); + zone_vector.push_back(std::make_pair(max_year, zone)); + } } #endif diff --git a/src/libqof/qof/gnc-timezone.hpp b/src/libqof/qof/gnc-timezone.hpp index 546bbf32d0..9bb0eadbef 100644 --- a/src/libqof/qof/gnc-timezone.hpp +++ b/src/libqof/qof/gnc-timezone.hpp @@ -59,6 +59,7 @@ public: static const unsigned int max_year; //9999 private: void parse_file(const std::string& tzname); + bool construct(const std::string& tzname); TZ_Vector zone_vector; #if PLATFORM(WINDOWS) void load_windows_dynamic_tz(HKEY, time_zone_names);