]> git.mxchange.org Git - simgear.git/blob - simgear/timing/timezone.cxx
Migrating timing support routines over to SimGear.
[simgear.git] / simgear / timing / timezone.cxx
1 /* -*- Mode: C++ -*- *****************************************************
2  * timezone.cc
3  * Written by Durk Talsma. Started July 1999.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  **************************************************************************/
20
21 /*************************************************************************
22  *
23  * Timezone 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 #include <stdio.h>
29 #include "timezone.h"
30
31
32 Timezone::Timezone(float la, float lo, char* cc, char* desc) :
33   GeoCoord(la, lo)
34
35   countryCode = strdup(cc);
36   descriptor = strdup(desc);
37 }
38
39 /* Build a timezone object from a textline in zone.tab */
40 Timezone::Timezone(const char *infoString) :
41   GeoCoord()
42 {
43   int i = 0;
44   while (infoString[i] != '\t')
45     i++;
46   char buffer[128];
47   char latlon[128];
48   strncpy(buffer, infoString, i);
49   buffer[i] = 0;
50   countryCode = strdup(buffer);
51   i ++;
52   int start = i;
53   while (infoString[i] != '\t')
54     i++;
55   int size = i - start;
56   strncpy(latlon, (&infoString[start]), size);
57   latlon[size] = 0;
58   char sign;
59   sign = latlon[0];
60   strncpy(buffer, &latlon[1], 2);
61   lat = atof(buffer);
62   strncpy(buffer, &latlon[3], 2);
63   lat += (atof(buffer) / 60);
64   int nextPos;
65   if (strlen(latlon) > 12)
66     {
67       nextPos = 7;
68       strncpy(buffer, &latlon[5], 2);
69       lat += (atof(buffer) / 3600.0);
70     }
71   else
72     nextPos = 5;
73   if (sign == '-')
74     lat = -lat;
75
76   sign = latlon[nextPos];
77   nextPos++;
78   strncpy(buffer, &latlon[nextPos], 3);
79   lon = atof(buffer);
80   nextPos += 3;
81   strncpy(buffer, &latlon[nextPos], 2);
82   buffer[2] = 0;
83  
84   lon  += (atof(buffer) / 60);
85   if (strlen(latlon) > 12)
86     {
87       nextPos += 2;
88       strncpy(buffer, &latlon[nextPos], 2); 
89       lon +=  (atof (buffer) / 3600.00);
90     }
91   if (sign == '-')
92     lon = -lon;
93   i ++;
94   start = i;
95   while (!((infoString[i] == '\t') || (infoString[i] == '\n')))
96     i++;
97   size = i - start;
98   strncpy(buffer, (&infoString[start]), size);
99   buffer[size] = 0;
100   descriptor = strdup(buffer);
101 }
102
103 /* the copy constructor */
104 Timezone::Timezone(const Timezone& other)
105 {
106   lat = other.getLat();
107   lon = other.getLon();
108   countryCode = strdup(other.countryCode);
109   descriptor = strdup(other.descriptor);
110 }
111
112
113 /********* Member functions for TimezoneContainer class ********/
114
115 TimezoneContainer::TimezoneContainer(const char *filename)
116 {
117   char buffer[256];
118   FILE* infile = fopen(filename, "r");
119   if (!(infile))
120     {
121       fprintf(stderr, "Unable to open file %s\n", filename);
122       exit(1);
123     }
124   else
125     { 
126       
127       while (1)
128         {
129           fgets(buffer, 256, infile);
130           if (feof(infile))
131             break;
132           for (int i = 0; i < 256; i++)
133             {
134               if (buffer[i] == '#')
135                 buffer[i] = 0;
136             }
137           if (buffer[0])
138             {
139               data.push_back(new Timezone(buffer));
140             }
141         }
142     }
143 }
144
145 TimezoneContainer::~TimezoneContainer()
146 {
147 }