More gracefully bail out on a missing timzeone file

This commit is contained in:
Erik Hofman 2021-03-13 13:09:59 +01:00
parent 1ea11d987e
commit 508a511070
2 changed files with 16 additions and 10 deletions

View File

@ -124,7 +124,7 @@ static int fgcompute_change(fgtz_rule *rule, int year);
static struct ttinfo *fgfind_transition (time_t timer); static struct ttinfo *fgfind_transition (time_t timer);
static void fgcompute_tzname_max (size_t chars); static void fgcompute_tzname_max (size_t chars);
static inline int decode (const void *ptr); static inline int decode (const void *ptr);
void fgtzfile_read (const char *file); bool fgtzfile_read (const char *file);
static void offtime (const time_t *t, long int offset, struct tm *tp); static void offtime (const time_t *t, long int offset, struct tm *tp);
static char *tzstring (const char* string); static char *tzstring (const char* string);
@ -327,8 +327,7 @@ static void fgtzset_internal (int always, const char *tz)
old_fgtz = tz ? strdup (tz) : NULL; old_fgtz = tz ? strdup (tz) : NULL;
/* Try to read a data file. */ /* Try to read a data file. */
fgtzfile_read (tz); if (fgtzfile_read(tz) && use_fgtzfile)
if (use_fgtzfile)
return; return;
// The default behaviour of the original tzset_internal (int always, char* tz) // The default behaviour of the original tzset_internal (int always, char* tz)
// function is to set up a default timezone, in any case file_read() fails // function is to set up a default timezone, in any case file_read() fails
@ -785,7 +784,7 @@ static struct ttinfo * fgfind_transition (time_t timer)
/**************************************************************************/ /**************************************************************************/
void fgtzfile_read (const char *file) bool fgtzfile_read (const char *file)
{ {
// static const char default_tzdir[] = TZDIR; // static const char default_tzdir[] = TZDIR;
size_t num_isstd, num_isgmt; size_t num_isstd, num_isgmt;
@ -818,7 +817,7 @@ void fgtzfile_read (const char *file)
file = TZDEFAULT; file = TZDEFAULT;
else if (*file == '\0') else if (*file == '\0')
/* User specified the empty string; use UTC with no leap seconds. */ /* User specified the empty string; use UTC with no leap seconds. */
return; return true;
else else
{ {
/* We must not allow to read an arbitrary file in a setuid /* We must not allow to read an arbitrary file in a setuid
@ -866,9 +865,9 @@ void fgtzfile_read (const char *file)
#endif #endif
if (f == NULL) { if (f == NULL) {
perror( "fgtzfile_read(): " ); // perror( "fgtzfile_read(): " );
errno = 0; errno = 0;
return; return false;
} }
if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1) if (fread ((void *) &tzhead, sizeof (tzhead), 1, f) != 1)
@ -991,10 +990,11 @@ void fgtzfile_read (const char *file)
fgcompute_tzname_max (chars); fgcompute_tzname_max (chars);
use_fgtzfile = 1; use_fgtzfile = 1;
return; return true;
lose:; lose:;
fclose(f); fclose(f);
return false;
} }
/****************************************************************************/ /****************************************************************************/

View File

@ -241,11 +241,17 @@ void SGTime::updateLocal( const SGGeod& aLocation, const SGPath& root ) {
currGMT = sgTimeGetGMT( gmtime(&cur_time) ); currGMT = sgTimeGetGMT( gmtime(&cur_time) );
std::string zs = zone.utf8Str(); std::string zs = zone.utf8Str();
aircraftLocalTime = sgTimeGetGMT( (fgLocaltime(&cur_time, zs.c_str())) ); struct tm *local_time = fgLocaltime(&cur_time, zs.c_str());
if (local_time) {
aircraftLocalTime = sgTimeGetGMT( local_time );
local_offset = aircraftLocalTime - currGMT; local_offset = aircraftLocalTime - currGMT;
// cout << "Using " << local_offset << " as local time offset Timezone is " // cout << "Using " << local_offset << " as local time offset Timezone is "
// << zonename << endl; // << zonename << endl;
} }
else {
SG_LOG( SG_EVENT, SG_ALERT, "Timezone file not found: " << zs );
}
}
// given a date in months, mn, days, dy, years, yr, return the // given a date in months, mn, days, dy, years, yr, return the