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 ************************************************************************/
34 #include <simgear/structure/exception.hxx>
38 SGTimeZone::SGTimeZone(const SGGeod& geod, char* cc, char* desc) :
39 centerpoint(SGVec3d::fromGeod(geod))
45 /* Build a timezone object from a textline in zone.tab */
46 SGTimeZone::SGTimeZone(const char *infoString)
48 double lat = 0.0, lon = 0.0;
51 while (infoString[i] != '\t')
55 strncpy(buffer, infoString, i);
60 while (infoString[i] != '\t') {
64 strncpy(latlon, (&infoString[start]), size);
68 strncpy(buffer, &latlon[1], 2);
71 strncpy(buffer, &latlon[3], 2);
73 lat += (atof(buffer) / 60);
75 if (strlen(latlon) > 12) {
77 strncpy(buffer, &latlon[5], 2);
79 lat += (atof(buffer) / 3600.0);
87 sign = latlon[nextPos];
89 strncpy(buffer, &latlon[nextPos], 3);
93 strncpy(buffer, &latlon[nextPos], 2);
96 lon += (atof(buffer) / 60);
97 if (strlen(latlon) > 12) {
99 strncpy(buffer, &latlon[nextPos], 2);
101 lon += (atof (buffer) / 3600.00);
108 while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
112 strncpy(buffer, (&infoString[start]), size);
116 centerpoint = SGVec3d::fromGeod(SGGeod::fromDeg(lon, lat));
119 /* the copy constructor */
120 SGTimeZone::SGTimeZone(const SGTimeZone& other)
122 centerpoint = other.centerpoint;
123 countryCode = other.countryCode;
124 descriptor = other.descriptor;
128 /********* Member functions for SGTimeZoneContainer class ********/
130 SGTimeZoneContainer::SGTimeZoneContainer(const char *filename)
133 FILE* infile = fopen(filename, "rb");
135 string e = "Unable to open time zone file '";
136 throw sg_exception(e + filename + '\'');
142 fgets(buffer, 256, infile);
146 for (char *p = buffer; *p; p++) {
153 zones.push_back(new SGTimeZone(buffer));
157 perror( "SGTimeZoneContainer()" );
164 SGTimeZoneContainer::~SGTimeZoneContainer()
166 TZVec::iterator it = zones.begin();
167 for (; it != zones.end(); ++it) {
172 SGTimeZone* SGTimeZoneContainer::getNearest(const SGGeod& ref) const
174 SGVec3d refCart(SGVec3d::fromGeod(ref));
175 SGTimeZone* match = NULL;
176 double minDist2 = HUGE_VAL;
178 TZVec::const_iterator it = zones.begin();
179 for (; it != zones.end(); ++it) {
180 double d2 = distSqr((*it)->cartCenterpoint(), refCart);