]> git.mxchange.org Git - flightgear.git/blob - Simulator/Time/fg_time.cxx
Initial revision.
[flightgear.git] / Simulator / Time / fg_time.cxx
1 // fg_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  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <Include/compiler.h>
29
30 #ifdef FG_HAVE_STD_INCLUDES
31 #  include <cmath>
32 #  include <cstdio>
33 #  include <cstdlib>
34 #  include <ctime>
35 #else
36 #  include <math.h>
37 #  include <stdio.h>
38 #  include <stdlib.h>
39 #  include <time.h>
40 #endif
41
42 #ifdef HAVE_SYS_TIMEB_H
43 #  include <sys/timeb.h> // for ftime() and struct timeb
44 #endif
45 #ifdef HAVE_UNISTD_H
46 #  include <unistd.h>    // for gettimeofday()
47 #endif
48 #ifdef HAVE_SYS_TIME_H
49 #  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
50 #endif
51
52 #include <Debug/logstream.hxx>
53 #include <Astro/sky.hxx>
54 #include <Astro/solarsystem.hxx>
55 #include <FDM/flight.hxx>
56 #include <Include/fg_constants.h>
57 #include <Main/options.hxx>
58 #include <Time/light.hxx>
59
60 #include "fg_time.hxx"
61
62
63 #define DEGHR(x)        ((x)/15.)
64 #define RADHR(x)        DEGHR(x*RAD_TO_DEG)
65
66 // #define MK_TIME_IS_GMT 0         // default value
67 // #define TIME_ZONE_OFFSET_WORK 0  // default value
68
69
70 fgTIME cur_time_params;
71
72
73 // Force an update of the sky and lighting parameters
74 static void local_update_sky_and_lighting_params( void ) {
75     // fgSunInit();
76     SolarSystem::theSolarSystem->rebuild();
77     cur_light_params.Update();
78     fgSkyColorsInit();
79 }
80
81
82 // Initialize the time dependent variables
83 void fgTimeInit(fgTIME *t) {
84     FG_LOG( FG_EVENT, FG_INFO, "Initializing Time" );
85
86     t->gst_diff = -9999.0;
87
88     FG_LOG( FG_EVENT, FG_DEBUG, 
89             "time offset = " << current_options.get_time_offset() );
90
91     t->warp = current_options.get_time_offset();
92     t->warp_delta = 0;
93
94     t->pause = current_options.get_pause();
95 }
96
97
98 // given a date in months, mn, days, dy, years, yr, return the
99 // modified Julian date (number of days elapsed since 1900 jan 0.5),
100 // mjd.  Adapted from Xephem.
101
102 double cal_mjd (int mn, double dy, int yr) {
103     static double last_mjd, last_dy;
104     double mjd;
105     static int last_mn, last_yr;
106     int b, d, m, y;
107     long c;
108
109     if (mn == last_mn && yr == last_yr && dy == last_dy) {
110         mjd = last_mjd;
111         return(mjd);
112     }
113
114     m = mn;
115     y = (yr < 0) ? yr + 1 : yr;
116     if (mn < 3) {
117         m += 12;
118         y -= 1;
119     }
120
121     if (yr < 1582 || (yr == 1582 && (mn < 10 || (mn == 10 && dy < 15)))) {
122         b = 0;
123     } else {
124         int a;
125         a = y/100;
126         b = 2 - a + a/4;
127     }
128
129     if (y < 0) {
130         c = (long)((365.25*y) - 0.75) - 694025L;
131     } else {
132         c = (long)(365.25*y) - 694025L;
133     }
134     
135     d = (int)(30.6001*(m+1));
136
137     mjd = b + c + d + dy - 0.5;
138
139     last_mn = mn;
140     last_dy = dy;
141     last_yr = yr;
142     last_mjd = mjd;
143
144     return(mjd);
145 }
146
147
148 // given an mjd, return greenwich mean sidereal time, gst
149
150 double utc_gst (double mjd) {
151     double gst;
152     double day = floor(mjd-0.5)+0.5;
153     double hr = (mjd-day)*24.0;
154     double T, x;
155
156     T = ((int)(mjd - 0.5) + 0.5 - J2000)/36525.0;
157     x = 24110.54841 + (8640184.812866 + (0.093104 - 6.2e-6 * T) * T) * T;
158     x /= 3600.0;
159     gst = (1.0/SIDRATE)*hr + x;
160
161     FG_LOG( FG_EVENT, FG_DEBUG, "  gst => " << gst );
162
163     return(gst);
164 }
165
166
167 // given Julian Date and Longitude (decimal degrees West) compute and
168 // return Local Sidereal Time, in decimal hours.
169 //
170 // Provided courtesy of ecdowney@noao.edu (Elwood Downey) 
171 //
172
173 double sidereal_precise (double mjd, double lng) {
174     double gst;
175     double lst;
176
177     /* printf ("Current Lst on JD %13.5f at %8.4f degrees West: ", 
178        mjd + MJD0, lng); */
179
180     // convert to required internal units
181     lng *= DEG_TO_RAD;
182
183     // compute LST and print
184     gst = utc_gst (mjd);
185     lst = gst - RADHR (lng);
186     lst -= 24.0*floor(lst/24.0);
187     // printf ("%7.4f\n", lst);
188
189     // that's all
190     return (lst);
191 }
192
193
194 // Fix up timezone if using ftime()
195 long int fix_up_timezone( long int timezone_orig ) {
196 #if !defined( HAVE_GETTIMEOFDAY ) && defined( HAVE_FTIME )
197     // ftime() needs a little extra help finding the current timezone
198     struct timeb current;
199     ftime(&current);
200     return( current.timezone * 60 );
201 #else
202     return( timezone_orig );
203 #endif
204 }
205
206
207 // Return time_t for Sat Mar 21 12:00:00 GMT
208 //
209 // I believe the mktime() has a SYSV vs. BSD behavior difference.
210 //
211 // The BSD style mktime() is nice because it returns its result
212 // assuming you have specified the input time in GMT
213 //
214 // The SYSV style mktime() is a pain because it returns its result
215 // assuming you have specified the input time in your local timezone.
216 // Therefore you have to go to extra trouble to convert back to GMT.
217 //
218 // If you are having problems with incorrectly positioned astronomical
219 // bodies, this is a really good place to start looking.
220
221 time_t get_start_gmt(int year) {
222     struct tm mt;
223
224     // For now we assume that if daylight is not defined in
225     // /usr/include/time.h that we have a machine with a BSD behaving
226     // mktime()
227 #   if !defined(HAVE_DAYLIGHT)
228 #       define MK_TIME_IS_GMT 1
229 #   endif
230
231     // timezone seems to work as a proper offset for Linux & Solaris
232 #   if defined( __linux__ ) || defined( __sun__ ) 
233 #       define TIMEZONE_OFFSET_WORKS 1
234 #   endif
235
236     mt.tm_mon = 2;
237     mt.tm_mday = 21;
238     mt.tm_year = year;
239     mt.tm_hour = 12;
240     mt.tm_min = 0;
241     mt.tm_sec = 0;
242     mt.tm_isdst = -1; // let the system determine the proper time zone
243
244 #   if defined( MK_TIME_IS_GMT )
245     return ( mktime(&mt) );
246 #   else // ! defined ( MK_TIME_IS_GMT )
247
248     long int start = mktime(&mt);
249
250     FG_LOG( FG_EVENT, FG_DEBUG, "start1 = " << start );
251     // the ctime() call can screw up time progression on some versions
252     // of Linux
253     // fgPrintf( FG_EVENT, FG_DEBUG, "start2 = %s", ctime(&start));
254     FG_LOG( FG_EVENT, FG_DEBUG, "(tm_isdst = " << mt.tm_isdst << ")" );
255
256     timezone = fix_up_timezone( timezone );
257
258 #   if defined( TIMEZONE_OFFSET_WORKS )
259     FG_LOG( FG_EVENT, FG_DEBUG, 
260             "start = " << start << ", timezone = " << timezone );
261     return( start - timezone );
262 #   else // ! defined( TIMEZONE_OFFSET_WORKS )
263
264     daylight = mt.tm_isdst;
265     if ( daylight > 0 ) {
266         daylight = 1;
267     } else if ( daylight < 0 ) {
268         FG_LOG( FG_EVENT, FG_WARN, 
269                 "OOOPS, problem in fg_time.cxx, no daylight savings info." );
270     }
271
272     long int offset = -(timezone / 3600 - daylight);
273
274     FG_LOG( FG_EVENT, FG_DEBUG, "  Raw time zone offset = " << timezone );
275     FG_LOG( FG_EVENT, FG_DEBUG, "  Daylight Savings = " << daylight );
276     FG_LOG( FG_EVENT, FG_DEBUG, "  Local hours from GMT = " << offset );
277     
278     long int start_gmt = start - timezone + (daylight * 3600);
279     
280     FG_LOG( FG_EVENT, FG_DEBUG, "  March 21 noon (CST) = " << start );
281
282     return ( start_gmt );
283 #   endif // ! defined( TIMEZONE_OFFSET_WORKS )
284 #   endif // ! defined ( MK_TIME_IS_GMT )
285 }
286
287 static char*
288 format_time( const struct tm* p, char* buf )
289 {
290     sprintf( buf, "%d/%d/%2d %d:%02d:%02d", 
291              p->tm_mon, p->tm_mday, p->tm_year,
292              p->tm_hour, p->tm_min, p->tm_sec);
293     return buf;
294 }
295
296 // return a courser but cheaper estimate of sidereal time
297 double sidereal_course(fgTIME *t, double lng) {
298     struct tm *gmt;
299     time_t start_gmt, now;
300     double diff, part, days, hours, lst;
301     char tbuf[64];
302
303     gmt = t->gmt;
304     now = t->cur_time;
305     start_gmt = get_start_gmt(gmt->tm_year);
306
307     FG_LOG( FG_EVENT, FG_DEBUG, "  COURSE: GMT = " << format_time(gmt, tbuf) );
308     FG_LOG( FG_EVENT, FG_DEBUG, "  March 21 noon (GMT) = " << start_gmt );
309
310     diff = (now - start_gmt) / (3600.0 * 24.0);
311     
312     FG_LOG( FG_EVENT, FG_DEBUG, 
313             "  Time since 3/21/" << gmt->tm_year << " GMT = " << diff );
314
315     part = fmod(diff, 1.0);
316     days = diff - part;
317     hours = gmt->tm_hour + gmt->tm_min/60.0 + gmt->tm_sec/3600.0;
318
319     lst = (days - lng)/15.0 + hours - 12;
320
321     while ( lst < 0.0 ) {
322         lst += 24.0;
323     }
324
325     FG_LOG( FG_EVENT, FG_DEBUG,
326             "  days = " << days << "  hours = " << hours << "  lon = " 
327             << lng << "  lst = " << lst );
328
329     return(lst);
330 }
331
332
333 // Update time variables such as gmt, julian date, and sidereal time
334 void fgTimeUpdate(FGInterface *f, fgTIME *t) {
335     double gst_precise, gst_course;
336
337     FG_LOG( FG_EVENT, FG_DEBUG, "Updating time" );
338
339     // get current Unix calendar time (in seconds)
340     t->warp += t->warp_delta;
341     t->cur_time = time(NULL) + t->warp;
342     FG_LOG( FG_EVENT, FG_DEBUG, 
343             "  Current Unix calendar time = " << t->cur_time 
344             << "  warp = " << t->warp << "  delta = " << t->warp_delta );
345
346     if ( t->warp_delta ) {
347         // time is changing so force an update
348         local_update_sky_and_lighting_params();
349     }
350
351     // get GMT break down for current time
352     t->gmt = gmtime(&t->cur_time);
353     FG_LOG( FG_EVENT, FG_DEBUG, 
354             "  Current GMT = " << t->gmt->tm_mon+1 << "/" 
355             << t->gmt->tm_mday << "/" << t->gmt->tm_year << " "
356             << t->gmt->tm_hour << ":" << t->gmt->tm_min << ":" 
357             << t->gmt->tm_sec );
358
359     // calculate modified Julian date
360     t->mjd = cal_mjd ((int)(t->gmt->tm_mon+1), (double)t->gmt->tm_mday, 
361              (int)(t->gmt->tm_year + 1900));
362
363     // add in partial day
364     t->mjd += (t->gmt->tm_hour / 24.0) + (t->gmt->tm_min / (24.0 * 60.0)) +
365            (t->gmt->tm_sec / (24.0 * 60.0 * 60.0));
366
367     // convert "back" to Julian date + partial day (as a fraction of one)
368     t->jd = t->mjd + MJD0;
369     FG_LOG( FG_EVENT, FG_DEBUG, "  Current Julian Date = " << t->jd );
370
371     // printf("  Current Longitude = %.3f\n", FG_Longitude * RAD_TO_DEG);
372
373     // Calculate local side real time
374     if ( t->gst_diff < -100.0 ) {
375         // first time through do the expensive calculation & cheap
376         // calculation to get the difference.
377       FG_LOG( FG_EVENT, FG_INFO, "  First time, doing precise gst" );
378       t->gst = gst_precise = sidereal_precise(t->mjd, 0.00);
379       gst_course = sidereal_course(t, 0.00);
380       t->gst_diff = gst_precise - gst_course;
381
382       t->lst =
383           sidereal_course(t, -(f->get_Longitude() * RAD_TO_DEG)) + t->gst_diff;
384     } else {
385         // course + difference should drift off very slowly
386         t->gst = sidereal_course(t, 0.00) + t->gst_diff;
387         t->lst = sidereal_course(t, -(f->get_Longitude() * RAD_TO_DEG)) + 
388             t->gst_diff;
389     }
390     FG_LOG( FG_EVENT, FG_DEBUG,
391             "  Current lon=0.00 Sidereal Time = " << t->gst );
392     FG_LOG( FG_EVENT, FG_DEBUG,
393             "  Current LOCAL Sidereal Time = " << t->lst << " (" 
394             << sidereal_precise(t->mjd, -(f->get_Longitude() * RAD_TO_DEG)) 
395             << ") (diff = " << t->gst_diff << ")" );
396 }
397
398