]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timezone.cxx
Patch from Melchior Franz:
[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 Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA  02111-1307, USA.
19  *
20  **************************************************************************/
21
22 /*************************************************************************
23  *
24  * Timezone is derived from geocoord, and stores the timezone centerpoint,
25  * as well as the countrycode and the timezone descriptor. The latter is 
26  * used in order to get the local time. 
27  *
28  ************************************************************************/
29
30 #include <errno.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include "timezone.h"
34
35 Timezone::Timezone(float la, float lo, char* cc, char* desc) :
36     GeoCoord(la, lo)
37
38     countryCode = strdup(cc);
39     descriptor = strdup(desc);
40 }
41
42 /* Build a timezone object from a textline in zone.tab */
43 Timezone::Timezone(const char *infoString) :
44     GeoCoord()
45 {
46     int i = 0;
47     while (infoString[i] != '\t')
48         i++;
49     char buffer[128];
50     char latlon[128];
51     strncpy(buffer, infoString, i);
52     buffer[i] = 0;
53     countryCode = strdup(buffer);
54     i ++;
55     int start = i;
56     while (infoString[i] != '\t') {
57         i++;
58     }
59     int size = i - start;
60     strncpy(latlon, (&infoString[start]), size);
61     latlon[size] = 0;
62     char sign;
63     sign = latlon[0];
64     strncpy(buffer, &latlon[1], 2);
65     buffer[2] = 0;
66     lat = atof(buffer);
67     strncpy(buffer, &latlon[3], 2);
68     buffer[2] = 0;
69     lat += (atof(buffer) / 60);
70     int nextPos;
71     if (strlen(latlon) > 12) {
72         nextPos = 7;
73         strncpy(buffer, &latlon[5], 2);
74         buffer[2] = 0;
75         lat += (atof(buffer) / 3600.0);
76     } else {
77         nextPos = 5;
78     }
79     if (sign == '-') {
80         lat = -lat;
81     }
82
83     sign = latlon[nextPos];
84     nextPos++;
85     strncpy(buffer, &latlon[nextPos], 3);
86     buffer[3] = 0;
87     lon = atof(buffer);
88     nextPos += 3;
89     strncpy(buffer, &latlon[nextPos], 2);
90     buffer[2] = 0;
91  
92     lon  += (atof(buffer) / 60);
93     if (strlen(latlon) > 12) {
94         nextPos += 2;
95         strncpy(buffer, &latlon[nextPos], 2); 
96         buffer[2] = 0;
97         lon +=  (atof (buffer) / 3600.00);
98     }
99     if (sign == '-') {
100         lon = -lon;
101     }
102     i ++;
103     start = i;
104     while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
105         i++;
106     }
107     size = i - start;
108     strncpy(buffer, (&infoString[start]), size);
109     buffer[size] = 0;
110     descriptor = strdup(buffer);
111 }
112
113 /* the copy constructor */
114 Timezone::Timezone(const Timezone& other)
115 {
116     lat = other.getLat();
117     lon = other.getLon();
118     countryCode = strdup(other.countryCode);
119     descriptor = strdup(other.descriptor);
120 }
121
122
123 /********* Member functions for TimezoneContainer class ********/
124
125 TimezoneContainer::TimezoneContainer(const char *filename)
126 {
127     char buffer[256];
128     FILE* infile = fopen(filename, "rb");
129     if (!(infile)) {
130         fprintf(stderr, "Unable to open file %s\n", filename);
131         exit(1);
132     } else { 
133         errno = 0;
134     
135         while (1) {
136             fgets(buffer, 256, infile);
137             if (feof(infile)) {
138                 break;
139             }
140 #ifdef _MSC_VER
141             if( buffer[0] == '#' )
142                continue;
143 #else
144             for (char *p = buffer; *p; p++) {
145                 if (*p == '#') {
146                     *p = 0;
147                     break;
148                 }    
149             }
150 #endif
151             if (buffer[0]) {
152                 data.push_back(new Timezone(buffer));
153             }
154         }
155         if ( errno ) {
156             perror( "TimezoneContainer()" );
157             errno = 0;
158         }
159     }
160 }
161
162 TimezoneContainer::~TimezoneContainer()
163 {
164 }