]> git.mxchange.org Git - simgear.git/blobdiff - simgear/timing/sg_time.cxx
Patch from Melchior Franz:
[simgear.git] / simgear / timing / sg_time.cxx
index 698ae0080d085adb60e20a9102513b6840b987fc..d2823b7794a9738efe26349a900fc11d154dc2e7 100644 (file)
 #  include <stdlib.h>
 #endif
 
+#ifdef HAVE_SYS_TIME_H
+#  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
+#endif
 #ifdef HAVE_SYS_TIMEB_H
 #  include <sys/timeb.h> // for ftime() and struct timeb
 #endif
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>    // for gettimeofday()
 #endif
-#ifdef HAVE_SYS_TIME_H
-#  include <sys/time.h>  // for get/setitimer, gettimeofday, struct timeval
-#endif
 
 #include <math.h>        // for NAN
 
@@ -70,13 +70,19 @@ static const double J2000   = 2451545.0 - MJD0;
 static const double SIDRATE = 0.9972695677;
 
 
-SGTime::SGTime( double lon, double lat, const string& root )
+void SGTime::init( double lon, double lat,
+                   const string& root, time_t init_time )
 {
     SG_LOG( SG_EVENT, SG_INFO, "Initializing Time" );
 
     gst_diff = -9999.0;
 
-    cur_time = time(NULL); 
+    if ( init_time ) {
+       cur_time = init_time;
+    } else {
+       cur_time = time(NULL); 
+    }
+
     cout << "Current greenwich mean time = " << asctime(gmtime(&cur_time))
          << endl;
     cout << "Current local time          = " 
@@ -103,14 +109,19 @@ SGTime::SGTime( double lon, double lat, const string& root )
     }
 }
 
+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 );
 }
 
 
@@ -125,7 +136,7 @@ SGTime::~SGTime()
     if ( zonename != NULL ) {
         char *tmp = zonename;
         zonename = NULL;
-       delete tmp;
+       free(tmp);
     }
 }
 
@@ -153,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;
@@ -190,7 +201,7 @@ static double sidereal_course( time_t cur_time, struct tm *gmt, double lng )
 
 
 // Update the time related variables
-void SGTime::update( double lon, double lat, long int warp ) {
+void SGTime::update( double lon, double lat, time_t ct, long int warp ) {
     double gst_precise, gst_course;
 
 #if defined(_MSC_VER) || defined(__MINGW32__)
@@ -201,7 +212,11 @@ void SGTime::update( double lon, double lat, long int warp ) {
 
     // get current Unix calendar time (in seconds)
     // warp += warp_delta;
-    cur_time = time(NULL) + warp;
+    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 );
@@ -219,7 +234,7 @@ void SGTime::update( double lon, double lat, 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)) +
@@ -287,7 +302,7 @@ void SGTime::updateLocal( double lon, double lat, const string& root ) {
     if ( zonename ) {
         char *ptr = zonename;
         zonename = NULL;
-        delete ptr;
+        free(ptr);
     }
     zonename = strdup( zone.c_str() );
 
@@ -359,21 +374,23 @@ double sgTimeCalcMJD(int mn, double dy, int yr) {
 
 // return the current modified Julian date (number of days elapsed
 // since 1900 jan 0.5), mjd.
-double sgTimeCurrentMJD( long int warp ) {
+double sgTimeCurrentMJD( time_t ct, long int warp ) {
 
 #if defined(_MSC_VER) || defined(__MINGW32__)
     struct tm m_gmt;    // copy of system gmtime(&time_t) structure
+    struct tm *gmt = &m_gmt;
 #else
     struct tm *gmt;
 #endif
 
-#if defined(_MSC_VER) || defined(__MINGW32__)
-    tm * gmt = &m_gmt;
-#endif
-
     // 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 );