From 7a65a990ba9cc895dbf1970f0312f9fafb98b680 Mon Sep 17 00:00:00 2001 From: curt Date: Mon, 11 Feb 2002 22:27:27 +0000 Subject: [PATCH] Decouple sg_time code from the current time(NULL), i.e. you can run the time calculations for an alternate calendar time (i.e. if the application is being driven from an external source of data.) Fix a bug in sg_binobj.cxx which disrupted the 'broken down time' globally for the entire application. --- simgear/io/sg_binobj.cxx | 9 ++++++++- simgear/timing/sg_time.cxx | 23 +++++++++++++++++------ simgear/timing/sg_time.hxx | 9 ++++++--- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index ff72ea50..c6cfc636 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -205,11 +205,18 @@ bool SGBinObject::read_bin( const string& file ) { // read creation time time_t calendar_time; sgReadLong( fp, &calendar_time ); + +#if 0 + // The following code has a global effect on the host application + // and can screws up the time elsewhere. It should be avoided + // unless you need this for debugging in which case you should + // disable it again once the debugging task is finished. struct tm *local_tm; local_tm = localtime( &calendar_time ); char time_str[256]; strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm); - // cout << "File created on " << time_str << endl; + cout << "File created on " << time_str << endl; +#endif // read number of top level objects short nobjects; diff --git a/simgear/timing/sg_time.cxx b/simgear/timing/sg_time.cxx index ba977646..e87d1947 100644 --- a/simgear/timing/sg_time.cxx +++ b/simgear/timing/sg_time.cxx @@ -70,7 +70,8 @@ static const double J2000 = 2451545.0 - MJD0; static const double SIDRATE = 0.9972695677; -SGTime::SGTime( double lon, double lat, const string& root, time_t init_time ) +void SGTime::init( double lon, double lat, + const string& root, time_t init_time ) { SG_LOG( SG_EVENT, SG_INFO, "Initializing Time" ); @@ -108,14 +109,19 @@ SGTime::SGTime( double lon, double lat, const string& root, time_t init_time ) } } +SGTime::SGTime( double lon, double lat, const string& root, time_t init_time ) +{ + init( lon, lat, root, init_time ); +} + SGTime::SGTime( const string& root ) { - SGTime( 0.0, 0.0, root ); + init( 0.0, 0.0, root, 0 ); } SGTime::SGTime() { - SGTime( 0.0, 0.0, "" ); + init( 0.0, 0.0, "", 0 ); } @@ -158,7 +164,7 @@ static double sidereal_precise( double mjd, double lng ) // return a courser but cheaper estimate of sidereal time -static double sidereal_course( time_t cur_time, struct tm *gmt, double lng ) +static double sidereal_course( time_t cur_time, const struct tm *gmt, double lng ) { time_t start_gmt, now; double diff, part, days, hours, lstTmp; @@ -228,7 +234,7 @@ void SGTime::update( double lon, double lat, time_t ct, long int warp ) { << gmt->tm_sec ); // calculate modified Julian date starting with current - mjd = sgTimeCurrentMJD( warp ); + mjd = sgTimeCurrentMJD( ct, warp ); // add in partial day mjd += (gmt->tm_hour / 24.0) + (gmt->tm_min / (24.0 * 60.0)) + @@ -379,7 +385,12 @@ double sgTimeCurrentMJD( time_t ct, long int warp ) { // get current Unix calendar time (in seconds) // warp += warp_delta; - time_t cur_time = time(NULL) + warp; + time_t cur_time; + if ( ct ) { + cur_time = ct + warp; + } else { + cur_time = time(NULL) + warp; + } SG_LOG( SG_EVENT, SG_DEBUG, " Current Unix calendar time = " << cur_time << " warp = " << warp ); diff --git a/simgear/timing/sg_time.hxx b/simgear/timing/sg_time.hxx index 800394de..f2bab45a 100644 --- a/simgear/timing/sg_time.hxx +++ b/simgear/timing/sg_time.hxx @@ -125,7 +125,7 @@ public: * @param root root path point to data file location (timezone, etc.) * @param init_time provide an initialization time, 0 means use current clock time */ - SGTime( double lon, double lat, const string& root, time_t init_time = 0 ); + SGTime( double lon, double lat, const string& root, time_t init_time /* = 0 */ ); /** * Create an instance given a data file path. @@ -136,6 +136,9 @@ public: /** Destructor */ ~SGTime(); + /** init common constructor code */ + void init( double lon, double lat, const string& root, time_t init_time /* = 0 */ ); + /** * Update the time related variables. * The update() method requires you to pass in your position and @@ -149,7 +152,7 @@ public: clock time * @param warp an optional time offset specified in seconds. This * allows us to advance or rewind "time" if we choose to. */ - void update( double lon, double lat, time_t ct = 0, long int warp = 0 ); + void update( double lon, double lat, time_t ct /* = 0 */, long int warp /* = 0 */ ); /** * Given lon/lat, update timezone information and local_offset @@ -244,7 +247,7 @@ double sgTimeCalcMJD(int mn, double dy, int yr); * @param warp number of seconds to offset from current time (0 if no offset) * @return current modified Julian date (number of days elapsed * since 1900 jan 0.5), mjd. */ -double sgTimeCurrentMJD( time_t ct = 0, long int warp = 0 ); +double sgTimeCurrentMJD( time_t ct /* = 0 */, long int warp /* = 0 */ ); /** * \relates SGTime -- 2.39.5