Timezone fixes for cygwin.

This commit is contained in:
curt
2001-04-26 16:15:01 +00:00
parent 14569e4fae
commit 81e5963a5d
2 changed files with 137 additions and 116 deletions
+44 -28
View File
@@ -77,28 +77,29 @@ SGTime::SGTime( double lon, double lat, const string& root )
gst_diff = -9999.0; gst_diff = -9999.0;
cur_time = time(NULL); cur_time = time(NULL);
// cout << "Current greenwich mean time = " << asctime(gmtime(&cur_time)) cout << "Current greenwich mean time = " << asctime(gmtime(&cur_time))
// << endl; << endl;
// cout << "Current local time = " cout << "Current local time = "
// << asctime(localtime(&cur_time)) << endl; << asctime(localtime(&cur_time)) << endl;
if ( root != (string)"" ) { if ( root != (string)"" ) {
SGPath zone( root ); SGPath zone( root );
zone.append( "zone.tab" ); zone.append( "zone.tab" );
SG_LOG( SG_EVENT, SG_DEBUG, "Reading timezone info from: " SG_LOG( SG_EVENT, SG_INFO, "Reading timezone info from: "
<< zone.str() ); << zone.str() );
tzContainer = new TimezoneContainer( zone.c_str() ); tzContainer = new TimezoneContainer( zone.c_str() );
GeoCoord location( SGD_RADIANS_TO_DEGREES * lat, SGD_RADIANS_TO_DEGREES * lon ); GeoCoord location( SGD_RADIANS_TO_DEGREES * lat, SGD_RADIANS_TO_DEGREES * lon );
GeoCoord* nearestTz = tzContainer->getNearest(location); GeoCoord* nearestTz = tzContainer->getNearest(location);
SGPath name( root ); SGPath name( root );
name.append( nearestTz->getDescription() ); name.append( nearestTz->getDescription() );
zonename = strdup( name.c_str() ); zonename = strdup( name.c_str() );
// cout << "Using zonename = " << zonename << endl; SG_LOG( SG_EVENT, SG_INFO, "Using zonename = " << zonename );
} else { } else {
tzContainer = NULL; SG_LOG( SG_EVENT, SG_INFO, "*** NO TIME ZONE NAME ***" );
zonename = NULL; 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) ); return ( timegm(&mt) );
#elif defined( MK_TIME_IS_GMT ) #elif defined( MK_TIME_IS_GMT )
time_t ret = mktime(&mt); time_t ret = mktime(&mt);
#ifdef __CYGWIN__
ret -= _timezone;
#endif
// This is necessary as some mktime() calls may // This is necessary as some mktime() calls may
// try to access the system timezone files // try to access the system timezone files
// if this open fails errno is set to 2 // if this open fails errno is set to 2
// CYGWIN for one does this // CYGWIN for one does this
if ( errno ) { // if ( errno ) {
perror( "sgTimeGetGMT()" ); // perror( "sgTimeGetGMT()" );
errno = 0; // errno = 0;
} // }
// reset errno in any event.
errno = 0;
return ret; return ret;
#else // ! defined ( MK_TIME_IS_GMT ) #else // ! defined ( MK_TIME_IS_GMT )
// timezone seems to work as a proper offset for Linux & Solaris // 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 # define TIMEZONE_OFFSET_WORKS 1
# endif # endif
#if defined(__CYGWIN__)
#define TIMEZONE _timezone
#else
#define TIMEZONE timezone
#endif
time_t start = mktime(&mt); time_t start = mktime(&mt);
SG_LOG( SG_EVENT, SG_DEBUG, "start1 = " << start ); 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)); // fgPrintf( SG_EVENT, SG_DEBUG, "start2 = %s", ctime(&start));
SG_LOG( SG_EVENT, SG_DEBUG, "(tm_isdst = " << mt.tm_isdst << ")" ); 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 ) # if defined( TIMEZONE_OFFSET_WORKS )
SG_LOG( SG_EVENT, SG_DEBUG, SG_LOG( SG_EVENT, SG_DEBUG,
"start = " << start << ", timezone = " << timezone ); "start = " << start << ", timezone = " << TIMEZONE );
return( start - timezone ); return( start - TIMEZONE );
# else // ! defined( TIMEZONE_OFFSET_WORKS ) # else // ! defined( TIMEZONE_OFFSET_WORKS )
daylight = mt.tm_isdst; 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." ); "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, " Daylight Savings = " << daylight );
SG_LOG( SG_EVENT, SG_DEBUG, " Local hours from GMT = " << offset ); 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 ); SG_LOG( SG_EVENT, SG_DEBUG, " March 21 noon (CST) = " << start );
+94 -89
View File
@@ -26,88 +26,92 @@
* used in order to get the local time. * used in order to get the local time.
* *
************************************************************************/ ************************************************************************/
#include <errno.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include "timezone.h" #include "timezone.h"
Timezone::Timezone(float la, float lo, char* cc, char* desc) : Timezone::Timezone(float la, float lo, char* cc, char* desc) :
GeoCoord(la, lo) GeoCoord(la, lo)
{ {
countryCode = strdup(cc); countryCode = strdup(cc);
descriptor = strdup(desc); descriptor = strdup(desc);
} }
/* Build a timezone object from a textline in zone.tab */ /* Build a timezone object from a textline in zone.tab */
Timezone::Timezone(const char *infoString) : Timezone::Timezone(const char *infoString) :
GeoCoord() GeoCoord()
{ {
int i = 0; int i = 0;
while (infoString[i] != '\t') while (infoString[i] != '\t')
i++; i++;
char buffer[128]; char buffer[128];
char latlon[128]; char latlon[128];
strncpy(buffer, infoString, i); strncpy(buffer, infoString, i);
buffer[i] = 0; buffer[i] = 0;
countryCode = strdup(buffer); countryCode = strdup(buffer);
i ++; i ++;
int start = i; int start = i;
while (infoString[i] != '\t') while (infoString[i] != '\t') {
i++; 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 int size = i - start;
nextPos = 5; strncpy(latlon, (&infoString[start]), size);
if (sign == '-') latlon[size] = 0;
lat = -lat; char sign;
sign = latlon[0];
sign = latlon[nextPos]; strncpy(buffer, &latlon[1], 2);
nextPos++; lat = atof(buffer);
strncpy(buffer, &latlon[nextPos], 3); strncpy(buffer, &latlon[3], 2);
lon = atof(buffer); lat += (atof(buffer) / 60);
nextPos += 3; int nextPos;
strncpy(buffer, &latlon[nextPos], 2); if (strlen(latlon) > 12) {
buffer[2] = 0; nextPos = 7;
strncpy(buffer, &latlon[5], 2);
lon += (atof(buffer) / 60); lat += (atof(buffer) / 3600.0);
if (strlen(latlon) > 12) } else {
{ nextPos = 5;
nextPos += 2;
strncpy(buffer, &latlon[nextPos], 2);
lon += (atof (buffer) / 3600.00);
} }
if (sign == '-') if (sign == '-') {
lon = -lon; lat = -lat;
i ++; }
start = i;
while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) sign = latlon[nextPos];
i++; nextPos++;
size = i - start; strncpy(buffer, &latlon[nextPos], 3);
strncpy(buffer, (&infoString[start]), size); lon = atof(buffer);
buffer[size] = 0; nextPos += 3;
descriptor = strdup(buffer); 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 */ /* the copy constructor */
Timezone::Timezone(const Timezone& other) Timezone::Timezone(const Timezone& other)
{ {
lat = other.getLat(); lat = other.getLat();
lon = other.getLon(); lon = other.getLon();
countryCode = strdup(other.countryCode); countryCode = strdup(other.countryCode);
descriptor = strdup(other.descriptor); descriptor = strdup(other.descriptor);
} }
@@ -115,31 +119,32 @@ Timezone::Timezone(const Timezone& other)
TimezoneContainer::TimezoneContainer(const char *filename) TimezoneContainer::TimezoneContainer(const char *filename)
{ {
char buffer[256]; char buffer[256];
FILE* infile = fopen(filename, "r"); FILE* infile = fopen(filename, "rb");
if (!(infile)) if (!(infile)) {
{ fprintf(stderr, "Unable to open file %s\n", filename);
fprintf(stderr, "Unable to open file %s\n", filename); exit(1);
exit(1); } else {
} errno = 0;
else
{
while (1) while (1) {
{ fgets(buffer, 256, infile);
fgets(buffer, 256, infile); if (feof(infile)) {
if (feof(infile)) break;
break; }
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++) {
{ if (buffer[i] == '#') {
if (buffer[i] == '#') buffer[i] = 0;
buffer[i] = 0; }
} }
if (buffer[0]) if (buffer[0]) {
{ data.push_back(new Timezone(buffer));
data.push_back(new Timezone(buffer)); }
} }
} if ( errno ) {
perror( "TimezoneContainer()" );
errno = 0;
}
} }
} }