Fix crash when $TZ isn’t defined.

This commit is contained in:
John Ralls 2017-01-14 11:48:18 -08:00
parent c1e38d5a9f
commit 3925106e70
2 changed files with 27 additions and 21 deletions

View File

@ -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

View File

@ -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);