diff --git a/simgear/timing/timezone.cxx b/simgear/timing/timezone.cxx old mode 100755 new mode 100644 index 8b3bfc7c..4a07864d --- a/simgear/timing/timezone.cxx +++ b/simgear/timing/timezone.cxx @@ -35,10 +35,11 @@ #include #include +#include #include #include +#include -#include "zonedetect.h" #include "timezone.h" SGTimeZone::SGTimeZone(const SGGeod& geod, char* cc, char* desc) : @@ -134,10 +135,24 @@ SGTimeZone::SGTimeZone(const SGTimeZone& other) /********* Member functions for SGTimeZoneContainer class ********/ -SGTimeZoneContainer::SGTimeZoneContainer(const char *filename) : - tzdb_file(filename) +SGTimeZoneContainer::SGTimeZoneContainer(const char *filename) { - if (tzdb_file.find("zone.tab") != std::string::npos) + std::string tzdb_file = filename; + if (tzdb_file.find("zone.tab") == std::string::npos) + { + SGPath path(filename); + sg_ifstream tzdb_file(path, std::ios::in); + if ( !tzdb_file.is_open() ) { + throw sg_io_exception("cannot open timezone file", filename); + } + + tzdb_buffer = tzdb_file.read_all(); + cd = ZDOpenDatabaseFromMemory((void*)tzdb_buffer.c_str(), tzdb_buffer.size()); + if (!cd) { + throw sg_io_exception("timezone database read error"); + } + } + else // zone.tab is in filename { char buffer[256]; #if defined(SG_WINDOWS) @@ -188,6 +203,10 @@ SGTimeZoneContainer::~SGTimeZoneContainer() delete *it; } } + else if (cd) + { + ZDCloseDatabase(cd); + } } SGTimeZone* SGTimeZoneContainer::getNearest(const SGGeod& ref) const @@ -208,43 +227,38 @@ SGTimeZone* SGTimeZoneContainer::getNearest(const SGGeod& ref) const } } } - else if (!tzdb_file.empty()) + else if (cd) // timezone16.bin { - ZoneDetect *const cd = ZDOpenDatabase(tzdb_file.c_str()); - if (cd) - { - char *CountryAlpha2 = nullptr; - char *TimezoneIdPrefix = nullptr; - char *TimezoneId = nullptr; + char *CountryAlpha2 = nullptr; + char *TimezoneIdPrefix = nullptr; + char *TimezoneId = nullptr; - float safezone = 0; - float lat = ref.getLatitudeDeg(); - float lon = ref.getLongitudeDeg(); - ZoneDetectResult *results = ZDLookup(cd, lat, lon, &safezone); - if (results && results[0].data) + float safezone = 0; + float lat = ref.getLatitudeDeg(); + float lon = ref.getLongitudeDeg(); + ZoneDetectResult *results = ZDLookup(cd, lat, lon, &safezone); + if (results && results[0].data) + { + for(unsigned i=0; i #include +#include "zonedetect.h" + /** * SGTimeZone stores the timezone centerpoint, * as well as the countrycode and the timezone descriptor. The latter is @@ -97,7 +99,8 @@ public: SGTimeZone* getNearest(const SGGeod& ref) const; private: - std::string tzdb_file; + ZoneDetect *cd = nullptr; + std::string tzdb_buffer; // zone.tab related bool is_zone_tab = false;