]> git.mxchange.org Git - flightgear.git/commitdiff
Initial revision of:
authorcurt <curt>
Tue, 7 Sep 1999 23:11:22 +0000 (23:11 +0000)
committercurt <curt>
Tue, 7 Sep 1999 23:11:22 +0000 (23:11 +0000)
   geocoord.cxx geocoord.h lowleveltime.cxx lowleveltime.h
   mymath.cxx mymath.h timezone.cxx timezone.h
Incorporating Durk's better timezone handling:
   Makefile.am fg_time.cxx fg_time.hxx

src/Time/Makefile.am
src/Time/fg_time.cxx
src/Time/fg_time.hxx
src/Time/geocoord.cxx [new file with mode: 0644]
src/Time/geocoord.h [new file with mode: 0644]
src/Time/lowleveltime.cxx [new file with mode: 0644]
src/Time/lowleveltime.h [new file with mode: 0644]
src/Time/mymath.cxx [new file with mode: 0755]
src/Time/mymath.h [new file with mode: 0755]
src/Time/timezone.cxx [new file with mode: 0644]
src/Time/timezone.h [new file with mode: 0644]

index 2566840eeced79fc4ca7c324ebf5ee92a995b635..56443e6ba4f3696b041b2d789cbb3181a4740753 100644 (file)
@@ -4,8 +4,12 @@ libTime_a_SOURCES = \
        event.cxx event.hxx \
        fg_time.cxx fg_time.hxx \
        fg_timer.cxx fg_timer.hxx \
+       geocoord.cxx geocoord.h \
        light.cxx light.hxx \
+       lowleveltime.cxx lowleveltime.h \
+       timezone.cxx timezone.h \
        moonpos.cxx moonpos.hxx \
+       mymath.cxx mymath.h \
        sunpos.cxx sunpos.hxx \
        timestamp.hxx
 
index ad92f980cc580811c730d912de99f7b527efcc74..fe31c5c9aed7dcedf49a700625ee3e49e24c7070 100644 (file)
 #include <FDM/flight.hxx>
 #include <Include/fg_constants.h>
 #include <Main/options.hxx>
+#include <Misc/fgpath.hxx>
 #include <Time/light.hxx>
 
 #include "fg_time.hxx"
+#include "timezone.h"
+#include "lowleveltime.h"
 
 
 #define DEGHR(x)        ((x)/15.)
@@ -77,62 +80,103 @@ FGTime::FGTime()
     }
 
     cur_time_params = this;
+
+    FGPath buffer( current_options.get_fg_root() );
+    buffer.append( "Timezone" );
+    buffer.append( "zone.tab" );
+    FG_LOG( FG_EVENT, FG_INFO, "Reading timezone info from: " << buffer.str() );
+    tzContainer = new TimezoneContainer( buffer.c_str() );
+    warp=0;
+    warp_delta=0;
 }
 
 
 FGTime::~FGTime()
 {
+    delete tzContainer;
+    delete zonename;
 }
 
 
 // Initialize the time dependent variables (maybe I'll put this in the
 // constructor later)
-void FGTime::init() 
+void FGTime::init(FGInterface *f
 {
     FG_LOG( FG_EVENT, FG_INFO, "Initializing Time" );
     gst_diff = -9999.0;
     FG_LOG( FG_EVENT, FG_DEBUG, 
            "time offset = " << current_options.get_time_offset() );
     time_t timeOffset = current_options.get_time_offset();
-    time_t gstStart = current_options.get_start_gst();
-    time_t lstStart = current_options.get_start_lst();
+    int offsetType = current_options.get_time_offset_type();
+
+    time_t currGMT;
+    time_t systemLocalTime;
+    time_t aircraftLocalTime;
 
     // would it be better to put these sanity checks in the options
     // parsing code? (CLO)
-    if (timeOffset && gstStart)
-       {
-           FG_LOG( FG_EVENT, FG_ALERT, "Error: you specified both a time offset and a gst. Confused now!" );
-           current_options.usage();
-           exit(1);
-       }
-    if (timeOffset && lstStart)
-       {
-           FG_LOG( FG_EVENT, FG_ALERT, "Error: you specified both a time offset and a lst. Confused now!" );
-           current_options.usage();
-           exit(1);
-       }
-    if (gstStart && lstStart)
-       {
-           FG_LOG( FG_EVENT, FG_ALERT, "Error: you specified both a time offset and a lst. Confused now!" );
-           current_options.usage();
-           exit(1);
-       }
+
     cur_time = time(NULL); 
-    if (gstStart)
-       warp = gstStart - cur_time;
-    else if (lstStart) // I need to use the time zone info for this one, but I'll do that later.
-       // Until then, Gst and LST are the same 
-       {
-           warp = lstStart - cur_time;
-       }
-    else if (timeOffset)
+
+    // printf ("Current greenwich mean time = %24s", asctime(gmtime(&cur_time)));
+    // printf ("Current local time          = %24s", asctime(localtime(&cur_time)));
+    // time_t tmp = cur_time;
+    GeoCoord location(RAD_TO_DEG * f->get_Latitude(), 
+                     RAD_TO_DEG * f->get_Longitude());
+
+    GeoCoord* nearestTz = tzContainer->getNearest(location);
+
+    FGPath buffer( current_options.get_fg_root() );
+    buffer.append( "Timezone" );
+    buffer.append( nearestTz->getDescription() );
+
+    // printf("Using %s for timezone information\n", buffer);
+    zonename = strdup( buffer.c_str() );
+    show( buffer.c_str(), cur_time, 1); 
+    //printf ("Current greenwich mean time = %24s", asctime(gmtime(&cur_time)));
+    //printf ("Current local time          = %24s", asctime(localtime(&cur_time)));
+    currGMT = get_gmt(gmtime(&cur_time));
+    systemLocalTime = get_gmt(localtime(&cur_time));
+    aircraftLocalTime = get_gmt(fgLocaltime(&cur_time, buffer.c_str())); 
+    //printf ("Current greenwich mean time = %24s", asctime(gmtime(&cur_time)));
+    //printf ("Current local time          = %24s", asctime(localtime(&cur_time)));
+
+    //printf("LT  = %d\n", computerLocalTime);
+    // Okay, in principle, this trick allows to calculate the
+    // difference between GMT and localtime, in seconds.
+    // printf("Gmt = %d, SLT = %d, (difference = %d)\n", currGMT, systemLocalTime,   (currGMT - systemLocalTime));
+    // printf("Gmt = %d, ALT = %d, (difference = %d)\n", currGMT, aircraftLocalTime, (currGMT - aircraftLocalTime));
+    // exit(1);
+    // Okay, we now have six possible scenarios
+    switch (offsetType)
        {
+       case fgOPTIONS::FG_TIME_SYS_OFFSET:
            warp = timeOffset;
+           break;
+       case fgOPTIONS::FG_TIME_GMT_OFFSET:
+           warp = timeOffset - (currGMT - systemLocalTime);
+           break;
+       case fgOPTIONS::FG_TIME_LAT_OFFSET:
+           // warp = timeOffset - (currGMT - systemLocalTime + 
+           //        (currGMT - aircraftLocalTime));
+           warp = timeOffset - (aircraftLocalTime - systemLocalTime); 
+           break;
+       case fgOPTIONS::FG_TIME_SYS_ABSOLUTE:
+           warp = timeOffset - cur_time;
+           //printf("warp = %d\n", warp); 
+           break;
+       case fgOPTIONS::FG_TIME_GMT_ABSOLUTE:
+           warp = timeOffset - (currGMT - systemLocalTime) - cur_time;
+           break;
+       case fgOPTIONS::FG_TIME_LAT_ABSOLUTE:
+           warp = timeOffset - (aircraftLocalTime - systemLocalTime) - 
+               cur_time; 
+           break;
+       default:
+           printf("Unsupported type\n");
+           exit(1);
        }
-    else
-       {
-           warp = 0;
-       }
+
     warp_delta = 0;
     pause = current_options.get_pause();
 }
index 2a146cda16a5860bffcd95ff1fad33e35912f7b0..c87fc2403969bab479e54d3fff53bb58930166b8 100644 (file)
 
 #include <FDM/flight.hxx>
 
+#include "timezone.h"
+#include "lowleveltime.h"
 
 // Define a structure containing global time parameters
 class FGTime {
 
 private:
+    // tzContainer stores all the current Timezone control points/
+    TimezoneContainer* tzContainer;
+
+    //Store the current local timezone name;
+    char *zonename;
 
     // Unix "calendar" time in seconds
     time_t cur_time;
@@ -111,7 +118,7 @@ public:
     void togglePauseMode() { pause = !pause; }; 
 
     // Initialize the time dependent variables
-    void init();
+    void init(FGInterface *f);
 
     // Update the time dependent variables
     void update(FGInterface *f);
@@ -127,9 +134,23 @@ public:
     // time_t get_start_gmt(int year);
     time_t get_gmt(int year, int month, int day, 
                   int hour, int minute, int second);
+    time_t get_gmt(struct tm* the_time);
+  
     char* format_time( const struct tm* p, char* buf );
     long int fix_up_timezone( long int timezone_orig );
 };
 
 
+inline time_t FGTime::get_gmt(struct tm* the_time) // this is just a wrapper
+{
+  //printf("Using: %24s as input\n", asctime(the_time));
+  return get_gmt(the_time->tm_year,
+         the_time->tm_mon,
+         the_time->tm_mday,
+         the_time->tm_hour,
+         the_time->tm_min,
+         the_time->tm_sec);
+}
+
+
 #endif // _FG_TIME_HXX
diff --git a/src/Time/geocoord.cxx b/src/Time/geocoord.cxx
new file mode 100644 (file)
index 0000000..fb1cdec
--- /dev/null
@@ -0,0 +1,81 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * geocoord.h
+ * Written by Durk Talsma. Started March 1998.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/*************************************************************************
+ *
+ * This file defines a small and simple class to store geocentric 
+ * coordinates. Basically, class GeoCoord is intended as a base class for
+ * any kind of of object, that can be categorized according to its 
+ * location on earth, be it navaids, or aircraft. This class for originally
+ * written for FlightGear, in order to store Timezone control points. 
+ *
+ ************************************************************************/
+#include "geocoord.h"
+
+GeoCoord::GeoCoord(const GeoCoord& other)
+{
+  lat = other.lat;
+  lon = other.lon;
+}
+
+double GeoCoord::getAngle(const GeoCoord& other) const
+{
+  Vector first(      getX(),       getY(),       getZ());
+  Vector secnd(other.getX(), other.getY(), other.getZ());
+    double
+      dot = VecDot(first, secnd),
+      len1 = first.VecLen(),
+      len2 = secnd.VecLen(),
+      len = len1 * len2,
+      angle = 0;
+    //printf ("Dot: %f, len1: %f len2: %f\n", dot, len1, len2);
+    /*Vector pPos = prevPos - Reference->prevPos;
+      Vector pVel = prevVel - Reference->prevVel;*/
+
+
+    if ( ( (dot / len) < 1) && (dot / len > -1) && len )
+       angle = acos(dot / len);
+    return angle;
+}
+
+GeoCoord* GeoCoordContainer::getNearest(const GeoCoord& ref) const
+{
+  float angle, maxAngle = 180;
+
+  GeoCoordVectorConstIterator i, nearest;
+  for (i = data.begin(); i != data.end(); i++)
+    {
+      angle = RAD_TO_DEG * (*i)->getAngle(ref);
+      if (angle < maxAngle)
+       {
+         maxAngle = angle;
+         nearest = i;
+       }
+    }
+  return *nearest;
+}
+
+
+GeoCoordContainer::~GeoCoordContainer()
+{
+    GeoCoordVectorIterator i = data.begin();
+  while (i != data.end())
+    delete *i++;
+}
diff --git a/src/Time/geocoord.h b/src/Time/geocoord.h
new file mode 100644 (file)
index 0000000..6549802
--- /dev/null
@@ -0,0 +1,98 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * geocoord.h
+ * Written by Durk Talsma. Started July 1999.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/*************************************************************************
+ *
+ * This file defines a small and simple class to store geocentric 
+ * coordinates. Basically, class GeoCoord is intended as a base class for
+ * any kind of of object, that can be categorized according to its 
+ * location on earth, be it navaids, or aircraft. This class for originally
+ * written for FlightGear, in order to store Timezone control points. 
+ *
+ ************************************************************************/
+
+
+#ifndef _GEOCOORD_H_
+#define _GEOCOORD_H_
+
+//#include <stl.h>
+
+
+#include <math.h>
+#include <string>
+#include <iostream>
+//#include <streambuf> // looks like streambuf does not exist on linux.
+// But it looks like it isn't used anyways -:)
+#include <vector>
+
+using namespace std;
+
+#include "mymath.h"
+
+class GeoCoord
+{
+protected:
+  float lat;
+  float lon;
+
+public:
+  GeoCoord() { lat = 0.0; lon = 0.0;};
+  GeoCoord(float la, float lo) { lat = la; lon = lo;};
+  GeoCoord(const GeoCoord& other);
+  virtual ~GeoCoord() {};
+  
+  void set(float la, float lo) { lat = la; lon = lo; }; 
+  float getLat() const { return lat; };
+  float getLon() const { return lon; };
+  float getX()   const { return cos(DEG_TO_RAD*lat) * cos(DEG_TO_RAD*lon); };
+  float getY()   const { return cos(DEG_TO_RAD*lat) * sin(DEG_TO_RAD*lon); };
+  float getZ()   const { return sin(DEG_TO_RAD*lat); };
+
+
+  double getAngle(const GeoCoord& other) const;
+  virtual void print() {} ; 
+  virtual char *getDescription() {return 0;};
+};
+
+typedef vector<GeoCoord*> GeoCoordVector;
+typedef vector<GeoCoord*>::iterator GeoCoordVectorIterator;
+typedef vector<GeoCoord*>::const_iterator GeoCoordVectorConstIterator;
+
+/************************************************************************
+ * GeoCoordContainer is a simple container class, that stores objects
+ * derived from GeoCoord. Basically, it is a wrapper around an STL vector,
+ * with some added functionality
+ ***********************************************************************/
+
+class GeoCoordContainer
+{
+protected:
+  GeoCoordVector data;
+
+public:
+  GeoCoordContainer() {};
+  virtual ~GeoCoordContainer();
+
+  const GeoCoordVector& getData() const { return data; };
+  GeoCoord* getNearest(const GeoCoord& ref) const;
+};
+
+
+#endif // _GEO_COORD_H_
diff --git a/src/Time/lowleveltime.cxx b/src/Time/lowleveltime.cxx
new file mode 100644 (file)
index 0000000..393096d
--- /dev/null
@@ -0,0 +1,1127 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * Written by various people (I"ll look up the exact credits later)
+ * Modified by Durk Talsma, July 1999 for use in FlightGear
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/********************************************************************
+ * This file redefines some low-level Unix-like time functions for  *
+ * use with FlightGear. Most notably, localtime() is adapted to use *
+ * a custom timezone, in order to get the 'local' time for a given  *
+ * aircraft's position, and not only for the current location of the*
+ * computer running the sim.                                        *
+ *                                                                  *
+ * Software adapted from glibc functions, by Durk Talsma. Started   *
+ * July, 17, 1999.                                                  *
+ ********************************************************************/
+
+
+#include <time.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+//#include <libc-lock.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+#include "lowleveltime.h"
+
+
+/* BIG FAT WARNING: NOTICE THAT I HARDCODED ENDIANNES. PLEASE CHANGE THIS */
+#ifndef BIG_ENDIAN 
+#define BIG_ENDIAN 4321
+#endif
+
+#ifndef LITTLE_ENDIAN
+#define LITTLE_ENDIAN 1234
+#endif
+
+#ifndef BYTE_ORDER 
+#define BYTE_ORDER LITTLE_ENDIAN
+#endif
+
+
+#ifndef BYTE_ORDER
+#define BYTE_ORDER 
+#endif
+
+/* END OF BIG FAT WARNING */
+
+//#include "tzfile.h"
+
+#define        min(a, b)       ((a) < (b) ? (a) : (b))
+#define        max(a, b)       ((a) > (b) ? (a) : (b))
+#define        sign(x)         ((x) < 0 ? -1 : 1)
+
+struct leap
+  {
+    time_t transition;         /* Time the transition takes effect.  */
+    long int change;           /* Seconds of correction to apply.  */
+  };
+
+/* Header for a list of buffers containing time zone strings.  */
+struct tzstring_head
+{
+  struct tzstring_head *next;
+  /* The buffer itself immediately follows the header.
+     The buffer contains zero or more (possibly overlapping) strings.
+     The last string is followed by 2 '\0's instead of the usual 1.  */
+};
+
+
+/* First in a list of buffers containing time zone strings.
+   All the buffers but the last are read-only.  */
+static struct
+{
+  struct tzstring_head head;
+  char data[48];
+} tzstring_list;
+
+/* Size of the last buffer in the list, not counting its header.  */
+static size_t tzstring_last_buffer_size = sizeof tzstring_list.data;
+
+
+static char *old_fgtz = NULL;
+static int use_fgtzfile = 1;
+static int fgdaylight;
+static char* fgtzname[2];
+static long int fgtimezone;
+
+
+static size_t num_transitions;
+static time_t *transitions = NULL;
+static unsigned char *type_idxs = NULL;
+static size_t num_types;
+static struct ttinfo *types = NULL;
+static char *zone_names = NULL;
+static size_t num_leaps;
+static struct leap *leaps = NULL;
+
+
+static void fgtzset_internal (int always, char*tz);
+static int fgtz_compute(time_t timer, const struct tm *tm);
+static int fgcompute_change(fgtz_rule *rule, int year);
+static struct ttinfo *fgfind_transition (time_t timer);
+static void fgcompute_tzname_max (size_t chars);
+static inline int decode (const void *ptr);
+void fgtzfile_read (const char *file);
+static void offtime (const time_t *t, long int offset, struct tm *tp);
+static char *tzstring (const char* string);
+
+int fgtzfile_compute (time_t timer, int use_localtime,
+                 long int *leap_correct, int *leap_hit);
+struct ttinfo
+  {
+    long int offset;           /* Seconds east of GMT.  */
+    unsigned char isdst;       /* Used to set tm_isdst.  */
+    unsigned char idx;         /* Index into `zone_names'.  */
+    unsigned char isstd;       /* Transition times are in standard time.  */
+    unsigned char isgmt;       /* Transition times are in GMT.  */
+  };
+
+
+
+/* How many days come before each month (0-12).  */
+const unsigned short int mon_yday[2][13] =
+  {
+    /* Normal years.  */
+    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
+    /* Leap years.  */
+    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
+  };
+
+
+/* The C Standard says that localtime and gmtime return the same pointer.  */
+struct tm _fgtmbuf;
+
+
+#ifndef isleap
+/* Nonzero if YEAR is a leap year (every 4 years,
+   except every 100th isn't, and every 400th is).  */
+# define isleap(year) \
+  ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
+#endif
+
+
+
+
+/* Return the `struct tm' representation of *T in local time.  */
+struct tm * fgLocaltime (const time_t *t, char *tzName)
+{
+  return fgtz_convert (t, 1, &_fgtmbuf, tzName);
+}
+
+
+/* Return the `struct tm' representation of *TIMER in the local timezone.
+   Use local time if USE_LOCALTIME is nonzero, UTC otherwise.  */
+struct tm * fgtz_convert (const time_t *timer, int use_localtime, struct tm *tp, char *tzName)
+{
+  long int leap_correction;
+  long int offsetCorr;                 // ADDED TO RESOLVE NON-ANSI FIELDS IN struct tm
+  int leap_extra_secs;
+
+  if (timer == NULL)
+    {
+      //set_errno (EINVAL);
+      return NULL;
+    }
+
+  //libc_lock_lock (tzset_lock);
+
+  /* Update internal database according to current TZ setting.
+     POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname.
+     This is a good idea since this allows at least a bit more parallelism.
+     By analogy we apply the same rule to gmtime_r.  */
+  fgtzset_internal (tp == &_fgtmbuf, tzName);
+
+  if (use_fgtzfile)
+    {
+      if (! fgtzfile_compute (*timer, use_localtime,
+                             &leap_correction, &leap_extra_secs))
+       tp = NULL;
+    }
+  else
+    {
+      offtime (timer, 0, tp);
+      if (! fgtz_compute (*timer, tp))
+       tp = NULL;
+      leap_correction = 0L;
+      leap_extra_secs = 0;
+    }
+
+  if (tp)
+    {
+      if (use_localtime)
+       {
+         tp->tm_isdst = fgdaylight;
+         //tp->tm_zone = fgtzname[fgdaylight];       // NON_ANSI
+         //tp->tm_gmtoff = -fgtimezone;              // NON_ANSI
+         offsetCorr = -fgtimezone;
+       }
+      else
+       {
+         tp->tm_isdst = 0;
+         //tp->tm_zone = "GMT";                     // NON_ANSI
+         //tp->tm_gmtoff = 0L;                      // NON_ANSI
+         offsetCorr = -fgtimezone;
+       }
+
+      //offtime (timer, tp->tm_gmtoff - leap_correction, tp);
+      offtime (timer, offsetCorr - leap_correction, tp);
+      tp->tm_sec += leap_extra_secs;
+    }
+
+  //libc_lock_unlock (tzset_lock);
+
+  return tp;
+}
+
+
+
+/* the following stuff is adapted from the tzCode package */
+
+static size_t  longest;
+static char *  abbr (struct tm * tmp);
+
+void show(const char *zone, time_t t, int v)
+{
+       struct tm *     tmp;
+
+       (void) printf("%-*s  ", (int) longest, zone);
+       if (v)
+               (void) printf("%.24s UTC = ", asctime(gmtime(&t)));
+       tmp = fgLocaltime(&t, zone);
+       (void) printf("%.24s", asctime(tmp));
+       if (*abbr(tmp) != '\0')
+               (void) printf(" %s", abbr(tmp));
+       if (v) {
+               (void) printf(" isdst=%d", tmp->tm_isdst);
+#ifdef TM_GMTOFF
+               (void) printf(" gmtoff=%ld", tmp->TM_GMTOFF);
+#endif /* defined TM_GMTOFF */
+       }
+       (void) printf("\n");
+}
+
+static char *abbr(struct tm *tmp)
+{
+       register char * result;
+       static char     nada;
+
+       if (tmp->tm_isdst != 0 && tmp->tm_isdst != 1)
+               return &nada;
+       result = fgtzname[tmp->tm_isdst];
+       return (result == NULL) ? &nada : result;
+}
+
+
+
+/***********************************************************************/
+
+
+/* Interpret the TZ envariable.  */
+static void fgtzset_internal (int always, char* tz)
+{
+  time_t now;
+  time(&now);
+  static int is_initialized = 0;
+  //register const char *tz;
+  register size_t l;
+  char *tzbuf;
+  unsigned short int hh, mm, ss;
+  unsigned short int whichrule;
+
+  if (is_initialized && !always)
+    return;
+  is_initialized = 1;
+
+  /* Examine the TZ environment variable.  */
+  //tz = getenv ("TZ");
+  if (tz == NULL)
+    /* No user specification; use the site-wide default.  */
+    tz = TZDEFAULT;
+  else if (*tz == '\0')
+    /* User specified the empty string; use UTC explicitly.  */
+    tz = "Universal";
+
+  /* A leading colon means "implementation defined syntax".
+     We ignore the colon and always use the same algorithm:
+     try a data file, and if none exists parse the 1003.1 syntax.  */
+  if (tz && *tz == ':')
+    ++tz;
+
+  /* Check whether the value changes since the last run.  */
+  if (old_fgtz != NULL && tz != NULL && strcmp (tz, old_fgtz) == 0)
+    /* No change, simply return.  */
+    return;
+
+  fgtz_rules[0].name = NULL;
+  fgtz_rules[1].name = NULL;
+
+  /* Save the value of `tz'.  */
+  if (old_fgtz != NULL)
+    free (old_fgtz);
+  old_fgtz = tz ? strdup (tz) : NULL;
+
+  /* Try to read a data file.  */
+  fgtzfile_read (tz);
+  if (use_fgtzfile)
+    return;
+  // The default behaviour of the originale tzset_internal (int always, char* tz) 
+  // function is to set up a default timezone, in any casetz file_read() fails
+  // Currently this leads to problems, because it modidifies the system timezone
+  // and not the local aircraft timezone, contained in FlightGear. I could adapt 
+  // this in future versions of this code, but doubt whether this is what we really
+  // want. So right now, exit when timezone information reading failed. 
+  // Guess I'll change that to something like 12 * (FG_LON / 180.0)
+  // 
+  // For now, I'll leave it like this.
+  else
+  {
+    printf ("Timezone reading failed\n");
+    exit(1);
+  }
+  // this emacs "comment out" function is cool!
+//   // /* No data file found.  Default to UTC if nothing specified.  */
+// //   printf ("1. Current local time          = %24s", asctime(localtime(&now)));
+//   if (tz == NULL || *tz == '\0')
+//     {
+//       fgtz_rules[0].name = fgtz_rules[1].name = "UTC";
+//       fgtz_rules[0].type = fgtz_rules[1].type = fgtz_rule::J0;
+//       fgtz_rules[0].m = fgtz_rules[0].n = fgtz_rules[0].d = 0;
+//       fgtz_rules[1].m = fgtz_rules[1].n = fgtz_rules[1].d = 0;
+//       fgtz_rules[0].secs = fgtz_rules[1].secs = 0;
+//       fgtz_rules[0].offset = fgtz_rules[1].offset = 0L;
+//       fgtz_rules[0].change = fgtz_rules[1].change = (time_t) -1;
+//       fgtz_rules[0].computed_for = fgtz_rules[1].computed_for = 0;
+//       return;
+//     }
+
+//   /* Clear out old state and reset to unnamed UTC.  */
+//   //printf ("2. Current local time          = %24s", asctime(localtime(&now)));
+//   memset (fgtz_rules, 0, sizeof fgtz_rules);
+//   fgtz_rules[0].name = fgtz_rules[1].name = "";
+
+//   /* Get the standard timezone name.  */
+//   tzbuf = malloc (strlen (tz) + 1);
+//   if (! tzbuf)
+//     {
+//       /* Clear the old tz name so we will try again.  */
+//       free (old_fgtz);
+//       old_fgtz = NULL;
+//       return;
+//     }
+//   //printf ("3. Current local time          = %24s", asctime(localtime(&now)));
+//   if (sscanf (tz, "%[^0-9,+-]", tzbuf) != 1 ||
+//       (l = strlen (tzbuf)) < 3)
+//     {
+//       free (tzbuf);
+//       return;
+//     }
+
+//   fgtz_rules[0].name = tzstring (tzbuf);
+
+//   tz += l;
+//   //printf ("4. Current local time          = %24s", asctime(localtime(&now)));
+//   /* Figure out the standard offset from UTC.  */
+//   if (*tz == '\0' || (*tz != '+' && *tz != '-' && !isdigit (*tz)))
+//     {
+//       free (tzbuf);
+//       return;
+//     }
+//   //printf ("5. Current local time          = %24s", asctime(localtime(&now)));
+//   if (*tz == '-' || *tz == '+')
+//     fgtz_rules[0].offset = *tz++ == '-' ? 1L : -1L;
+//   else
+//     fgtz_rules[0].offset = -1L;
+//   switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
+//     {
+//     default:
+//       free (tzbuf);
+//       return;
+//     case 1:
+//       mm = 0;
+//     case 2:
+//       ss = 0;
+//     case 3:
+//       break;
+//     }
+//     //printf ("6. Current local time          = %24s", asctime(localtime(&now)));
+//   fgtz_rules[0].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
+//                      (min (hh, 23) * 60 * 60));
+
+//   for (l = 0; l < 3; ++l)
+//     {
+//       while (isdigit(*tz))
+//     ++tz;
+//       if (l < 2 && *tz == ':')
+//     ++tz;
+//     }
+//   //printf ("7. Current local time          = %24s", asctime(localtime(&now)));
+//   /* Get the DST timezone name (if any).  */
+//   if (*tz != '\0')
+//     {
+//       char *n = tzbuf + strlen (tzbuf) + 1;
+//       if (sscanf (tz, "%[^0-9,+-]", n) != 1 ||
+//       (l = strlen (n)) < 3)
+//     goto done_names;        /* Punt on name, set up the offsets.  */
+//   //printf ("7.1 Current local time          = %24s", asctime(localtime(&now)));
+//       fgtz_rules[1].name = tzstring (n);
+
+//       tz += l;
+
+//       /* Figure out the DST offset from GMT.  */
+//       if (*tz == '-' || *tz == '+')
+//     fgtz_rules[1].offset = *tz++ == '-' ? 1L : -1L;
+//       else
+//     fgtz_rules[1].offset = -1L;
+//   //printf ("7.2 Current local time          = %24s", asctime(localtime(&now)));
+//       switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
+//     {
+//     default:
+//       /* Default to one hour later than standard time.  */
+//       fgtz_rules[1].offset = fgtz_rules[0].offset + (60 * 60);
+//       break;
+
+//     case 1:
+//       mm = 0;
+//     case 2:
+//       ss = 0;
+//     case 3:
+//       fgtz_rules[1].offset *= (min (ss, 59) + (min (mm, 59) * 60) +
+//                              (min (hh, 23) * (60 * 60)));
+//       break;
+//     }
+//   //printf ("7.3 Current local time          = %24s", asctime(localtime(&now)));
+//       for (l = 0; l < 3; ++l)
+//     {
+//       while (isdigit (*tz))
+//         ++tz;
+//       if (l < 2 && *tz == ':')
+//         ++tz;
+//     }
+//   //printf ("7.4 Current local time          = %24s", asctime(localtime(&now)));
+//       if (*tz == '\0' || (tz[0] == ',' && tz[1] == '\0'))
+//     {
+//       /* There is no rule.  See if there is a default rule file.  */
+//   //printf ("7.4.1 Current local time          = %24s", asctime(localtime(&now)));
+//       tzfile_default (fgtz_rules[0].name, fgtz_rules[1].name,
+//                         fgtz_rules[0].offset, fgtz_rules[1].offset);
+//   //printf ("7.4.2 Current local time          = %24s", asctime(localtime(&now)));
+//       if (use_fgtzfile)
+//         {
+//           free (old_fgtz);
+//           old_fgtz = NULL;
+//           free (tzbuf);
+//           return;
+//         }
+//     }
+//     }
+//   else
+//     {
+//       /* There is no DST.  */
+//       fgtz_rules[1].name = fgtz_rules[0].name;
+//       free (tzbuf);
+//       return;
+//     }
+//   //printf ("7.5 Current local time          = %24s", asctime(localtime(&now)));
+//  done_names:
+//   //printf ("8. Current local time          = %24s", asctime(localtime(&now)));
+//   free (tzbuf);
+
+//   /* Figure out the standard <-> DST rules.  */
+//   for (whichrule = 0; whichrule < 2; ++whichrule)
+//     {
+//       register fgtz_rule *tzr = &fgtz_rules[whichrule];
+
+//       /* Ignore comma to support string following the incorrect
+//      specification in early POSIX.1 printings.  */
+//       tz += *tz == ',';
+
+//       /* Get the date of the change.  */
+//       if (*tz == 'J' || isdigit (*tz))
+//     {
+//       char *end;
+//       tzr->type = *tz == 'J' ? fgtz_rule::J1 : fgtz_rule::J0;
+//       if (tzr->type == fgtz_rule::J1 && !isdigit (*++tz))
+//         return;
+//       tzr->d = (unsigned short int) strtoul (tz, &end, 10);
+//       if (end == tz || tzr->d > 365)
+//         return;
+//       else if (tzr->type == fgtz_rule::J1 && tzr->d == 0)
+//         return;
+//       tz = end;
+//     }
+//       else if (*tz == 'M')
+//     {
+//       int n;
+//       tzr->type = fgtz_rule::M;
+//       if (sscanf (tz, "M%hu.%hu.%hu%n",
+//                   &tzr->m, &tzr->n, &tzr->d, &n) != 3 ||
+//           tzr->m < 1 || tzr->m > 12 ||
+//           tzr->n < 1 || tzr->n > 5 || tzr->d > 6)
+//         return;
+//       tz += n;
+//     }
+//       else if (*tz == '\0')
+//     {
+//       /* United States Federal Law, the equivalent of "M4.1.0,M10.5.0".  */
+//       tzr->type = fgtz_rule::M;
+//       if (tzr == &fgtz_rules[0])
+//         {
+//           tzr->m = 4;
+//           tzr->n = 1;
+//           tzr->d = 0;
+//         }
+//       else
+//         {
+//           tzr->m = 10;
+//           tzr->n = 5;
+//           tzr->d = 0;
+//         }
+//     }
+//       else
+//     return;
+//       //printf ("9. Current local time          = %24s", asctime(localtime(&now)));
+//       if (*tz != '\0' && *tz != '/' && *tz != ',')
+//     return;
+//       else if (*tz == '/')
+//     {
+//       /* Get the time of day of the change.  */
+//       ++tz;
+//       if (*tz == '\0')
+//         return;
+//       switch (sscanf (tz, "%hu:%hu:%hu", &hh, &mm, &ss))
+//         {
+//         default:
+//           hh = 2;           /* Default to 2:00 AM.  */
+//         case 1:
+//           mm = 0;
+//         case 2:
+//           ss = 0;
+//         case 3:
+//           break;
+//         }
+//       for (l = 0; l < 3; ++l)
+//         {
+//           while (isdigit (*tz))
+//             ++tz;
+//           if (l < 2 && *tz == ':')
+//             ++tz;
+//         }
+//       tzr->secs = (hh * 60 * 60) + (mm * 60) + ss;
+//     }
+//       else
+//     /* Default to 2:00 AM.  */
+//     tzr->secs = 2 * 60 * 60;
+
+//       tzr->computed_for = -1;
+//     }
+// //   printf ("10. Current local time          = %24s", asctime(localtime(&now)));
+// 
+}
+
+/************************************************************************/
+
+
+/* Figure out the correct timezone for *TIMER and TM (which must be the same)
+   and set `tzname', `timezone', and `daylight' accordingly.
+   Return nonzero on success, zero on failure.  */
+size_t fgtzname_cur_max;
+
+static int fgtz_compute (time_t timer, const struct tm* tm)
+  //     time_t timer;
+  // const struct tm *tm;
+{
+  if (! fgcompute_change (&fgtz_rules[0], 1900 + tm->tm_year) ||
+      ! fgcompute_change (&fgtz_rules[1], 1900 + tm->tm_year))
+    return 0;
+
+  fgdaylight = timer >= fgtz_rules[0].change && timer < fgtz_rules[1].change;
+  fgtimezone = -fgtz_rules[fgdaylight].offset;
+  fgtzname[0] = (char *) fgtz_rules[0].name;
+  fgtzname[1] = (char *) fgtz_rules[1].name;
+
+  {
+    /* Keep tzname_cur_max up to date.  */
+    size_t len0 = strlen (fgtzname[0]);
+    size_t len1 = strlen (fgtzname[1]);
+    if (len0 > fgtzname_cur_max)
+      fgtzname_cur_max = len0;
+    if (len1 > fgtzname_cur_max)
+      fgtzname_cur_max = len1;
+  }
+
+  return 1;
+}
+
+/**********************************************************************/
+
+/* Figure out the exact time (as a time_t) in YEAR
+   when the change described by RULE will occur and
+   put it in RULE->change, saving YEAR in RULE->computed_for.
+   Return nonzero if successful, zero on failure.  */
+static int fgcompute_change (fgtz_rule *rule, int year)
+  //     tz_rule *rule;
+  // int year;
+{
+  register time_t t;
+  int y;
+
+  if (year != -1 && rule->computed_for == year)
+    /* Operations on times in 1969 will be slower.  Oh well.  */
+    return 1;
+
+  /* First set T to January 1st, 0:00:00 GMT in YEAR.  */
+  t = 0;
+  for (y = 1970; y < year; ++y)
+    t += SECSPERDAY * (isleap (y) ? 366 : 365);
+
+  switch (rule->type)
+    {
+    case fgtz_rule::J1:
+      /* Jn - Julian day, 1 == January 1, 60 == March 1 even in leap years.
+        In non-leap years, or if the day number is 59 or less, just
+        add SECSPERDAY times the day number-1 to the time of
+        January 1, midnight, to get the day.  */
+      t += (rule->d - 1) * SECSPERDAY;
+      if (rule->d >= 60 && isleap (year))
+       t += SECSPERDAY;
+      break;
+
+    case fgtz_rule::J0:
+      /* n - Day of year.
+        Just add SECSPERDAY times the day number to the time of Jan 1st.  */
+      t += rule->d * SECSPERDAY;
+      break;
+
+    case fgtz_rule::M:
+      /* Mm.n.d - Nth "Dth day" of month M.  */
+      {
+       register int i, d, m1, yy0, yy1, yy2, dow;
+       register const unsigned short int *myday =
+         &mon_yday[isleap (year)][rule->m];
+
+       /* First add SECSPERDAY for each day in months before M.  */
+       t += myday[-1] * SECSPERDAY;
+
+       /* Use Zeller's Congruence to get day-of-week of first day of month. */
+       m1 = (rule->m + 9) % 12 + 1;
+       yy0 = (rule->m <= 2) ? (year - 1) : year;
+       yy1 = yy0 / 100;
+       yy2 = yy0 % 100;
+       dow = ((26 * m1 - 2) / 10 + 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
+       if (dow < 0)
+         dow += 7;
+
+       /* DOW is the day-of-week of the first day of the month.  Get the
+          day-of-month (zero-origin) of the first DOW day of the month.  */
+       d = rule->d - dow;
+       if (d < 0)
+         d += 7;
+       for (i = 1; i < rule->n; ++i)
+         {
+           if (d + 7 >= myday[0] - myday[-1])
+             break;
+           d += 7;
+         }
+
+       /* D is the day-of-month (zero-origin) of the day we want.  */
+       t += d * SECSPERDAY;
+      }
+      break;
+    }
+
+  /* T is now the Epoch-relative time of 0:00:00 GMT on the day we want.
+     Just add the time of day and local offset from GMT, and we're done.  */
+
+  rule->change = t - rule->offset + rule->secs;
+  rule->computed_for = year;
+  return 1;
+}
+
+/*************************************************************************/
+
+int fgtzfile_compute (time_t timer, int use_localtime,
+                 long int *leap_correct, int *leap_hit)
+{
+  register size_t i;
+
+  if (use_localtime)
+    {
+      struct ttinfo *info = fgfind_transition (timer);
+      fgdaylight = info->isdst;
+      fgtimezone = -info->offset;
+      for (i = 0;
+          i < num_types && i < sizeof (fgtzname) / sizeof (fgtzname[0]);
+          ++i)
+       fgtzname[types[i].isdst] = &zone_names[types[i].idx];
+      if (info->isdst < sizeof (fgtzname) / sizeof (fgtzname[0]))
+       fgtzname[info->isdst] = &zone_names[info->idx];
+    }
+
+  *leap_correct = 0L;
+  *leap_hit = 0;
+
+  /* Find the last leap second correction transition time before TIMER.  */
+  i = num_leaps;
+  do
+    if (i-- == 0)
+      return 1;
+  while (timer < leaps[i].transition);
+
+  /* Apply its correction.  */
+  *leap_correct = leaps[i].change;
+
+  if (timer == leaps[i].transition && /* Exactly at the transition time.  */
+      ((i == 0 && leaps[i].change > 0) ||
+       leaps[i].change > leaps[i - 1].change))
+    {
+      *leap_hit = 1;
+      while (i > 0 &&
+            leaps[i].transition == leaps[i - 1].transition + 1 &&
+            leaps[i].change == leaps[i - 1].change + 1)
+       {
+         ++*leap_hit;
+         --i;
+       }
+    }
+
+  return 1;
+}
+
+/**************************************************************************/
+
+static struct ttinfo * fgfind_transition (time_t timer)
+{
+  size_t i;
+
+  if (num_transitions == 0 || timer < transitions[0])
+    {
+      /* TIMER is before any transition (or there are no transitions).
+        Choose the first non-DST type
+        (or the first if they're all DST types).  */
+      i = 0;
+      while (i < num_types && types[i].isdst)
+       ++i;
+      if (i == num_types)
+       i = 0;
+    }
+  else
+    {
+      /* Find the first transition after TIMER, and
+        then pick the type of the transition before it.  */
+      for (i = 1; i < num_transitions; ++i)
+       if (timer < transitions[i])
+         break;
+      i = type_idxs[i - 1];
+    }
+
+  return &types[i];
+}
+
+
+/**************************************************************************/
+void fgtzfile_read (const char *file)
+{
+  static const char default_tzdir[] = TZDIR;
+  size_t num_isstd, num_isgmt;
+  register FILE *f;
+  struct tzhead tzhead;
+  size_t chars;
+  register size_t i;
+  struct ttinfo *info;
+
+  use_fgtzfile = 0;
+
+  if (transitions != NULL)
+    free ((void *) transitions);
+  transitions = NULL;
+  if (type_idxs != NULL)
+    free ((void *) type_idxs);
+  type_idxs = NULL;
+  if (types != NULL)
+    free ((void *) types);
+  types = NULL;
+  if (zone_names != NULL)
+    free ((void *) zone_names);
+  zone_names = NULL;
+  if (leaps != NULL)
+    free ((void *) leaps);
+  leaps = NULL;
+
+  if (file == NULL)
+    /* No user specification; use the site-wide default.  */
+    file = TZDEFAULT;
+  else if (*file == '\0')
+    /* User specified the empty string; use UTC with no leap seconds.  */
+    return;
+  else
+    {
+      /* We must not allow to read an arbitrary file in a setuid
+        program.  So we fail for any file which is not in the
+        directory hierachy starting at TZDIR
+        and which is not the system wide default TZDEFAULT.  */
+      //if (libc_enable_secure
+      //  && ((*file == '/'
+      //       && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
+      //       && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
+      //      || strstr (file, "../") != NULL))
+       /* This test is certainly a bit too restrictive but it should
+          catch all critical cases.  */
+      //return;
+    }
+
+//   if (*file != '/') // if a relative path is used, append what file points to
+//                     // to the path indicated by TZDIR.
+//     {
+//       const char *tzdir;
+//       unsigned int len, tzdir_len;
+//       char *_new;
+
+//       tzdir = getenv ("TZDIR");
+//       if (tzdir == NULL || *tzdir == '\0')
+//     {
+//       tzdir = default_tzdir;
+//       tzdir_len = sizeof (default_tzdir) - 1;
+//     }
+//       else
+//     tzdir_len = strlen (tzdir);
+//       len = strlen (file) + 1;
+//       _new = (char *) alloca (tzdir_len + 1 + len);
+//       memcpy (_new, tzdir, tzdir_len);
+//       _new[tzdir_len] = '/';
+//       memcpy (&_new[tzdir_len + 1], file, len);
+//       file = _new;
+//     }
+
+  f = fopen (file, "rb");
+  if (f == NULL)
+    return;
+
+  if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
+    goto lose;
+
+  num_transitions = (size_t) decode (tzhead.tzh_timecnt);
+  num_types = (size_t) decode (tzhead.tzh_typecnt);
+  chars = (size_t) decode (tzhead.tzh_charcnt);
+  num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
+  num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
+  num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
+
+  if (num_transitions > 0)
+    {
+      transitions = (time_t *) malloc (num_transitions * sizeof(time_t));
+      if (transitions == NULL)
+       goto lose;
+      type_idxs = (unsigned char *) malloc (num_transitions);
+      if (type_idxs == NULL)
+       goto lose;
+    }
+  if (num_types > 0)
+    {
+      types = (struct ttinfo *) malloc (num_types * sizeof (struct ttinfo));
+      if (types == NULL)
+       goto lose;
+    }
+  if (chars > 0)
+    {
+      zone_names = (char *) malloc (chars);
+      if (zone_names == NULL)
+       goto lose;
+    }
+  if (num_leaps > 0)
+    {
+      leaps = (struct leap *) malloc (num_leaps * sizeof (struct leap));
+      if (leaps == NULL)
+       goto lose;
+    }
+
+  if (sizeof (time_t) < 4)
+      abort ();
+
+  if (fread(transitions, 4, num_transitions, f) != num_transitions ||
+      fread(type_idxs, 1, num_transitions, f) != num_transitions)
+    goto lose;
+
+  /* Check for bogus indices in the data file, so we can hereafter
+     safely use type_idxs[T] as indices into `types' and never crash.  */
+  for (i = 0; i < num_transitions; ++i)
+    if (type_idxs[i] >= num_types)
+      goto lose;
+
+  if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
+    {
+      /* Decode the transition times, stored as 4-byte integers in
+        network (big-endian) byte order.  We work from the end of
+        the array so as not to clobber the next element to be
+        processed when sizeof (time_t) > 4.  */
+      i = num_transitions;
+      while (i-- > 0)
+       transitions[i] = decode ((char *) transitions + i*4);
+    }
+
+  for (i = 0; i < num_types; ++i)
+    {
+      unsigned char x[4];
+      if (fread (x, 1, 4, f) != 4 ||
+         fread (&types[i].isdst, 1, 1, f) != 1 ||
+         fread (&types[i].idx, 1, 1, f) != 1)
+       goto lose;
+      if (types[i].idx >= chars) /* Bogus index in data file.  */
+       goto lose;
+      types[i].offset = (long int) decode (x);
+    }
+
+  if (fread (zone_names, 1, chars, f) != chars)
+    goto lose;
+
+  for (i = 0; i < num_leaps; ++i)
+    {
+      unsigned char x[4];
+      if (fread (x, 1, sizeof (x), f) != sizeof (x))
+       goto lose;
+      leaps[i].transition = (time_t) decode (x);
+      if (fread (x, 1, sizeof (x), f) != sizeof (x))
+       goto lose;
+      leaps[i].change = (long int) decode (x);
+    }
+
+  for (i = 0; i < num_isstd; ++i)
+    {
+      int c = getc (f);
+      if (c == EOF)
+       goto lose;
+      types[i].isstd = c != 0;
+    }
+  while (i < num_types)
+    types[i++].isstd = 0;
+
+  for (i = 0; i < num_isgmt; ++i)
+    {
+      int c = getc (f);
+      if (c == EOF)
+       goto lose;
+      types[i].isgmt = c != 0;
+    }
+  while (i < num_types)
+    types[i++].isgmt = 0;
+
+  fclose (f);
+
+  info = fgfind_transition (0);
+  for (i = 0; i < num_types && i < sizeof (fgtzname) / sizeof (fgtzname[0]);
+       ++i)
+    fgtzname[types[i].isdst] = tzstring (&zone_names[types[i].idx]);
+  if (info->isdst < sizeof (fgtzname) / sizeof (fgtzname[0]))
+    fgtzname[info->isdst] = tzstring (&zone_names[info->idx]);
+
+  fgcompute_tzname_max (chars);
+
+  use_fgtzfile = 1;
+  return;
+
+ lose:;
+  fclose(f);
+}
+
+/****************************************************************************/
+static void fgcompute_tzname_max (size_t chars)
+{
+  extern size_t tzname_cur_max; /* Defined in tzset.c. */
+
+  const char *p;
+
+  p = zone_names;
+  do
+    {
+      const char *start = p;
+      while (*p != '\0')
+       ++p;
+      if ((size_t) (p - start) > fgtzname_cur_max)
+       fgtzname_cur_max = p - start;
+    } while (++p < &zone_names[chars]);
+}
+
+/**************************************************************************/
+
+//#include <endian.h>
+
+/* Decode the four bytes at PTR as a signed integer in network byte order.  */
+static inline int decode (const void *ptr)
+{
+  if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
+    return *(const int *) ptr;
+  else
+    {
+      const unsigned char *p = (unsigned char *)ptr;
+      int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
+
+      result = (result << 8) | *p++;
+      result = (result << 8) | *p++;
+      result = (result << 8) | *p++;
+      result = (result << 8) | *p++;
+
+      return result;
+    }
+}
+
+
+
+#define        SECS_PER_HOUR   (60 * 60)
+#define        SECS_PER_DAY    (SECS_PER_HOUR * 24)
+
+/* Compute the `struct tm' representation of *T,
+   offset OFFSET seconds east of UTC,
+   and store year, yday, mon, mday, wday, hour, min, sec into *TP.  */
+
+void offtime (const time_t *t, long int offset, struct tm *tp)
+  //    const time_t *t;
+  // long int offset;
+  // struct tm *tp;
+{
+  register long int days, rem, y;
+  register const unsigned short int *ip;
+
+  days = *t / SECS_PER_DAY;
+  rem = *t % SECS_PER_DAY;
+  rem += offset;
+  while (rem < 0)
+    {
+      rem += SECS_PER_DAY;
+      --days;
+    }
+  while (rem >= SECS_PER_DAY)
+    {
+      rem -= SECS_PER_DAY;
+      ++days;
+    }
+  tp->tm_hour = rem / SECS_PER_HOUR;
+  rem %= SECS_PER_HOUR;
+  tp->tm_min = rem / 60;
+  tp->tm_sec = rem % 60;
+  /* January 1, 1970 was a Thursday.  */
+  tp->tm_wday = (4 + days) % 7;
+  if (tp->tm_wday < 0)
+    tp->tm_wday += 7;
+  y = 1970;
+
+#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
+
+  while (days < 0 || days >= (isleap (y) ? 366 : 365))
+    {
+      /* Guess a corrected year, assuming 365 days per year.  */
+      long int yg = y + days / 365 - (days % 365 < 0);
+
+      /* Adjust DAYS and Y to match the guessed year.  */
+      days -= ((yg - y) * 365
+              + LEAPS_THRU_END_OF (yg - 1)
+              - LEAPS_THRU_END_OF (y - 1));
+      y = yg;
+    }
+  tp->tm_year = y - 1900;
+  tp->tm_yday = days;
+  ip = mon_yday[isleap(y)];
+  for (y = 11; days < ip[y]; --y)
+    continue;
+  days -= ip[y];
+  tp->tm_mon = y;
+  tp->tm_mday = days + 1;
+}
+
+/* Allocate a time zone string with given contents.
+   The string will never be moved or deallocated.
+   However, its contents may be shared with other such strings.  */
+char *tzstring (const char* string)
+  //const char *string;
+{
+  struct tzstring_head *h = &tzstring_list.head;
+  size_t needed;
+  char *p;
+
+  /* Look through time zone string list for a duplicate of this one.  */
+  for (h = &tzstring_list.head;  ;  h = h->next)
+    {
+      for (p = (char *) (h + 1);  p[0] | p[1];  ++p)
+       if (strcmp (p, string) == 0)
+         return p;
+      if (! h->next)
+       break;
+    }
+
+  /* No duplicate was found.  Copy to the end of this buffer if there's room;
+     otherwise, append a large-enough new buffer to the list and use it.  */
+  ++p;
+  needed = strlen (string) + 2; /* Need 2 trailing '\0's after last string.  */
+
+  if ((size_t) ((char *) (h + 1) + tzstring_last_buffer_size - p) < needed)
+    {
+      size_t buffer_size = tzstring_last_buffer_size;
+      while ((buffer_size *= 2) < needed)
+       continue;
+      if (! (h = h->next = (struct tzstring_head *)malloc (sizeof *h + buffer_size)))
+       return NULL;
+      h->next = NULL;
+      tzstring_last_buffer_size = buffer_size;
+      p = (char *) (h + 1);
+    }
+
+  return strncpy (p, string, needed);
+}
diff --git a/src/Time/lowleveltime.h b/src/Time/lowleveltime.h
new file mode 100644 (file)
index 0000000..f2d4876
--- /dev/null
@@ -0,0 +1,131 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * lowleveltime.h
+ * Written by various people (I"ll look up the exact credits later)
+ * Modified by Durk Talsma, July 1999 for use in FlightGear
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/********************************************************************
+ * This file redefines some low-level Unix-like time functions for  *
+ * use with FlightGear. Most notably, localtime() is adapted to use *
+ * a custom timezone, in order to get the 'local' time for a given  *
+ * aircraft's position, and not only for the current location of the*
+ * computer running the sim.                                        *
+ *                                                                  *
+ * Software adapted from glibc functions, by Durk Talsma. Started   *
+ * July, 17, 1999.                                                  *
+ ********************************************************************/
+
+#ifndef _LOWLEVELTIME_H_
+#define _LOWLEVELTIME_H_
+
+#include <time.h>
+
+/* adapted from zdump.c */
+void show (const char *zone, time_t t, int v);
+
+/* adapted from <time.h> */
+struct tm * fgLocaltime (const time_t *t, char *tzName);
+
+/* Prototype for the internal function to get information based on TZ.  */
+extern struct tm *fgtz_convert (const time_t *t, int use_localtime,
+                                    struct tm *tp, char *tzName);
+
+/* This structure contains all the information about a
+   timezone given in the POSIX standard TZ envariable.  */
+typedef struct
+  {
+    const char *name;
+
+    /* When to change.  */
+    enum { J0, J1, M } type;   /* Interpretation of:  */
+    unsigned short int m, n, d;        /* Month, week, day.  */
+    unsigned int secs;         /* Time of day.  */
+
+    long int offset;           /* Seconds east of GMT (west if < 0).  */
+
+    /* We cache the computed time of change for a
+       given year so we don't have to recompute it.  */
+    time_t change;     /* When to change to this zone.  */
+    int computed_for;  /* Year above is computed for.  */
+  } fgtz_rule;
+
+/* tz_rules[0] is standard, tz_rules[1] is daylight.  */
+static fgtz_rule fgtz_rules[2];
+
+struct tzhead {
+       char    tzh_magic[4];           /* TZ_MAGIC */
+       char    tzh_reserved[16];       /* reserved for future use */
+       char    tzh_ttisgmtcnt[4];      /* coded number of trans. time flags */
+       char    tzh_ttisstdcnt[4];      /* coded number of trans. time flags */
+       char    tzh_leapcnt[4];         /* coded number of leap seconds */
+       char    tzh_timecnt[4];         /* coded number of transition times */
+       char    tzh_typecnt[4];         /* coded number of local time types */
+       char    tzh_charcnt[4];         /* coded number of abbr. chars */
+};
+
+
+/* Defined in mktime.c.  */
+extern const unsigned short int mon_yday[2][13];
+
+#ifndef TZDIR
+#define TZDIR  "/usr/local/etc/zoneinfo" /* Time zone object file directory */
+#endif /* !defined TZDIR */
+
+
+
+#ifndef TZDEFAULT
+#define TZDEFAULT      "localtime"
+#endif /* !defined TZDEFAULT */
+
+#define SECSPERMIN     60
+#define MINSPERHOUR    60
+#define HOURSPERDAY    24
+#define DAYSPERWEEK    7
+#define DAYSPERNYEAR   365
+#define DAYSPERLYEAR   366
+#define SECSPERHOUR    (SECSPERMIN * MINSPERHOUR)
+#define SECSPERDAY     ((long) SECSPERHOUR * HOURSPERDAY)
+#define MONSPERYEAR    12
+
+#define TM_SUNDAY      0
+#define TM_MONDAY      1
+#define TM_TUESDAY     2
+#define TM_WEDNESDAY   3
+#define TM_THURSDAY    4
+#define TM_FRIDAY      5
+#define TM_SATURDAY    6
+
+#define TM_JANUARY     0
+#define TM_FEBRUARY    1
+#define TM_MARCH       2
+#define TM_APRIL       3
+#define TM_MAY         4
+#define TM_JUNE                5
+#define TM_JULY                6
+#define TM_AUGUST      7
+#define TM_SEPTEMBER   8
+#define TM_OCTOBER     9
+#define TM_NOVEMBER    10
+#define TM_DECEMBER    11
+
+#define TM_YEAR_BASE   1900
+
+#define EPOCH_YEAR     1970
+#define EPOCH_WDAY     TM_THURSDAY
+
+#endif 
diff --git a/src/Time/mymath.cxx b/src/Time/mymath.cxx
new file mode 100755 (executable)
index 0000000..5f288d0
--- /dev/null
@@ -0,0 +1,371 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * mymath.cc
+ * Written by Durk Talsma, around 1995/1996.
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/********************************************************************
+ * This file defines a simple Vector and Matrix library. These were 
+ * originally written for my (yet) unpublished planetarium / solar 
+ * system simulator program. The are included here, because I have 
+ * experience the code to calculate angles between vectors. I'm sure 
+ * similar functions exist already somewhere else so I don't mind 
+ * whether this gets eventually replaced by something more suitable
+ * The functions are based on a description in Tyler. A. (1994). C++
+ * Real-time 3D graphics. Sigma press, Wilmslow, England.
+ * 
+ * The original versions were written under windows, hence the occasional 
+ *::MessageBox() statements between conditional compile statements
+ *
+ ********************************************************************/
+
+
+
+#include "mymath.h"
+
+const double PiOver180 = 1.74532925199433E-002;
+const double Pix4dif3  = 4.1887902;
+
+Vector::Vector(int size)
+{
+       build(size);
+    for (int i = 0; i < nrData; i++)
+       data[i] = 0.00;
+}
+
+double Vector::VecLen()
+{
+
+   double length = 0;
+   for (int i= 0; i < nrData; i++)
+               length += powl(data[i],2);
+   return sqrtl(length);
+}
+
+
+double VecDot(Vector first, Vector second)
+{
+       /*double
+       result = ((first.xVal*second.xVal) +
+                                 (first.yVal*second.yVal) +
+                         (first.zVal*second.zVal));
+    return result; */
+
+    double result = 0;
+    for (int i = 0; i < first.nrData; i++)
+       result += first.data[i] * second.data[i];
+
+    return result;
+}
+
+Vector VecCross(Vector first, Vector second)
+{
+
+       /*return Vec3 ( (first.yVal*second.zVal - first.zVal*second.yVal),
+                                 (first.zVal*second.xVal - first.zVal*second.xVal),
+                      (first.xVal*second.yVal - first.yVal*second.xVal) );
+               */
+        #ifdef DEBUG
+        if ( (first.nrData != 4) || (first.nrData != second.nrData) )
+        {
+               ::MessageBox(0, "Attempting to calculate Cross product with 2\n"
+                         "unequally sized vectors in\n"
+                         "Vector VecCross(Vector, Vector", "Error",
+                         MB_OK);
+            exit(1);
+        }
+        #endif
+       double x = first.data[1] * second.data[2] - first.data[2]*second.data[1];
+        double y = first.data[2] * second.data[0] - first.data[0]*second.data[2];
+        double z = first.data[0] * second.data[1] - first.data[1]*second.data[0];
+       return Vector(x,y,z);
+}
+
+
+
+
+Vector operator- (Vector first, Vector second)
+{
+       /*return Vec3( first.xVal - second.xVal,
+                        first.yVal - second.yVal,
+                 first.zVal - second.zVal );
+    */
+           #ifdef DEBUG
+        if ( first.nrData != second.nrData )
+        {
+               ::MessageBox(0, "Attempting to subtract 2 \n"
+                         "unequally sized vectors in\n"
+                         "Vector operator-(Vector, Vector", "Error",
+                         MB_OK);
+            //exit(1);
+            return Vector(0);
+        }
+        #endif
+        double *temp = new double[first.nrData];
+        for (int i = 0; i < first.nrData; i++)
+               temp[i] = first.data[i] - second.data[i];
+        Vector result(first.nrData, temp);
+        delete [] temp;
+        return result;
+}
+
+
+Vector operator+ (Vector first, Vector second)
+{
+       /*return Vec3( first.xVal + second.xVal,
+                        first.yVal + second.yVal,
+                 first.zVal + second.zVal );
+    */
+    #ifdef DEBUG
+    if ( first.nrData != second.nrData )
+       {
+       ::MessageBox(0, "Attempting to add 2\n"
+                         "unequally sized vectors in\n"
+                         "Vector operator+(Vector, Vector", "Error",
+                         MB_OK);
+        exit(1);
+    }
+    #endif
+    double *temp = new double[first.nrData];
+    for (int i = 0; i < first.nrData; i++)
+       temp[i] = first.data[i] + second.data[i];
+    Vector result(first.nrData, temp);
+    delete [] temp;
+    return result;
+}
+
+Vector Vector::operator +=(Vector other)
+{
+       #ifdef DEBUG
+    if ( first.nrData != second.nrData )
+    {
+       ::MessageBox(0, "Attempting to add 2\n"
+                         "unequally sized vectors in\n"
+                         "Vector operator+(Vector, Vector", "Error",
+                         MB_OK);
+        exit(1);
+    }
+    #endif
+    for (int i = 0; i < nrData; i++)
+       data[i] += other.data[i];
+    return *this;
+}
+
+Vector Vector::operator -=(Vector other)
+{
+       #ifdef DEBUG
+    if ( first.nrData != second.nrData )
+    {
+       ::MessageBox(0, "Attempting to add 2\n"
+                         "unequally sized vectors in\n"
+                         "Vector operator+(Vector, Vector", "Error",
+                         MB_OK);
+        exit(1);
+    }
+    #endif
+    for (int i = 0; i < nrData; i++)
+       data[i] -= other.data[i];
+    return *this;
+}
+
+
+
+
+
+Vector operator* (Matrix mx, Vector vc)
+{
+       int sizes[3];
+    sizes[0] = vc.nrData;
+    sizes[1] = mx.rows;
+    sizes[2] = mx.columns;
+
+    #ifdef DEBUG
+    if ( (sizes[0] != sizes[1]) || (sizes[0] != sizes[2]) )
+    {
+        char buffer[50];
+        sprintf(buffer, "Sizes don't match in function\n"
+                                       "Vector operator*(Matrix, Vector)\n"
+                        "sizes are: %d, %d, %d", sizes[0], sizes[1],sizes[2]);
+       MessageBox(0, buffer, "Error", MB_OK | MB_ICONEXCLAMATION);
+        exit(1);
+    }
+    #endif
+    double* result = new double[sizes[0]];
+    int col, row;
+
+    for (col = 0; col < sizes[0]; col++)
+    {
+       result[col] = 0;
+        for (row = 0; row < sizes[0]; row++)
+               result[col] += vc[row] * mx[row][col];
+    }
+    Vector res(4, result);
+
+   /*
+    #ifdef DEBUG
+    char buffer[200];
+    sprintf(buffer, "return value of vector * matrix multiplication is\n"
+                               "(%f, %f, %f, %f) ", result[0], result[1], result[2], result[3]);
+    ::MessageBox(0, buffer, "Information", MB_OK);
+    #endif
+   */
+       delete [] result;
+    return res;
+}
+
+Vector operator*(Vector v1, Vector v2)
+{
+       int size1 = v1.nrData;
+
+    #ifdef DEBUG
+
+    int size2 = v2.nrData;
+    if(size1 != size2)
+    {
+       ::MessageBox(0, "Vector sizes don't match in Vector operator*(Vector, Vector)",
+                               "Error", MB_OK);
+        exit(1);
+    }
+    #endif
+    double *tempRes = new double[size1];
+    for (int i= 0; i < size1; i++)
+       tempRes[i] = v1[i] * v2[i];
+    Vector result(size1, tempRes);
+    delete tempRes;
+    return result;
+}
+
+
+Vector operator*(Vector vec, double d)
+{
+       double* tempRes = new double[vec.nrData];
+    for (int i = 0; i < vec.nrData; i++)
+       tempRes[i] = vec[i] * d;
+    Vector result(vec.nrData, tempRes);
+    delete tempRes;
+    return result;
+}
+
+Vector operator/(Vector vec, double d)
+{
+       double* tempRes = new double[vec.nrData];
+    for (int i = 0; i < vec.nrData; i++)
+       tempRes[i] = vec[i] / d;
+    Vector result(vec.nrData, tempRes);
+    delete tempRes;
+    return result;
+}
+
+ostream& operator << (ostream& os, Vector& vec)
+{
+  os << /*setw(4) << */vec.nrData << '\t';
+    for (int i = 0; i < vec.nrData; i++)
+      os /*<< setw(25) */<< vec[i] << '\t';
+    return os;
+}
+
+istream& operator >> (istream& is, Vector& vec)
+{
+       is >> vec.nrData;
+    if (vec.data)
+       delete [] vec.data;
+    vec.data = new double[vec.nrData];
+    for (int i = 0; i < vec.nrData; i++)
+       is >> vec.data[i];
+    return is;
+}
+
+ostream& Vector::binSave(ostream& os)
+{
+       os.write((char*) &nrData, sizeof(int));
+    os.write((char*) data, nrData* sizeof(double));
+       return os;
+}
+
+
+
+
+
+
+/******************************************************************************
+               Matrix manipulation routines
+******************************************************************************/
+
+Matrix::Matrix(int r, int c, double*dta)
+{
+       build(r,c);
+    for (int i = 0; i < rows; i++)
+       for (int j = 0; j < columns; j++)
+               data[i][j] = (*dta++);
+}
+
+Matrix::Matrix(int r, int c, double** dta)
+{
+       build(r,c);
+    for (int i = 0; i < rows; i++)
+       for (int j = 0; j < columns; j++)
+                       data[i][j] = dta[i][j];
+}
+
+Matrix::Matrix(int r, int c, Vector* dta)
+{
+       build(r,c);
+    for (int i = 0; i < rows; i++)
+       data[i] = dta[i];
+}
+
+Matrix::Matrix(Matrix& other)
+{
+       build (other.rows, other.columns);
+    for (int i = 0; i< rows; i++)
+       (*this)[i] = other[i];
+}
+
+void Matrix::build(int row, int col)
+{
+    rows = row;
+    columns = col;
+
+       data = new Vector [rows];
+    for (int i = 0; i < rows; i++)
+       data[i].build(col);
+}
+
+Matrix& Matrix::operator =(Matrix& other)
+{
+       rows = other.rows;
+    columns = other.columns;
+    for (int i = 0; i < rows; i++)
+       (*this)[i] = other[i];
+    return *this;
+}
+
+
+Vector Matrix::operator ()(int col)
+{
+       Vector Col(rows);
+    for (int i = 0; i < rows; i++)
+       Col[i] = data[i][col];
+    return Col;
+}
+
+void Matrix::norm(int scale)
+{
+       for (int i = 0; i < rows; i++)
+       for (int j = 0; j < columns; j++)
+               data[i][j] /= scale;
+}
diff --git a/src/Time/mymath.h b/src/Time/mymath.h
new file mode 100755 (executable)
index 0000000..8d1c5a9
--- /dev/null
@@ -0,0 +1,454 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * mymath.h
+ * Written by Durk Talsma, around 1995/1996.
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/********************************************************************
+ * This file defines a simple Vector and Matrix library. These were 
+ * originally written for my (yet) unpublished planetarium / solar 
+ * system simulator program. The are included here, because I have 
+ * experience the code to calculate angles between vectors. I'm sure 
+ * similar functions exist already somewhere else so I don't mind 
+ * whether this gets eventually replaced by something more suitable
+ * The functions are based on a description in Tyler. A. (1994). C++
+ * Real-time 3D graphics. Sigma press, Wilmslow, England.
+ * 
+ * The original versions were written under windows, hence the occasional 
+ *::MessageBox() statements between conditional compile statements
+ *
+ ********************************************************************/
+
+
+#ifndef _MY_MATH_H_
+#define _MY_MATH_H__
+#include <math.h>
+#include <fstream>
+#include <iomanip>
+
+using namespace std;
+
+#include <fg_constants.h>
+extern const  double PiOver180;
+extern const double Pix4dif3;
+
+
+
+class Matrix;
+
+class Vector
+{
+    private:
+        int nrData;
+       double* data;
+
+       public:
+       Vector();
+        Vector(int size);
+        Vector(int size, double* dta);
+        Vector(double x, double y, double z);
+       Vector(Vector& other);
+       Vector(istream& is);
+        ~Vector();
+
+       void SetVal(double x, double y, double z);
+
+        void build(int);
+       Vector* GetVector();
+       double GetX();
+       double GetY();
+       double GetZ();
+       void AddX(double x);
+        void AddY(double y);
+       void AddZ(double z);
+       void SubtractX(double x);
+       void SubtractY(double y);
+       void SubtractZ(double z);
+       double VecLen();
+        Vector& operator = (Vector&);
+        double& operator[] (int);
+        int getDim();
+       
+        ostream& binSave(ostream& os);
+       
+       Vector operator +=(Vector other);
+        Vector operator -=(Vector other);
+       
+       friend double VecDot(Vector first, Vector second);
+       friend Vector VecCross(Vector first, Vector second);
+       friend Vector operator-(Vector first, Vector second);
+       friend Vector operator+(Vector first, Vector second);
+       friend Vector operator*(Matrix mx, Vector vc);
+       friend Vector operator*(Vector v1, Vector v2);
+       friend Vector operator*(Vector vec, double d);
+       friend Vector operator/(Vector vec, double d);
+       
+       friend ostream& operator << (ostream& os, Vector& vec);
+       friend istream& operator >> (istream& is, Vector& vec);
+};
+
+/*-----------------------------------------------------------------------------
+                               nonmember friend functions
+------------------------------------------------------------------------------*/
+
+double VecDot(Vector first, Vector second);
+Vector VecCross(Vector first, Vector second);
+Vector operator-(Vector first, Vector second);
+Vector operator+(Vector first, Vector second);
+Vector operator*(Matrix mx, Vector vc);
+Vector operator*(Vector v1, Vector v2);
+Vector operator*(Vector vec, double d);
+Vector operator/(Vector vec, double d);
+
+ostream& operator << (ostream& os, Vector& vec);
+istream& operator >> (istream& is, Vector& vec);
+
+
+/*------------------------------------------------------------------------------
+                       inline member functions
+------------------------------------------------------------------------------*/
+
+inline Vector::Vector()
+{
+       nrData = 0;
+    data = 0;
+}
+
+inline void Vector::build(int size)
+{
+       nrData = size;
+    data = new double[nrData];
+    #ifdef DEBUG
+    if (!data)
+    {
+               ::MessageBox(0, "Error Allocating Memory for a new Vector", "Error",
+                               MB_OK);
+        exit(1);
+    }
+    #endif
+    //for (int i = 0; i < nrData; i++)
+    // data[i] = 0.00;
+}
+
+
+inline Vector::Vector(int size, double* dta)
+{
+       build(size);
+    memcpy(data, dta, nrData*sizeof(double));
+}
+
+
+inline Vector::Vector(Vector& other)
+{
+       build(other.nrData);
+    memcpy(data,other.data,nrData*sizeof(double));
+}
+
+inline Vector::Vector(double x, double y, double z)
+{
+    build(4);      // one extra for matrix multiplication...
+       data[0] = x;
+       data[1] = y;
+       data[2] = z;
+    data[3] = 0.00;
+
+}
+
+inline Vector::Vector(istream& is)
+{
+       is.read((char*) &nrData, sizeof(int));
+    data = new double[nrData];
+    is.read((char*) data, nrData * sizeof(double));
+}
+
+inline Vector::~Vector()
+{
+       delete [] data;
+}
+
+
+inline void Vector::SetVal(double x, double y, double z)
+{
+       #ifdef DEBUG
+    if (nrData != 4)
+    {
+       ::MessageBox(0, "Attempt to assign data to a vector\n"
+                        "With size unequal to 4 in function\n"
+                        " Vector::Setval(double, double, double", "Error" , MB_OK);
+       exit(1);
+    }
+    #endif
+    data[0] = x,
+    data[1] = y;
+    data[2] = z;
+    data[3] = 0.00;
+}
+
+inline Vector* Vector::GetVector()
+{
+       return this;
+}
+
+inline double Vector::GetX()
+{
+       #ifdef DEBUG
+    if (nrData < 1)
+    {
+       ::MessageBox(0, "Attempt to retrieve x-value of a vector\n"
+                        "With size smaller than 1 in function\n"
+                        " Vector::GetX();", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    return data[0];
+}
+
+inline double Vector::GetY()
+{
+       #ifdef DEBUG
+    if (nrData < 2)
+    {
+       ::MessageBox(0, "Attempt to retrieve the y value of a vector\n"
+                        "With size smaller than 2 in function\n"
+                        " Vector::GetY();", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    return data[1];
+}
+
+inline double Vector::GetZ()
+{
+       #ifdef DEBUG
+    if (nrData < 3)
+    {
+               ::MessageBox(0, "Attempt to retrieve the z value of a vector\n"
+                        "With size smaller than 2 in function\n"
+                        " Vector::GetZ();", "Error", MB_OK);
+        exit(1);
+    }
+    #endif
+    return data[2];
+}
+
+inline void Vector::AddX(double x)
+{
+       #ifdef DEBUG
+    if (nrData < 1)
+    {
+       ::MessageBox(0, "Attempt to chance x-value to a vector\n"
+                        "With size smaller than 1 in function\n"
+                        " Vector::AddX(double);", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    data[0] += x;
+}
+
+inline void Vector::AddY(double y)
+{
+       #ifdef DEBUG
+    if (nrData < 2)
+    {
+       ::MessageBox(0, "Attempt to chance y-value to a vector\n"
+                        "With size smaller than 2 in function\n"
+                        " Vector::AddY(double);", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    data[1] += y;
+}
+
+inline void Vector::AddZ(double z)
+{
+       #ifdef DEBUG
+    if (nrData < 3)
+    {
+       ::MessageBox(0, "Attempt to chance z-value to a vector\n"
+                        "With size smaller than 3 in function\n"
+                        " Vector::AddZ(double);", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    data[2] += z;
+}
+
+inline void Vector::SubtractX(double x)
+{
+       #ifdef DEBUG
+    if (nrData < 1)
+    {
+       ::MessageBox(0, "Attempt to chance x-value to a vector\n"
+                        "With size smaller than 1 in function\n"
+                        " Vector::SubtractX(double);", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    data[0] -= x;
+}
+
+inline void Vector::SubtractY(double y)
+{
+       #ifdef DEBUG
+    if (nrData < 2)
+    {
+       ::MessageBox(0, "Attempt to chance y-value to a vector\n"
+                        "With size smaller than 2 in function\n"
+                        " Vector::SubractY(double);", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    data[1] -= y;
+}
+
+inline void Vector::SubtractZ(double z)
+{
+       #ifdef DEBUG
+    if (nrData < 3)
+    {
+       ::MessageBox(0, "Attempt to chance z-value to a vector\n"
+                        "With size smaller than 3 in function\n"
+                        " Vector::SubtractZ(double);", "Error", MB_OK);
+       exit(1);
+    }
+    #endif
+    data[2] -= z;
+}
+
+
+inline Vector& Vector::operator= (Vector& other)
+{
+    if (data)
+       delete[] data;
+    build(other.nrData);
+    memcpy(data, other.data, nrData*sizeof(double));
+    return *this;
+}
+
+inline double& Vector::operator [](int index)
+{
+       return *(data+index);
+}
+
+
+inline int Vector::getDim()
+{
+       return nrData;
+}
+
+/*-----------------------------------------------------------------------------
+                                                       Some generic conversion routines
+------------------------------------------------------------------------------*/
+
+float CosD(float angle);
+float SinD(float angle);
+float Radians(float angle);
+int Round(float value);
+
+/* ----------------------------------------------------------------------------
+                               And their inlined implementation
+------------------------------------------------------------------------------*/
+
+inline float CosD(float angle)
+{
+       return cos(Radians(angle));
+}
+
+inline float SinD(float angle)
+{
+       return(Radians(angle));
+}
+
+
+inline float Radians(float angle)
+{
+       return (angle*PiOver180);
+}
+
+inline int Round(float value)
+{
+       return ( (int) (value+0.5));
+}
+
+
+
+/******************************************************************************
+
+                                                       Matrix class
+
+******************************************************************************/
+
+class Matrix
+{
+    protected:
+        int rows;
+        int columns;
+        Vector* data;
+
+    public:
+
+        Matrix();
+        Matrix(int r, int c);
+               Matrix(int r, int c, double* dta);
+        Matrix(int r, int c, double** dta);
+        Matrix(int r, int c, Vector*dta);
+        Matrix(Matrix&);
+        ~Matrix();
+
+        void build(int r, int c);
+        Matrix& operator=(Matrix&);
+        Vector& operator[](int);
+        Vector operator ()(int);
+
+               int getrows();
+        int getcols();
+        void norm(int scal);
+
+        friend Vector operator*(Matrix mc, Vector vc);
+};
+
+/*------------------------------------------------------------------------------
+                                       inline Matrix routines
+------------------------------------------------------------------------------*/
+
+inline Matrix::Matrix()
+{
+       rows = 0;
+    columns = 0;
+    data = 0;
+}
+
+inline Matrix::Matrix(int r, int c)
+{
+       build(r, c);
+}
+
+inline Matrix::~Matrix()
+{
+       delete [] data;
+}
+
+
+inline Vector& Matrix::operator[] (int row)
+{
+       return data[row];
+}
+
+
+#endif // _MYMATH_H_
+
+
diff --git a/src/Time/timezone.cxx b/src/Time/timezone.cxx
new file mode 100644 (file)
index 0000000..cd5db78
--- /dev/null
@@ -0,0 +1,147 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * timezone.cc
+ * Written by Durk Talsma. Started July 1999.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/*************************************************************************
+ *
+ * Timezone is derived from geocoord, and stores the timezone centerpoint,
+ * as well as the countrycode and the timezone descriptor. The latter is 
+ * used in order to get the local time. 
+ *
+ ************************************************************************/
+#include <stdio.h>
+#include "timezone.h"
+
+
+Timezone::Timezone(float la, float lo, char* cc, char* desc) :
+  GeoCoord(la, lo)
+{ 
+  countryCode = strdup(cc);
+  descriptor = strdup(desc);
+}
+
+/* Build a timezone object from a textline in zone.tab */
+Timezone::Timezone(const char *infoString) :
+  GeoCoord()
+{
+  int i = 0;
+  while (infoString[i] != '\t')
+    i++;
+  char buffer[128];
+  char latlon[128];
+  strncpy(buffer, infoString, i);
+  buffer[i] = 0;
+  countryCode = strdup(buffer);
+  i ++;
+  int start = i;
+  while (infoString[i] != '\t')
+    i++;
+  int size = i - start;
+  strncpy(latlon, (&infoString[start]), size);
+  latlon[size] = 0;
+  char sign;
+  sign = latlon[0];
+  strncpy(buffer, &latlon[1], 2);
+  lat = atof(buffer);
+  strncpy(buffer, &latlon[3], 2);
+  lat += (atof(buffer) / 60);
+  int nextPos;
+  if (strlen(latlon) > 12)
+    {
+      nextPos = 7;
+      strncpy(buffer, &latlon[5], 2);
+      lat += (atof(buffer) / 3600.0);
+    }
+  else
+    nextPos = 5;
+  if (sign == '-')
+    lat = -lat;
+
+  sign = latlon[nextPos];
+  nextPos++;
+  strncpy(buffer, &latlon[nextPos], 3);
+  lon = atof(buffer);
+  nextPos += 3;
+  strncpy(buffer, &latlon[nextPos], 2);
+  buffer[2] = 0;
+  lon  += (atof(buffer) / 60);
+  if (strlen(latlon) > 12)
+    {
+      nextPos += 2;
+      strncpy(buffer, &latlon[nextPos], 2); 
+      lon +=  (atof (buffer) / 3600.00);
+    }
+  if (sign == '-')
+    lon = -lon;
+  i ++;
+  start = i;
+  while (!((infoString[i] == '\t') || (infoString[i] == '\n')))
+    i++;
+  size = i - start;
+  strncpy(buffer, (&infoString[start]), size);
+  buffer[size] = 0;
+  descriptor = strdup(buffer);
+}
+
+/* the copy constructor */
+Timezone::Timezone(const Timezone& other)
+{
+  lat = other.getLat();
+  lon = other.getLon();
+  countryCode = strdup(other.countryCode);
+  descriptor = strdup(other.descriptor);
+}
+
+
+/********* Member functions for TimezoneContainer class ********/
+
+TimezoneContainer::TimezoneContainer(const char *filename)
+{
+  char buffer[256];
+  FILE* infile = fopen(filename, "r");
+  if (!(infile))
+    {
+      fprintf(stderr, "Unable to open file %s\n", filename);
+      exit(1);
+    }
+  else
+    { 
+      
+      while (1)
+       {
+         fgets(buffer, 256, infile);
+         if (feof(infile))
+           break;
+         for (int i = 0; i < 256; i++)
+           {
+             if (buffer[i] == '#')
+               buffer[i] = 0;
+           }
+         if (buffer[0])
+           {
+             data.push_back(new Timezone(buffer));
+           }
+       }
+    }
+}
+
+TimezoneContainer::~TimezoneContainer()
+{
+}
diff --git a/src/Time/timezone.h b/src/Time/timezone.h
new file mode 100644 (file)
index 0000000..cac4aca
--- /dev/null
@@ -0,0 +1,72 @@
+/* -*- Mode: C++ -*- *****************************************************
+ * timezone.h
+ * Written by Durk Talsma. Started July 1999.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ **************************************************************************/
+
+/*************************************************************************
+ *
+ * Timezone is derived from geocoord, and stores the timezone centerpoint,
+ * as well as the countrycode and the timezone descriptor. The latter is 
+ * used in order to get the local time. 
+ *
+ ************************************************************************/
+
+#ifndef _TIMEZONE_H_
+#define _TIMEZONE_H_
+
+#include "geocoord.h"
+#include <stdio.h>
+
+class Timezone : public GeoCoord
+{
+private:
+  char* countryCode;
+  char* descriptor;
+
+public:
+  Timezone() :
+    GeoCoord()
+    { 
+      countryCode = 0; 
+      descriptor = 0;
+    };
+  Timezone(float la, float lo, char* cc, char* desc);
+  Timezone(const char *infoString);
+  Timezone(const Timezone &other);
+  virtual ~Timezone() { delete [] countryCode; delete [] descriptor; };
+  
+
+  virtual void print() { printf("%s", descriptor);};
+  virtual char * getDescription() { return descriptor; };
+};
+
+/************************************************************************
+ * Timezone container is derived from GeoCoordContainer, and has some 
+ * added functionality.
+ ************************************************************************/
+
+class TimezoneContainer : public GeoCoordContainer
+{
+ public:
+  TimezoneContainer(const char *filename);
+  virtual ~TimezoneContainer();
+};
+
+
+
+#endif // _TIMEZONE_H_