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)
66 // #define MK_TIME_IS_GMT 0 // default value
67 // #define TIME_ZONE_OFFSET_WORK 0 // default value
70 fgTIME cur_time_params;
73 // Force an update of the sky and lighting parameters
74 static void local_update_sky_and_lighting_params( void ) {
76 SolarSystem::theSolarSystem->rebuild();
77 cur_light_params.Update();
82 // Initialize the time dependent variables
83 void fgTimeInit(fgTIME *t) {
84 FG_LOG( FG_EVENT, FG_INFO, "Initializing Time" );
86 t->gst_diff = -9999.0;
88 FG_LOG( FG_EVENT, FG_DEBUG,
89 "time offset = " << current_options.get_time_offset() );
91 t->warp = current_options.get_time_offset();
94 t->pause = current_options.get_pause();
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.
102 double cal_mjd (int mn, double dy, int yr) {
103 static double last_mjd, last_dy;
105 static int last_mn, last_yr;
109 if (mn == last_mn && yr == last_yr && dy == last_dy) {
115 y = (yr < 0) ? yr + 1 : yr;
121 if (yr < 1582 || (yr == 1582 && (mn < 10 || (mn == 10 && dy < 15)))) {
130 c = (long)((365.25*y) - 0.75) - 694025L;
132 c = (long)(365.25*y) - 694025L;
135 d = (int)(30.6001*(m+1));
137 mjd = b + c + d + dy - 0.5;
148 // given an mjd, return greenwich mean sidereal time, gst
150 double utc_gst (double mjd) {
152 double day = floor(mjd-0.5)+0.5;
153 double hr = (mjd-day)*24.0;
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;
159 gst = (1.0/SIDRATE)*hr + x;
161 FG_LOG( FG_EVENT, FG_DEBUG, " gst => " << gst );
167 // given Julian Date and Longitude (decimal degrees West) compute and
168 // return Local Sidereal Time, in decimal hours.
170 // Provided courtesy of ecdowney@noao.edu (Elwood Downey)
173 double sidereal_precise (double mjd, double lng) {
177 /* printf ("Current Lst on JD %13.5f at %8.4f degrees West: ",
180 // convert to required internal units
183 // compute LST and print
185 lst = gst - RADHR (lng);
186 lst -= 24.0*floor(lst/24.0);
187 // printf ("%7.4f\n", lst);
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;
200 return( current.timezone * 60 );
202 return( timezone_orig );
207 // Return time_t for Sat Mar 21 12:00:00 GMT
209 // I believe the mktime() has a SYSV vs. BSD behavior difference.
211 // The BSD style mktime() is nice because it returns its result
212 // assuming you have specified the input time in GMT
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.
218 // If you are having problems with incorrectly positioned astronomical
219 // bodies, this is a really good place to start looking.
221 time_t get_start_gmt(int year) {
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
227 # if !defined(HAVE_DAYLIGHT)
228 # define MK_TIME_IS_GMT 1
231 // timezone seems to work as a proper offset for Linux & Solaris
232 # if defined( __linux__ ) || defined( __sun__ )
233 # define TIMEZONE_OFFSET_WORKS 1
242 mt.tm_isdst = -1; // let the system determine the proper time zone
244 # if defined( MK_TIME_IS_GMT )
245 return ( mktime(&mt) );
246 # else // ! defined ( MK_TIME_IS_GMT )
248 long int start = mktime(&mt);
250 FG_LOG( FG_EVENT, FG_DEBUG, "start1 = " << start );
251 // the ctime() call can screw up time progression on some versions
253 // fgPrintf( FG_EVENT, FG_DEBUG, "start2 = %s", ctime(&start));
254 FG_LOG( FG_EVENT, FG_DEBUG, "(tm_isdst = " << mt.tm_isdst << ")" );
256 timezone = fix_up_timezone( timezone );
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 )
264 daylight = mt.tm_isdst;
265 if ( daylight > 0 ) {
267 } else if ( daylight < 0 ) {
268 FG_LOG( FG_EVENT, FG_WARN,
269 "OOOPS, problem in fg_time.cxx, no daylight savings info." );
272 long int offset = -(timezone / 3600 - daylight);
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 );
278 long int start_gmt = start - timezone + (daylight * 3600);
280 FG_LOG( FG_EVENT, FG_DEBUG, " March 21 noon (CST) = " << start );
282 return ( start_gmt );
283 # endif // ! defined( TIMEZONE_OFFSET_WORKS )
284 # endif // ! defined ( MK_TIME_IS_GMT )
288 format_time( const struct tm* p, char* buf )
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);
296 // return a courser but cheaper estimate of sidereal time
297 double sidereal_course(fgTIME *t, double lng) {
299 time_t start_gmt, now;
300 double diff, part, days, hours, lst;
305 start_gmt = get_start_gmt(gmt->tm_year);
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 );
310 diff = (now - start_gmt) / (3600.0 * 24.0);
312 FG_LOG( FG_EVENT, FG_DEBUG,
313 " Time since 3/21/" << gmt->tm_year << " GMT = " << diff );
315 part = fmod(diff, 1.0);
317 hours = gmt->tm_hour + gmt->tm_min/60.0 + gmt->tm_sec/3600.0;
319 lst = (days - lng)/15.0 + hours - 12;
321 while ( lst < 0.0 ) {
325 FG_LOG( FG_EVENT, FG_DEBUG,
326 " days = " << days << " hours = " << hours << " lon = "
327 << lng << " lst = " << lst );
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;
337 FG_LOG( FG_EVENT, FG_DEBUG, "Updating time" );
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 );
346 if ( t->warp_delta ) {
347 // time is changing so force an update
348 local_update_sky_and_lighting_params();
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 << ":"
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));
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));
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 );
371 // printf(" Current Longitude = %.3f\n", FG_Longitude * RAD_TO_DEG);
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;
383 sidereal_course(t, -(f->get_Longitude() * RAD_TO_DEG)) + t->gst_diff;
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)) +
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 << ")" );