Don't use localtime_r on Windows

That code was not tested actually.
On windows struct tm doesn't have localtime_r and tm_zone
pull/110/head
Hleb Valoshka 2018-11-06 00:56:36 +03:00
parent d7839beaf2
commit 9d5c914e4b
2 changed files with 34 additions and 25 deletions

View File

@ -57,7 +57,6 @@
//#include "qtvideocapturedialog.h"
#include "celestia/scriptmenu.h"
#include "celestia/url.h"
#include "celutil/localtime.h"
#include "qtbookmark.h"
#if defined(_WIN32)
@ -1336,6 +1335,7 @@ void CelestiaAppWindow::createMenus()
m_appCore->getSimulation()->setSyncTime(check);
// Set up the default time zone name and offset from UTC
#ifndef _WIN32
time_t curtime = time(nullptr);
m_appCore->start(astro::UTCtoTDB((double) curtime / 86400.0 + (double) astro::Date(1970, 1, 1)));
@ -1345,6 +1345,39 @@ void CelestiaAppWindow::createMenus()
m_appCore->setTimeZoneBias(result.tm_gmtoff);
m_appCore->setTimeZoneName(result.tm_zone);
}
#else
TIME_ZONE_INFORMATION tzi;
DWORD dst = GetTimeZoneInformation(&tzi);
if (dst != TIME_ZONE_ID_INVALID)
{
LONG dstBias = 0;
WCHAR* tzName = nullptr;
if (dst == TIME_ZONE_ID_STANDARD)
{
dstBias = tzi.StandardBias;
tzName = tzi.StandardName;
}
else if (dst == TIME_ZONE_ID_DAYLIGHT)
{
dstBias = tzi.DaylightBias;
tzName = tzi.DaylightName;
}
if (tzName == nullptr)
{
m_appCore->setTimeZoneName(" ");
}
else
{
char tz_name_out[20];
size_t length = wcstombs(tz_name_out, tzName, sizeof(tz_name_out)-1);
tz_name_out[std::min(sizeof(tz_name_out)-1, length)] = '\0';
m_appCore->setTimeZoneName(tz_name_out);
}
m_appCore->setTimeZoneBias((tzi.Bias + dstBias) * -60);
}
#endif
// If LocalTime is set to false, set the time zone bias to zero.
if (settings.contains("LocalTime"))

View File

@ -1,24 +0,0 @@
#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