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 ************************************************************************/
30 # include <simgear_config.h>
38 #include <simgear/structure/exception.hxx>
42 SGTimeZone::SGTimeZone(const SGGeod& geod, char* cc, char* desc) :
43 centerpoint(SGVec3d::fromGeod(geod))
49 /* Build a timezone object from a textline in zone.tab */
50 SGTimeZone::SGTimeZone(const char *infoString)
52 double lat = 0.0, lon = 0.0;
55 while (infoString[i] != '\t')
59 strncpy(buffer, infoString, i);
64 while (infoString[i] != '\t') {
68 strncpy(latlon, (&infoString[start]), size);
72 strncpy(buffer, &latlon[1], 2);
75 strncpy(buffer, &latlon[3], 2);
77 lat += (atof(buffer) / 60);
79 if (strlen(latlon) > 12) {
81 strncpy(buffer, &latlon[5], 2);
83 lat += (atof(buffer) / 3600.0);
91 sign = latlon[nextPos];
93 strncpy(buffer, &latlon[nextPos], 3);
97 strncpy(buffer, &latlon[nextPos], 2);
100 lon += (atof(buffer) / 60);
101 if (strlen(latlon) > 12) {
103 strncpy(buffer, &latlon[nextPos], 2);
105 lon += (atof (buffer) / 3600.00);
112 while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
116 strncpy(buffer, (&infoString[start]), size);
120 centerpoint = SGVec3d::fromGeod(SGGeod::fromDeg(lon, lat));
123 /* the copy constructor */
124 SGTimeZone::SGTimeZone(const SGTimeZone& other)
126 centerpoint = other.centerpoint;
127 countryCode = other.countryCode;
128 descriptor = other.descriptor;
132 /********* Member functions for SGTimeZoneContainer class ********/
134 SGTimeZoneContainer::SGTimeZoneContainer(const char *filename)
137 FILE* infile = fopen(filename, "rb");
139 string e = "Unable to open time zone file '";
140 throw sg_exception(e + filename + '\'');
146 (void) fgets(buffer, 256, infile);
150 for (char *p = buffer; *p; p++) {
157 zones.push_back(new SGTimeZone(buffer));
161 perror( "SGTimeZoneContainer()" );
168 SGTimeZoneContainer::~SGTimeZoneContainer()
170 TZVec::iterator it = zones.begin();
171 for (; it != zones.end(); ++it) {
176 SGTimeZone* SGTimeZoneContainer::getNearest(const SGGeod& ref) const
178 SGVec3d refCart(SGVec3d::fromGeod(ref));
179 SGTimeZone* match = NULL;
180 double minDist2 = HUGE_VAL;
182 TZVec::const_iterator it = zones.begin();
183 for (; it != zones.end(); ++it) {
184 double d2 = distSqr((*it)->cartCenterpoint(), refCart);