Timezone fixes for cygwin.
This commit is contained in:
+44
-28
@@ -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 );
|
||||||
|
|
||||||
|
|||||||
+93
-88
@@ -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);
|
int size = i - start;
|
||||||
latlon[size] = 0;
|
strncpy(latlon, (&infoString[start]), size);
|
||||||
char sign;
|
latlon[size] = 0;
|
||||||
sign = latlon[0];
|
char sign;
|
||||||
strncpy(buffer, &latlon[1], 2);
|
sign = latlon[0];
|
||||||
lat = atof(buffer);
|
strncpy(buffer, &latlon[1], 2);
|
||||||
strncpy(buffer, &latlon[3], 2);
|
lat = atof(buffer);
|
||||||
lat += (atof(buffer) / 60);
|
strncpy(buffer, &latlon[3], 2);
|
||||||
int nextPos;
|
lat += (atof(buffer) / 60);
|
||||||
if (strlen(latlon) > 12)
|
int nextPos;
|
||||||
{
|
if (strlen(latlon) > 12) {
|
||||||
nextPos = 7;
|
nextPos = 7;
|
||||||
strncpy(buffer, &latlon[5], 2);
|
strncpy(buffer, &latlon[5], 2);
|
||||||
lat += (atof(buffer) / 3600.0);
|
lat += (atof(buffer) / 3600.0);
|
||||||
|
} else {
|
||||||
|
nextPos = 5;
|
||||||
|
}
|
||||||
|
if (sign == '-') {
|
||||||
|
lat = -lat;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
nextPos = 5;
|
|
||||||
if (sign == '-')
|
|
||||||
lat = -lat;
|
|
||||||
|
|
||||||
sign = latlon[nextPos];
|
sign = latlon[nextPos];
|
||||||
nextPos++;
|
nextPos++;
|
||||||
strncpy(buffer, &latlon[nextPos], 3);
|
strncpy(buffer, &latlon[nextPos], 3);
|
||||||
lon = atof(buffer);
|
lon = atof(buffer);
|
||||||
nextPos += 3;
|
nextPos += 3;
|
||||||
strncpy(buffer, &latlon[nextPos], 2);
|
strncpy(buffer, &latlon[nextPos], 2);
|
||||||
buffer[2] = 0;
|
buffer[2] = 0;
|
||||||
|
|
||||||
lon += (atof(buffer) / 60);
|
lon += (atof(buffer) / 60);
|
||||||
if (strlen(latlon) > 12)
|
if (strlen(latlon) > 12) {
|
||||||
{
|
nextPos += 2;
|
||||||
nextPos += 2;
|
strncpy(buffer, &latlon[nextPos], 2);
|
||||||
strncpy(buffer, &latlon[nextPos], 2);
|
lon += (atof (buffer) / 3600.00);
|
||||||
lon += (atof (buffer) / 3600.00);
|
|
||||||
}
|
}
|
||||||
if (sign == '-')
|
if (sign == '-') {
|
||||||
lon = -lon;
|
lon = -lon;
|
||||||
i ++;
|
}
|
||||||
start = i;
|
i ++;
|
||||||
while (!((infoString[i] == '\t') || (infoString[i] == '\n')))
|
start = i;
|
||||||
i++;
|
while (!((infoString[i] == '\t') || (infoString[i] == '\n'))) {
|
||||||
size = i - start;
|
i++;
|
||||||
strncpy(buffer, (&infoString[start]), size);
|
}
|
||||||
buffer[size] = 0;
|
size = i - start;
|
||||||
descriptor = strdup(buffer);
|
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) {
|
||||||
|
fgets(buffer, 256, infile);
|
||||||
while (1)
|
if (feof(infile)) {
|
||||||
{
|
break;
|
||||||
fgets(buffer, 256, infile);
|
}
|
||||||
if (feof(infile))
|
for (int i = 0; i < 256; i++) {
|
||||||
break;
|
if (buffer[i] == '#') {
|
||||||
for (int i = 0; i < 256; i++)
|
buffer[i] = 0;
|
||||||
{
|
}
|
||||||
if (buffer[i] == '#')
|
}
|
||||||
buffer[i] = 0;
|
if (buffer[0]) {
|
||||||
}
|
data.push_back(new Timezone(buffer));
|
||||||
if (buffer[0])
|
}
|
||||||
{
|
}
|
||||||
data.push_back(new Timezone(buffer));
|
if ( errno ) {
|
||||||
}
|
perror( "TimezoneContainer()" );
|
||||||
}
|
errno = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user