Use localtime_r on all platforms to set TZ offset and name (Closes: #92)

pull/110/head
Hleb Valoshka 2018-09-22 17:34:26 +03:00
parent 4bdfe271e5
commit cfc6923c20
3 changed files with 39 additions and 23 deletions

View File

@ -29,6 +29,7 @@
#include <celmath/quaternion.h>
#include <celutil/util.h>
#include <celutil/debug.h>
#include <celutil/localtime.h>
#include <celmath/mathlib.h>
#include <celengine/astro.h>
#include "celestiacore.h"
@ -517,21 +518,14 @@ int main(int argc, char* argv[])
appCore->initRenderer();
// Set the simulation starting time to the current system time
time_t curtime=time(nullptr);
time_t curtime = time(nullptr);
appCore->start((double) curtime / 86400.0 + (double) astro::Date(1970, 1, 1));
#ifdef MACOSX
/* localtime in Darwin is is reentrant only
equiv to Linux localtime_r()
should probably port !MACOSX code to use this too, available since
libc 5.2.5 according to manpage */
struct tm *temptime=localtime(&curtime);
appCore->setTimeZoneBias(temptime->tm_gmtoff);
appCore->setTimeZoneName(temptime->tm_zone);
#else
localtime(&curtime); // Only doing this to set timezone as a side effect
appCore->setTimeZoneBias(-timezone);
appCore->setTimeZoneName(tzname[daylight?0:1]);
#endif
struct tm result;
if (localtime_r(&curtime, &result))
{
appCore->setTimeZoneBias(result.tm_gmtoff);
appCore->setTimeZoneName(result.tm_zone);
}
if (startfile == 1) {
if (argv[argc - 1][0] == '-') {

View File

@ -57,11 +57,11 @@
//#include "qtvideocapturedialog.h"
#include "celestia/scriptmenu.h"
#include "celestia/url.h"
#include "celutil/localtime.h"
#include "qtbookmark.h"
#if defined(_WIN32)
#include "celestia/avicapture.h"
// TODO: Add Mac support
#elif !defined(TARGET_OS_MAC)
#ifdef THEORA
@ -1334,15 +1334,13 @@ void CelestiaAppWindow::createMenus()
// Set up the default time zone name and offset from UTC
time_t curtime = time(nullptr);
m_appCore->start(astro::UTCtoTDB((double) curtime / 86400.0 + (double) astro::Date(1970, 1, 1)));
localtime(&curtime); // Only doing this to set timezone as a side effect
#ifdef TARGET_OS_MAC
CFTimeZoneRef tz = CFTimeZoneCopyDefault();
m_appCore->setTimeZoneBias(-CFTimeZoneGetSecondsFromGMT(tz, CFAbsoluteTimeGetCurrent())+3600*daylight);
CFRelease(tz);
#else
m_appCore->setTimeZoneBias(-timezone + 3600 * daylight);
#endif
struct tm result;
if (localtime_r(&curtime, &result))
{
m_appCore->setTimeZoneBias(result.tm_gmtoff);
m_appCore->setTimeZoneName(result.tm_zone);
}
// If LocalTime is set to false, set the time zone bias to zero.
if (settings.contains("LocalTime"))

View File

@ -0,0 +1,24 @@
#ifndef LOCALTIME_H
#define LOCALTIME_H
#ifdef _WIN32
/*
Very dump implementation of localtime_r for Windows platform.
*/
extern "C" {
struct tm *localtime_r(time_t *clock, struct tm *result)
{
struct tm *p = localtime(clock);
if (p)
{
*result = *p;
return result;
}
return NULL;
}
}
#endif
#endif