]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timezone.cxx
0b2e74c6c5ad0094a2fcaff6841b5c78f2a56fe8
[simgear.git] / simgear / timing / timezone.cxx
1 /* -*- Mode: C++ -*- *****************************************************
2  * timezone.cc
3  * Written by Durk Talsma. Started July 1999.
4  *
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.
9  *
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.
14  *
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.
18  *
19  **************************************************************************/
20
21 /*************************************************************************
22  *
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. 
26  *
27  ************************************************************************/
28
29 #include <errno.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 #include <simgear/structure/exception.hxx>
34
35 #include "timezone.h"
36
37 SGTimeZone::SGTimeZone(float la, float lo, char* cc, char* desc) :
38     SGGeoCoord(la, lo)
39
40     countryCode = cc;
41     descriptor = desc;
42 }
43
44 /* Build a timezone object from a textline in zone.tab */
45 SGTimeZone::SGTimeZone(const char *infoString) :
46     SGGeoCoord()
47 {
48     int i = 0;
49     while (infoString[i] != '\t')
50         i++;
51     char buffer[128];
52     char latlon[128];
53     strncpy(buffer, infoString, i);
54     buffer[i] = 0;
55     countryCode = buffer;
56     i ++;
57     int start = i;
58     while (infoString[i] != '\t') {
59         i++;
60     }
61     int size = i - start;
62     strncpy(latlon, (&infoString[start]), size);
63     latlon[size] = 0;
64     char sign;
65     sign = latlon[0];
66     strncpy(buffer, &latlon[1], 2);
67     buffer[2] = 0;
68     lat = atof(buffer);
69     strncpy(buffer, &latlon[3], 2);
70     buffer[2] = 0;
71     lat += (atof(buffer) / 60);
72     int nextPos;
73     if (strlen(latlon) > 12) {
74         nextPos = 7;
75         strncpy(buffer, &latlon[5], 2);
76         buffer[2] = 0;
77         lat += (atof(buffer) / 3600.0);
78     } else {
79         nextPos = 5;
80     }
81     if (sign == '-') {
82         lat = -lat;
83     }
84
85     sign = latlon[nextPos];
86     nextPos++;
87     strncpy(buffer, &latlon[nextPos], 3);
88     buffer[3] = 0;
89     lon = atof(buffer);
90     nextPos += 3;
91     strncpy(buffer, &latlon[nextPos], 2);
92     buffer[2] = 0;
93  
94     lon  += (atof(buffer) / 60);
95     if (strlen(latlon) > 12) {
96         nextPos += 2;
97         strncpy(buffer, &latlon[nextPos], 2); 
98         buffer[2] = 0;
99         lon +=  (atof (buffer) / 3600.00);
100     }
101     if (sign == '-') {
102         lon = -lon;
103     }
104     i ++;
105     start = i;
106     while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
107         i++;
108     }
109     size = i - start;
110     strncpy(buffer, (&infoString[start]), size);
111     buffer[size] = 0;
112     descriptor = buffer;
113 }
114
115 /* the copy constructor */
116 SGTimeZone::SGTimeZone(const SGTimeZone& other)
117 {
118     lat = other.getLat();
119     lon = other.getLon();
120     countryCode = other.countryCode;
121     descriptor = other.descriptor;
122 }
123
124
125 /********* Member functions for SGTimeZoneContainer class ********/
126
127 SGTimeZoneContainer::SGTimeZoneContainer(const char *filename)
128 {
129     char buffer[256];
130     FILE* infile = fopen(filename, "rb");
131     if (!(infile)) {
132         string e = "Unable to open time zone file '";
133         throw sg_exception(e + filename + '\'');
134
135     } else { 
136         errno = 0;
137     
138         while (1) {
139             fgets(buffer, 256, infile);
140             if (feof(infile)) {
141                 break;
142             }
143             for (char *p = buffer; *p; p++) {
144                 if (*p == '#') {
145                     *p = 0;
146                     break;
147                 }    
148             }
149             if (buffer[0]) {
150                 data.push_back(new SGTimeZone(buffer));
151             }
152         }
153         if ( errno ) {
154             perror( "SGTimeZoneContainer()" );
155             errno = 0;
156         }
157     }
158     fclose(infile);
159 }
160
161 SGTimeZoneContainer::~SGTimeZoneContainer()
162 {
163 }