Celestia
1// tzutil.cpp
2//
3// Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
4//
5// Miscellaneous useful functions.
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11
12#ifdef _WIN3213#include <windows.h>14#include "winutil.h"15#include "gettext.h"16#include "logger.h"17
18namespace util = celestia::util;19
20#else21// we need the C version of this header to get the POSIX function localtime_r
22#include <time.h>23#endif24
25#include "tzutil.h"26
27bool GetTZInfo(std::string& tzName, int& dstBias)28{
29#ifdef _WIN3230using celestia::util::GetLogger;31
32TIME_ZONE_INFORMATION tzi;33DWORD dst = GetTimeZoneInformation(&tzi);34if (dst == TIME_ZONE_ID_INVALID)35return false;36
37LONG bias = 0;38WCHAR* name = nullptr;39
40switch (dst)41{42case TIME_ZONE_ID_STANDARD:43bias = tzi.StandardBias;44name = tzi.StandardName;45break;46case TIME_ZONE_ID_DAYLIGHT:47bias = tzi.DaylightBias;48name = tzi.DaylightName;49break;50default:51GetLogger()->warn(_("Unknown value returned by GetTimeZoneInformation()\n"));52return false;53}54
55tzName = name == nullptr ? " " : util::WideToUTF8(name);56dstBias = (tzi.Bias + bias) * -60;57return true;58#else59tm result;60time_t curtime = time(nullptr); // required only to get TZ info61if (!localtime_r(&curtime, &result))62return false;63
64dstBias = result.tm_gmtoff;65tzName = result.tm_zone;66return true;67#endif68}
69