From 81e5963a5db6df30b72c9ec716d06e86eb6fca03 Mon Sep 17 00:00:00 2001 From: curt Date: Thu, 26 Apr 2001 16:15:01 +0000 Subject: [PATCH] Timezone fixes for cygwin. --- simgear/timing/sg_time.cxx | 76 +++++++++------ simgear/timing/timezone.cxx | 181 ++++++++++++++++++------------------ 2 files changed, 139 insertions(+), 118 deletions(-) diff --git a/simgear/timing/sg_time.cxx b/simgear/timing/sg_time.cxx index 64628674..91a2d8ef 100644 --- a/simgear/timing/sg_time.cxx +++ b/simgear/timing/sg_time.cxx @@ -77,28 +77,29 @@ SGTime::SGTime( double lon, double lat, const string& root ) gst_diff = -9999.0; cur_time = time(NULL); - // cout << "Current greenwich mean time = " << asctime(gmtime(&cur_time)) - // << endl; - // cout << "Current local time = " - // << asctime(localtime(&cur_time)) << endl; + cout << "Current greenwich mean time = " << asctime(gmtime(&cur_time)) + << endl; + cout << "Current local time = " + << asctime(localtime(&cur_time)) << endl; if ( root != (string)"" ) { - SGPath zone( root ); - zone.append( "zone.tab" ); - SG_LOG( SG_EVENT, SG_DEBUG, "Reading timezone info from: " - << zone.str() ); - tzContainer = new TimezoneContainer( zone.c_str() ); - - GeoCoord location( SGD_RADIANS_TO_DEGREES * lat, SGD_RADIANS_TO_DEGREES * lon ); - GeoCoord* nearestTz = tzContainer->getNearest(location); - - SGPath name( root ); - name.append( nearestTz->getDescription() ); - zonename = strdup( name.c_str() ); - // cout << "Using zonename = " << zonename << endl; + SGPath zone( root ); + zone.append( "zone.tab" ); + SG_LOG( SG_EVENT, SG_INFO, "Reading timezone info from: " + << zone.str() ); + tzContainer = new TimezoneContainer( zone.c_str() ); + + GeoCoord location( SGD_RADIANS_TO_DEGREES * lat, SGD_RADIANS_TO_DEGREES * lon ); + GeoCoord* nearestTz = tzContainer->getNearest(location); + + SGPath name( root ); + name.append( nearestTz->getDescription() ); + zonename = strdup( name.c_str() ); + SG_LOG( SG_EVENT, SG_INFO, "Using zonename = " << zonename ); } else { - tzContainer = NULL; - zonename = NULL; + SG_LOG( SG_EVENT, SG_INFO, "*** NO TIME ZONE NAME ***" ); + tzContainer = NULL; + zonename = NULL; } } @@ -413,22 +414,37 @@ time_t sgTimeGetGMT(int year, int month, int day, int hour, int min, int sec) return ( timegm(&mt) ); #elif defined( MK_TIME_IS_GMT ) time_t ret = mktime(&mt); + +#ifdef __CYGWIN__ + ret -= _timezone; +#endif + // This is necessary as some mktime() calls may // try to access the system timezone files // if this open fails errno is set to 2 // CYGWIN for one does this - if ( errno ) { - perror( "sgTimeGetGMT()" ); - errno = 0; - } + // if ( errno ) { + // perror( "sgTimeGetGMT()" ); + // errno = 0; + // } + + // reset errno in any event. + errno = 0; + return ret; #else // ! defined ( MK_TIME_IS_GMT ) // timezone seems to work as a proper offset for Linux & Solaris -# if defined( __linux__ ) || defined( __sun__ ) +# if defined( __linux__ ) || defined( __sun__ ) ||defined(__CYGWIN__) # define TIMEZONE_OFFSET_WORKS 1 # endif +#if defined(__CYGWIN__) +#define TIMEZONE _timezone +#else +#define TIMEZONE timezone +#endif + time_t start = mktime(&mt); SG_LOG( SG_EVENT, SG_DEBUG, "start1 = " << start ); @@ -437,12 +453,12 @@ time_t sgTimeGetGMT(int year, int month, int day, int hour, int min, int sec) // fgPrintf( SG_EVENT, SG_DEBUG, "start2 = %s", ctime(&start)); SG_LOG( SG_EVENT, SG_DEBUG, "(tm_isdst = " << mt.tm_isdst << ")" ); - timezone = fix_up_timezone( timezone ); + TIMEZONE = fix_up_timezone( TIMEZONE ); # if defined( TIMEZONE_OFFSET_WORKS ) SG_LOG( SG_EVENT, SG_DEBUG, - "start = " << start << ", timezone = " << timezone ); - return( start - timezone ); + "start = " << start << ", timezone = " << TIMEZONE ); + return( start - TIMEZONE ); # else // ! defined( TIMEZONE_OFFSET_WORKS ) daylight = mt.tm_isdst; @@ -453,13 +469,13 @@ time_t sgTimeGetGMT(int year, int month, int day, int hour, int min, int sec) "OOOPS, problem in sg_time.cxx, no daylight savings info." ); } - long int offset = -(timezone / 3600 - daylight); + long int offset = -(TIMEZONE / 3600 - daylight); - SG_LOG( SG_EVENT, SG_DEBUG, " Raw time zone offset = " << timezone ); + SG_LOG( SG_EVENT, SG_DEBUG, " Raw time zone offset = " << TIMEZONE ); SG_LOG( SG_EVENT, SG_DEBUG, " Daylight Savings = " << daylight ); SG_LOG( SG_EVENT, SG_DEBUG, " Local hours from GMT = " << offset ); - long int start_gmt = start - timezone + (daylight * 3600); + long int start_gmt = start - TIMEZONE + (daylight * 3600); SG_LOG( SG_EVENT, SG_DEBUG, " March 21 noon (CST) = " << start ); diff --git a/simgear/timing/timezone.cxx b/simgear/timing/timezone.cxx index 51951a15..9bdb7a2f 100644 --- a/simgear/timing/timezone.cxx +++ b/simgear/timing/timezone.cxx @@ -26,88 +26,92 @@ * used in order to get the local time. * ************************************************************************/ + +#include +#include #include #include "timezone.h" - Timezone::Timezone(float la, float lo, char* cc, char* desc) : - GeoCoord(la, lo) + GeoCoord(la, lo) { - countryCode = strdup(cc); - descriptor = strdup(desc); + countryCode = strdup(cc); + descriptor = strdup(desc); } /* Build a timezone object from a textline in zone.tab */ Timezone::Timezone(const char *infoString) : - GeoCoord() + 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); + 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; } - 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; + 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); + 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); + 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); + lat = other.getLat(); + lon = other.getLon(); + countryCode = strdup(other.countryCode); + descriptor = strdup(other.descriptor); } @@ -115,31 +119,32 @@ Timezone::Timezone(const Timezone& other) 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)); - } - } + char buffer[256]; + FILE* infile = fopen(filename, "rb"); + if (!(infile)) { + fprintf(stderr, "Unable to open file %s\n", filename); + exit(1); + } else { + errno = 0; + + 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)); + } + } + if ( errno ) { + perror( "TimezoneContainer()" ); + errno = 0; + } } } -- 2.39.5