1 // sg_time.cxx -- data structures and routines for managing time related stuff.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22 
23 
24 #include <simgear_config.h>
25 #include <simgear/compiler.h>
26 
27 #include <errno.h>		// for errno
28 
29 #include <cstdio>
30 #include <cstdlib>
31 #include <ctime>
32 #include <cstring>
33 
34 #include <string>
35 
36 #ifdef HAVE_SYS_TIME_H
37 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
38 #endif
39 #ifdef HAVE_UNISTD_H
40 #  include <unistd.h>    // for gettimeofday()
41 #endif
42 
43 #include <math.h>        // for NAN
44 
45 #include <simgear/constants.h>
46 #include <simgear/debug/logstream.hxx>
47 #include <simgear/misc/sg_path.hxx>
48 
49 #include "sg_time.hxx"
50 #include "timezone.h"
51 #include "lowleveltime.h"
52 
53 #define DEGHR(x)        ((x)/15.)
54 #define RADHR(x)        DEGHR(x*SGD_RADIANS_TO_DEGREES)
55 
56 using std::string;
57 
58 static const double MJD0    = 2415020.0;
59 static const double J2000   = 2451545.0 - MJD0;
60 static const double SIDRATE = 0.9972695677;
61 
62 // tzContainer stores all the current Timezone control points/
63 std::unique_ptr<SGTimeZoneContainer> static_tzContainer;
64 
init(const SGGeod & location,const SGPath & root,time_t init_time)65 void SGTime::init( const SGGeod& location, const SGPath& root, time_t init_time )
66 {
67     gst_diff = -9999.0;
68 
69     if ( init_time ) {
70 	cur_time = init_time;
71     } else {
72 	cur_time = time(NULL);
73     }
74 
75     char* gmt = asctime(gmtime(&cur_time));
76     char* local = asctime(localtime(&cur_time));
77     gmt[strlen(gmt) - 1] = '\0';
78     local[strlen(local) - 1] = '\0';
79     SG_LOG( SG_EVENT, SG_DEBUG,
80                 "Current greenwich mean time = " << gmt);
81     SG_LOG( SG_EVENT, SG_DEBUG,
82              "Current local time          = " << local);
83 
84     if ( !root.isNull()) {
85         if (!static_tzContainer.get()) {
86             SGPath zone( root );
87             zone.append( "zone.tab" );
88             SG_LOG( SG_EVENT, SG_INFO, "Reading timezone info from: " << zone );
89             std::string zs = zone.utf8Str();
90             static_tzContainer.reset(new SGTimeZoneContainer( zs.c_str() ));
91         }
92 
93         SGTimeZone* nearestTz = static_tzContainer->getNearest(location);
94 
95         SGPath name( root );
96         name.append( nearestTz->getDescription() );
97         zonename = name.utf8Str();
98         SG_LOG( SG_EVENT, SG_DEBUG, "Using zonename = " << zonename );
99     } else {
100         zonename.erase();
101     }
102 }
103 
SGTime(const SGGeod & location,const SGPath & root,time_t init_time)104 SGTime::SGTime( const SGGeod& location, const SGPath& root,
105            time_t init_time )
106 {
107     init(location, root, init_time);
108 }
109 
SGTime(const SGPath & root)110 SGTime::SGTime( const SGPath& root ) {
111     init( SGGeod(), root, 0 );
112 }
113 
114 
SGTime()115 SGTime::SGTime() {
116     init( SGGeod(), SGPath(), 0 );
117 }
118 
119 
~SGTime()120 SGTime::~SGTime()
121 {
122 }
123 
124 
125 // given Julian Date and Longitude (decimal degrees West) compute
126 // Local Sidereal Time, in decimal hours.
127 //
128 // Provided courtesy of ecdowney@noao.edu (Elwood Downey)
sidereal_precise(double mjd,double lng)129 static double sidereal_precise( double mjd, double lng )
130 {
131     /* printf ("Current Lst on JD %13.5f at %8.4f degrees West: ",
132        mjd + MJD0, lng); */
133 
134     // convert to required internal units
135     lng *= SGD_DEGREES_TO_RADIANS;
136 
137     // compute LST and print
138     double gst = sgTimeCalcGST( mjd );
139     double lst = gst - RADHR( lng );
140     lst -= 24.0 * floor( lst / 24.0 );
141     // printf ("%7.4f\n", lstTmp);
142 
143     return lst;
144 }
145 
146 
147 // return a courser but cheaper estimate of sidereal time
sidereal_course(time_t cur_time,const struct tm * gmt,double lng)148 static double sidereal_course( time_t cur_time, const struct tm *gmt, double lng )
149 {
150     time_t start_gmt, now;
151     double diff, part, days, hours, lstTmp;
152 
153     now = cur_time;
154     start_gmt = sgTimeGetGMT(gmt->tm_year, 2, 21, 12, 0, 0);
155 
156     diff = (now - start_gmt) / (3600.0 * 24.0);
157 
158     part = fmod(diff, 1.0);
159     days = diff - part;
160     hours = gmt->tm_hour + gmt->tm_min/60.0 + gmt->tm_sec/3600.0;
161 
162     lstTmp = (days - lng)/15.0 + hours - 12;
163 
164     while ( lstTmp < 0.0 ) {
165 	lstTmp += 24.0;
166     }
167 
168     return lstTmp;
169 }
170 
171 // Update the time related variables
update(const SGGeod & location,time_t ct,long int warp)172 void SGTime::update( const SGGeod& location, time_t ct, long int warp )
173 {
174     double gst_precise, gst_course;
175 
176 
177     tm * gmt = &m_gmt;
178 
179 
180     // get current Unix calendar time (in seconds)
181     // warp += warp_delta;
182     if ( ct ) {
183 	cur_time = ct + warp;
184     } else {
185 	cur_time = time(NULL) + warp;
186     }
187 
188     // get GMT break down for current time
189 
190     memcpy( gmt, gmtime(&cur_time), sizeof(tm) );
191     // calculate modified Julian date starting with current
192     mjd = sgTimeCurrentMJD( ct, warp );
193 
194     // add in partial day
195     mjd += (gmt->tm_hour / 24.0) + (gmt->tm_min / (24.0 * 60.0)) +
196 	(gmt->tm_sec / (24.0 * 60.0 * 60.0));
197 
198     // convert "back" to Julian date + partial day (as a fraction of one)
199     jd = mjd + MJD0;
200 
201     // printf("  Current Longitude = %.3f\n", FG_Longitude * SGD_RADIANS_TO_DEGREES);
202 
203     // Calculate local side real time
204     if ( gst_diff < -100.0 ) {
205 	// first time through do the expensive calculation & cheap
206         // calculation to get the difference.
207 	SG_LOG( SG_EVENT, SG_DEBUG, "  First time, doing precise gst" );
208 	gst_precise = gst = sidereal_precise( mjd, 0.00 );
209 	gst_course = sidereal_course( cur_time, gmt, 0.00 );
210 
211 	gst_diff = gst_precise - gst_course;
212 
213 	lst = sidereal_course( cur_time, gmt,
214                                -location.getLongitudeDeg() ) + gst_diff;
215     } else {
216 	// course + difference should drift off very slowly
217 	gst = sidereal_course( cur_time, gmt, 0.00 ) + gst_diff;
218 	lst = sidereal_course( cur_time, gmt,
219                                -location.getLongitudeDeg()  ) + gst_diff;
220     }
221 }
222 
223 
224 // Given lon/lat, update timezone information and local_offset
updateLocal(const SGGeod & aLocation,const SGPath & root)225 void SGTime::updateLocal( const SGGeod& aLocation, const SGPath& root ) {
226   SGGeod location(aLocation);
227     if (!aLocation.isValid()) {
228         location = SGGeod();
229     }
230 
231     time_t currGMT;
232     time_t aircraftLocalTime;
233     SGTimeZone* nearestTz = static_tzContainer->getNearest(location);
234     SGPath zone( root );
235     zone.append ( nearestTz->getDescription() );
236     zonename = zone.utf8Str();
237 
238     //Avoid troubles when zone.tab hasn't got the right line endings
239     if (zonename[zonename.size()-1] == '\r')
240     {
241       zonename[zonename.size()-1]=0;
242       zone = SGPath(zonename);
243     }
244 
245     currGMT = sgTimeGetGMT( gmtime(&cur_time) );
246     std::string zs = zone.utf8Str();
247     aircraftLocalTime = sgTimeGetGMT( (fgLocaltime(&cur_time, zs.c_str())) );
248     local_offset = aircraftLocalTime - currGMT;
249     // cout << "Using " << local_offset << " as local time offset Timezone is "
250     //      << zonename << endl;
251 }
252 
253 
254 // given a date in months, mn, days, dy, years, yr, return the
255 // modified Julian date (number of days elapsed since 1900 jan 0.5),
256 // mjd.  Adapted from Xephem.
sgTimeCalcMJD(int mn,double dy,int yr)257 double sgTimeCalcMJD(int mn, double dy, int yr) {
258     double mjd;
259 
260     // internal book keeping data
261     static double last_mjd, last_dy;
262     static int last_mn, last_yr;
263 
264     int b, d, m, y;
265     long c;
266 
267     if (mn == last_mn && yr == last_yr && dy == last_dy) {
268 	mjd = last_mjd;
269     }
270 
271     m = mn;
272     y = (yr < 0) ? yr + 1 : yr;
273     if (mn < 3) {
274 	m += 12;
275 	y -= 1;
276     }
277 
278     if (yr < 1582 || (yr == 1582 && (mn < 10 || (mn == 10 && dy < 15)))) {
279 	b = 0;
280     } else {
281 	int a;
282 	a = y/100;
283 	b = 2 - a + a/4;
284     }
285 
286     if (y < 0) {
287 	c = (long)((365.25*y) - 0.75) - 694025L;
288     } else {
289 	c = (long)(365.25*y) - 694025L;
290     }
291 
292     d = (int)(30.6001*(m+1));
293 
294     mjd = b + c + d + dy - 0.5;
295 
296     last_mn = mn;
297     last_dy = dy;
298     last_yr = yr;
299     last_mjd = mjd;
300 
301     return mjd;
302 }
303 
304 
305 // return the current modified Julian date (number of days elapsed
306 // since 1900 jan 0.5), mjd.
sgTimeCurrentMJD(time_t ct,long int warp)307 double sgTimeCurrentMJD( time_t ct, long int warp ) {
308 
309     struct tm m_gmt;    // copy of system gmtime(&time_t) structure
310     struct tm *gmt = &m_gmt;
311 
312     // get current Unix calendar time (in seconds)
313     // warp += warp_delta;
314     time_t cur_time;
315     if ( ct ) {
316         cur_time = ct + warp;
317     } else {
318         cur_time = time(NULL) + warp;
319     }
320 
321     // get GMT break down for current time
322     memcpy( gmt, gmtime(&cur_time), sizeof(tm) );
323 
324     // calculate modified Julian date
325     // t->mjd = cal_mjd ((int)(t->gmt->tm_mon+1), (double)t->gmt->tm_mday,
326     //     (int)(t->gmt->tm_year + 1900));
327     double mjd = sgTimeCalcMJD( (int)(gmt->tm_mon+1), (double)gmt->tm_mday,
328                                 (int)(gmt->tm_year + 1900) );
329 
330     return mjd;
331 }
332 
333 
334 // given an mjd, calculate greenwich mean sidereal time, gst
sgTimeCalcGST(double mjd)335 double sgTimeCalcGST( double mjd ) {
336     double gst;
337 
338     double day = floor(mjd-0.5)+0.5;
339     double hr = (mjd-day)*24.0;
340     double T, x;
341 
342     T = ((int)(mjd - 0.5) + 0.5 - J2000)/36525.0;
343     x = 24110.54841 + (8640184.812866 + (0.093104 - 6.2e-6 * T) * T) * T;
344     x /= 3600.0;
345     gst = (1.0/SIDRATE)*hr + x;
346 
347     return gst;
348 }
349 
350 
351 /******************************************************************
352  * The following are some functions that were included as SGTime
353  * members, although they currently don't make use of any of the
354  * class's variables. Maybe this'll change in the future
355  *****************************************************************/
356 
357 // Return time_t for Sat Mar 21 12:00:00 GMT
358 //
359 
sgTimeGetGMT(int year,int month,int day,int hour,int min,int sec)360 time_t sgTimeGetGMT(int year, int month, int day, int hour, int min, int sec)
361 {
362     struct tm mt;
363 
364     mt.tm_mon = month;
365     mt.tm_mday = day;
366     mt.tm_year = year;
367     mt.tm_hour = hour;
368     mt.tm_min = min;
369     mt.tm_sec = sec;
370     mt.tm_isdst = -1; // let the system determine the proper time zone
371 
372 #if defined(SG_WINDOWS)
373     return _mkgmtime(&mt);
374 #elif defined( HAVE_TIMEGM )
375     return ( timegm(&mt) );
376 #else
377     #error Unix platforms should have timegm
378 #endif
379 }
380 
381 // format time
sgTimeFormatTime(const struct tm * p,char * buf)382 char* sgTimeFormatTime( const struct tm* p, char* buf )
383 {
384     sprintf( buf, "%d/%d/%2d %d:%02d:%02d",
385 	     p->tm_mon, p->tm_mday, p->tm_year,
386 	     p->tm_hour, p->tm_min, p->tm_sec);
387     return buf;
388 }
389