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