1 /* -*- Mode: C++ -*- *****************************************************
3 * Written by Durk Talsma. Started July 1999.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
21 /*************************************************************************
23 * SGTimeZone is derived from geocoord, and stores the timezone centerpoint,
24 * as well as the countrycode and the timezone descriptor. The latter is
25 * used in order to get the local time.
27 ************************************************************************/
33 #include <simgear/structure/exception.hxx>
37 SGTimeZone::SGTimeZone(const SGGeod& geod, char* cc, char* desc) :
38 centerpoint(SGVec3d::fromGeod(geod))
44 /* Build a timezone object from a textline in zone.tab */
45 SGTimeZone::SGTimeZone(const char *infoString)
47 double lat = 0.0, lon = 0.0;
50 while (infoString[i] != '\t')
54 strncpy(buffer, infoString, i);
59 while (infoString[i] != '\t') {
63 strncpy(latlon, (&infoString[start]), size);
67 strncpy(buffer, &latlon[1], 2);
70 strncpy(buffer, &latlon[3], 2);
72 lat += (atof(buffer) / 60);
74 if (strlen(latlon) > 12) {
76 strncpy(buffer, &latlon[5], 2);
78 lat += (atof(buffer) / 3600.0);
86 sign = latlon[nextPos];
88 strncpy(buffer, &latlon[nextPos], 3);
92 strncpy(buffer, &latlon[nextPos], 2);
95 lon += (atof(buffer) / 60);
96 if (strlen(latlon) > 12) {
98 strncpy(buffer, &latlon[nextPos], 2);
100 lon += (atof (buffer) / 3600.00);
107 while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
111 strncpy(buffer, (&infoString[start]), size);
115 centerpoint = SGVec3d::fromGeod(SGGeod::fromDeg(lon, lat));
118 /* the copy constructor */
119 SGTimeZone::SGTimeZone(const SGTimeZone& other)
121 centerpoint = other.centerpoint;
122 countryCode = other.countryCode;
123 descriptor = other.descriptor;
127 /********* Member functions for SGTimeZoneContainer class ********/
129 SGTimeZoneContainer::SGTimeZoneContainer(const char *filename)
132 FILE* infile = fopen(filename, "rb");
134 string e = "Unable to open time zone file '";
135 throw sg_exception(e + filename + '\'');
141 fgets(buffer, 256, infile);
145 for (char *p = buffer; *p; p++) {
152 zones.push_back(new SGTimeZone(buffer));
156 perror( "SGTimeZoneContainer()" );
163 SGTimeZoneContainer::~SGTimeZoneContainer()
165 TZVec::iterator it = zones.begin();
166 for (; it != zones.end(); ++it) {
171 SGTimeZone* SGTimeZoneContainer::getNearest(const SGGeod& ref) const
173 SGVec3d refCart(SGVec3d::fromGeod(ref));
174 SGTimeZone* match = NULL;
175 double minDist2 = HUGE_VAL;
177 TZVec::const_iterator it = zones.begin();
178 for (; it != zones.end(); ++it) {
179 double d2 = distSqr((*it)->cartCenterpoint(), refCart);