]> git.mxchange.org Git - simgear.git/commitdiff
Decouple sg_time code from the current time(NULL), i.e. you can run the
authorcurt <curt>
Mon, 11 Feb 2002 22:27:27 +0000 (22:27 +0000)
committercurt <curt>
Mon, 11 Feb 2002 22:27:27 +0000 (22:27 +0000)
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
simgear/timing/sg_time.cxx
simgear/timing/sg_time.hxx

index ff72ea501bf2e048f0676e64fe2e5fb01914d286..c6cfc63686eaa23a2a9181bb210a3a82f4cacd7b 100644 (file)
@@ -205,11 +205,18 @@ bool SGBinObject::read_bin( const string& file ) {
     // read creation time
     time_t calendar_time;
     sgReadLong( fp, &calendar_time );
     // 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);
     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;
 
     // read number of top level objects
     short nobjects;
index ba977646a254d24d554c451ae69a5cc4566126dc..e87d19475886ed4a48fa66a04f437cf23806a928 100644 (file)
@@ -70,7 +70,8 @@ static const double J2000   = 2451545.0 - MJD0;
 static const double SIDRATE = 0.9972695677;
 
 
 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" );
 
 {
     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::SGTime( const string& root ) {
-    SGTime( 0.0, 0.0, root );
+    init( 0.0, 0.0, root, 0 );
 }
 
 
 SGTime::SGTime() {
 }
 
 
 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
 
 
 // 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;
 {
     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
            << 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)) +
 
     // 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;
 
     // 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 );
     SG_LOG( SG_EVENT, SG_DEBUG, 
            "  Current Unix calendar time = " << cur_time 
            << "  warp = " << warp );
index 800394de3c0f8a8913ebf0398198615c77824475..f2bab45aff35d83e9bae408e5a0b894475a6c0c6 100644 (file)
@@ -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 */
      * @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.
 
     /**
      * Create an instance given a data file path.
@@ -136,6 +136,9 @@ public:
     /** Destructor */
     ~SGTime();
 
     /** 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
     /** 
      * 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.  */
               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
 
     /**
      * 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. */
  * @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
 
 /**
  * \relates SGTime