1 // fg_time.cxx -- data structures and routines for managing time related stuff.
3 // Written by Curtis Olson, started August 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
28 #include <Include/compiler.h>
30 #ifdef FG_HAVE_STD_INCLUDES
42 #ifdef HAVE_SYS_TIMEB_H
43 # include <sys/timeb.h> // for ftime() and struct timeb
46 # include <unistd.h> // for gettimeofday()
48 #ifdef HAVE_SYS_TIME_H
49 # include <sys/time.h> // for get/setitimer, gettimeofday, struct timeval
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>
60 #include "fg_time.hxx"
63 #define DEGHR(x) ((x)/15.)
64 #define RADHR(x) DEGHR(x*RAD_TO_DEG)
67 // #define MK_TIME_IS_GMT 0 // default value
68 // #define TIME_ZONE_OFFSET_WORK 0 // default value
73 if (cur_time_params) {
74 FG_LOG( FG_GENERAL, FG_ALERT,
75 "Error: only one instance of FGTime allowed" );
79 cur_time_params = this;
88 // Initialize the time dependent variables (maybe I'll put this in the
92 FG_LOG( FG_EVENT, FG_INFO, "Initializing Time" );
94 FG_LOG( FG_EVENT, FG_DEBUG,
95 "time offset = " << current_options.get_time_offset() );
96 time_t timeOffset = current_options.get_time_offset();
97 time_t gstStart = current_options.get_start_gst();
98 time_t lstStart = current_options.get_start_lst();
100 // would it be better to put these sanity checks in the options
101 // parsing code? (CLO)
102 if (timeOffset && gstStart)
104 FG_LOG( FG_EVENT, FG_ALERT, "Error: you specified both a time offset and a gst. Confused now!" );
105 current_options.usage();
108 if (timeOffset && lstStart)
110 FG_LOG( FG_EVENT, FG_ALERT, "Error: you specified both a time offset and a lst. Confused now!" );
111 current_options.usage();
114 if (gstStart && lstStart)
116 FG_LOG( FG_EVENT, FG_ALERT, "Error: you specified both a time offset and a lst. Confused now!" );
117 current_options.usage();
120 cur_time = time(NULL);
122 warp = gstStart - cur_time;
123 else if (lstStart) // I need to use the time zone info for this one, but I'll do that later.
124 // Until then, Gst and LST are the same
126 warp = lstStart - cur_time;
137 pause = current_options.get_pause();
141 // given a date in months, mn, days, dy, years, yr, return the
142 // modified Julian date (number of days elapsed since 1900 jan 0.5),
143 // mjd. Adapted from Xephem.
145 void FGTime::cal_mjd (int mn, double dy, int yr)
147 //static double last_mjd, last_dy;
149 //static int last_mn, last_yr;
153 if (mn == last_mn && yr == last_yr && dy == last_dy) {
159 y = (yr < 0) ? yr + 1 : yr;
165 if (yr < 1582 || (yr == 1582 && (mn < 10 || (mn == 10 && dy < 15)))) {
174 c = (long)((365.25*y) - 0.75) - 694025L;
176 c = (long)(365.25*y) - 694025L;
179 d = (int)(30.6001*(m+1));
181 mjd = b + c + d + dy - 0.5;
192 // given an mjd, calculate greenwich mean sidereal time, gst
193 void FGTime::utc_gst ()
195 double day = floor(mjd-0.5)+0.5;
196 double hr = (mjd-day)*24.0;
199 T = ((int)(mjd - 0.5) + 0.5 - J2000)/36525.0;
200 x = 24110.54841 + (8640184.812866 + (0.093104 - 6.2e-6 * T) * T) * T;
202 gst = (1.0/SIDRATE)*hr + x;
204 FG_LOG( FG_EVENT, FG_DEBUG, " gst => " << gst );
208 // given Julian Date and Longitude (decimal degrees West) compute
209 // Local Sidereal Time, in decimal hours.
211 // Provided courtesy of ecdowney@noao.edu (Elwood Downey)
213 double FGTime::sidereal_precise (double lng)
217 /* printf ("Current Lst on JD %13.5f at %8.4f degrees West: ",
220 // convert to required internal units
223 // compute LST and print
225 lstTmp = gst - RADHR (lng);
226 lstTmp -= 24.0*floor(lstTmp/24.0);
227 // printf ("%7.4f\n", lstTmp);
234 // return a courser but cheaper estimate of sidereal time
235 double FGTime::sidereal_course(double lng)
239 time_t start_gmt, now;
240 double diff, part, days, hours, lstTmp;
246 start_gmt = get_gmt(gmt->tm_year, 2, 21, 12, 0, 0);
248 FG_LOG( FG_EVENT, FG_DEBUG, " COURSE: GMT = " << format_time(gmt, tbuf) );
249 FG_LOG( FG_EVENT, FG_DEBUG, " March 21 noon (GMT) = " << start_gmt );
251 diff = (now - start_gmt) / (3600.0 * 24.0);
253 FG_LOG( FG_EVENT, FG_DEBUG,
254 " Time since 3/21/" << gmt->tm_year << " GMT = " << diff );
256 part = fmod(diff, 1.0);
258 hours = gmt->tm_hour + gmt->tm_min/60.0 + gmt->tm_sec/3600.0;
260 lstTmp = (days - lng)/15.0 + hours - 12;
262 while ( lstTmp < 0.0 ) {
266 FG_LOG( FG_EVENT, FG_DEBUG,
267 " days = " << days << " hours = " << hours << " lon = "
268 << lng << " lst = " << lstTmp );
274 // Update time variables such as gmt, julian date, and sidereal time
275 void FGTime::update(FGInterface *f)
277 double gst_precise, gst_course;
279 FG_LOG( FG_EVENT, FG_DEBUG, "Updating time" );
281 // get current Unix calendar time (in seconds)
283 cur_time = time(NULL) + warp;
284 FG_LOG( FG_EVENT, FG_DEBUG,
285 " Current Unix calendar time = " << cur_time
286 << " warp = " << warp << " delta = " << warp_delta );
289 // time is changing so force an update
290 local_update_sky_and_lighting_params();
293 // get GMT break down for current time
294 gmt = gmtime(&cur_time);
295 FG_LOG( FG_EVENT, FG_DEBUG,
296 " Current GMT = " << gmt->tm_mon+1 << "/"
297 << gmt->tm_mday << "/" << gmt->tm_year << " "
298 << gmt->tm_hour << ":" << gmt->tm_min << ":"
301 // calculate modified Julian date
302 // t->mjd = cal_mjd ((int)(t->gmt->tm_mon+1), (double)t->gmt->tm_mday,
303 // (int)(t->gmt->tm_year + 1900));
304 cal_mjd ((int)(gmt->tm_mon+1), (double)gmt->tm_mday,
305 (int)(gmt->tm_year + 1900));
307 // add in partial day
308 mjd += (gmt->tm_hour / 24.0) + (gmt->tm_min / (24.0 * 60.0)) +
309 (gmt->tm_sec / (24.0 * 60.0 * 60.0));
311 // convert "back" to Julian date + partial day (as a fraction of one)
313 FG_LOG( FG_EVENT, FG_DEBUG, " Current Julian Date = " << jd );
315 // printf(" Current Longitude = %.3f\n", FG_Longitude * RAD_TO_DEG);
317 // Calculate local side real time
318 if ( gst_diff < -100.0 ) {
319 // first time through do the expensive calculation & cheap
320 // calculation to get the difference.
321 FG_LOG( FG_EVENT, FG_INFO, " First time, doing precise gst" );
322 gst_precise = gst = sidereal_precise(0.00);
323 gst_course = sidereal_course(0.00);
325 gst_diff = gst_precise - gst_course;
327 lst = sidereal_course(-(f->get_Longitude() * RAD_TO_DEG)) + gst_diff;
329 // course + difference should drift off very slowly
330 gst = sidereal_course( 0.00 ) + gst_diff;
331 lst = sidereal_course( -(f->get_Longitude() * RAD_TO_DEG)) + gst_diff;
333 FG_LOG( FG_EVENT, FG_DEBUG,
334 " Current lon=0.00 Sidereal Time = " << gst );
335 FG_LOG( FG_EVENT, FG_DEBUG,
336 " Current LOCAL Sidereal Time = " << lst << " ("
337 << sidereal_precise(-(f->get_Longitude() * RAD_TO_DEG))
338 << ") (diff = " << gst_diff << ")" );
342 /******************************************************************
343 * The following are some functions that were included as FGTime
344 * members, although they currently don't make use of any of the
345 * class's variables. Maybe this'll change in the future
346 *****************************************************************/
348 // Return time_t for Sat Mar 21 12:00:00 GMT
350 // On many systems it is ambiguous if mktime() assumes the input is in
351 // GMT, or local timezone. To address this, a new function called
352 // timegm() is appearing. It works exactly like mktime() but
353 // explicitely interprets the input as GMT.
355 // timegm() is available and documented under FreeBSD. It is
356 // available, but completely undocumented on my current Debian 2.1
359 // In the absence of timegm() we have to guess what mktime() might do.
361 // Many older BSD style systems have a mktime() that assumes the input
362 // time in GMT. But FreeBSD explicitly states that mktime() assumes
365 // The mktime() on many SYSV style systems (such as Linux) usually
366 // returns its result assuming you have specified the input time in
367 // your local timezone. Therefore, in the absence if timegm() you
368 // have to go to extra trouble to convert back to GMT.
370 // If you are having problems with incorrectly positioned astronomical
371 // bodies, this is a really good place to start looking.
373 time_t FGTime::get_gmt(int year, int month, int day, int hour, int min, int sec)
383 mt.tm_isdst = -1; // let the system determine the proper time zone
385 // For now we assume that if daylight is not defined in
386 // /usr/include/time.h that we have a machine with a mktime() that
387 // assumes input is in GMT ... this only matters if we are
388 // building on a system that does not have timegm()
389 #if !defined(HAVE_DAYLIGHT)
390 # define MK_TIME_IS_GMT 1
393 #if defined( HAVE_TIMEGM )
394 return ( timegm(&mt) );
395 #elif defined( MK_TIME_IS_GMT )
396 return ( mktime(&mt) );
397 #else // ! defined ( MK_TIME_IS_GMT )
399 // timezone seems to work as a proper offset for Linux & Solaris
400 # if defined( __linux__ ) || defined( __sun__ )
401 # define TIMEZONE_OFFSET_WORKS 1
404 long int start = mktime(&mt);
406 FG_LOG( FG_EVENT, FG_DEBUG, "start1 = " << start );
407 // the ctime() call can screw up time progression on some versions
409 // fgPrintf( FG_EVENT, FG_DEBUG, "start2 = %s", ctime(&start));
410 FG_LOG( FG_EVENT, FG_DEBUG, "(tm_isdst = " << mt.tm_isdst << ")" );
412 timezone = fix_up_timezone( timezone );
414 # if defined( TIMEZONE_OFFSET_WORKS )
415 FG_LOG( FG_EVENT, FG_DEBUG,
416 "start = " << start << ", timezone = " << timezone );
417 return( start - timezone );
418 # else // ! defined( TIMEZONE_OFFSET_WORKS )
420 daylight = mt.tm_isdst;
421 if ( daylight > 0 ) {
423 } else if ( daylight < 0 ) {
424 FG_LOG( FG_EVENT, FG_WARN,
425 "OOOPS, problem in fg_time.cxx, no daylight savings info." );
428 long int offset = -(timezone / 3600 - daylight);
430 FG_LOG( FG_EVENT, FG_DEBUG, " Raw time zone offset = " << timezone );
431 FG_LOG( FG_EVENT, FG_DEBUG, " Daylight Savings = " << daylight );
432 FG_LOG( FG_EVENT, FG_DEBUG, " Local hours from GMT = " << offset );
434 long int start_gmt = start - timezone + (daylight * 3600);
436 FG_LOG( FG_EVENT, FG_DEBUG, " March 21 noon (CST) = " << start );
438 return ( start_gmt );
439 # endif // ! defined( TIMEZONE_OFFSET_WORKS )
440 #endif // ! defined ( MK_TIME_IS_GMT )
443 // Fix up timezone if using ftime()
444 long int FGTime::fix_up_timezone( long int timezone_orig )
446 #if !defined( HAVE_GETTIMEOFDAY ) && defined( HAVE_FTIME )
447 // ftime() needs a little extra help finding the current timezone
448 struct timeb current;
450 return( current.timezone * 60 );
452 return( timezone_orig );
457 char* FGTime::format_time( const struct tm* p, char* buf )
459 sprintf( buf, "%d/%d/%2d %d:%02d:%02d",
460 p->tm_mon, p->tm_mday, p->tm_year,
461 p->tm_hour, p->tm_min, p->tm_sec);
466 // Force an update of the sky and lighting parameters
467 void FGTime::local_update_sky_and_lighting_params( void ) {
469 SolarSystem::theSolarSystem->rebuild();
470 cur_light_params.Update();
475 FGTime* FGTime::cur_time_params = 0;