TrinityCore
Timezone.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "Timezone.h"
19#include "Hash.h"
20#include "Locales.h"
21#include "MapUtils.h"
22#include "StringConvert.h"
23#include <boost/locale/date_time_facet.hpp>
24#include <chrono>
25#include <memory>
26#include <unordered_map>
27
28namespace
29{
30std::unordered_map<uint32, Minutes, std::identity> InitTimezoneHashDb()
31{
32#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
33
34 // Generate our hash db to match values sent in client authentication packets
35 std::unordered_map<uint32, Minutes, std::identity> hashToOffset;
36 std::chrono::system_clock::time_point dummmy;
37 for (std::chrono::time_zone const& zone : std::chrono::get_tzdb().zones)
38 {
39 std::chrono::sys_info sysInfo = zone.get_info(dummmy);
40 Minutes offsetMinutes = std::chrono::duration_cast<Minutes>(sysInfo.offset);
41 std::string offsetStr = Trinity::ToString(offsetMinutes.count());
42 hashToOffset.emplace(Trinity::HashFnv1a(offsetStr), offsetMinutes);
43 }
44
45#else
46 // Pre-generated list of timezone offsets and their hashes for compilers (and their stl implementations) that dont support timezone api yet
47 std::unordered_map<uint32, Minutes, std::identity> hashToOffset =
48 {
49 { 0xAADC2D37u, -720min },
50 { 0x362F107Bu, -690min },
51 { 0x2C44C70Cu, -660min },
52 { 0xB84A209Eu, -640min },
53 { 0xBA3D57D1u, -630min },
54 { 0x4040695Au, -600min },
55 { 0xB65A75D0u, -570min },
56 { 0xC8614DEBu, -540min },
57 { 0x3A68BD26u, -510min },
58 { 0x51E8096Cu, -480min },
59 { 0x4DD8F896u, -420min },
60 { 0x674B7C0Fu, -360min },
61 { 0x633C6B39u, -300min },
62 { 0x0BAD340Au, -240min },
63 { 0x74B25683u, -225min },
64 { 0x09B9FCD7u, -210min },
65 { 0x150C169Bu, -180min },
66 { 0x191B2771u, -120min },
67 { 0xD7D3B14Eu, -60min },
68 { 0x47CE5170u, -44min },
69 { 0x350CA8AFu, 0min },
70 { 0x15E8E23Bu, 60min },
71 { 0x733864AEu, 120min },
72 { 0xF71F9C94u, 180min },
73 { 0xBDE50F54u, 210min },
74 { 0x2BDD6DB9u, 240min },
75 { 0xB1E07F42u, 270min },
76 { 0x454FF132u, 300min },
77 { 0x3F4DA929u, 330min },
78 { 0xD1554AC4u, 360min },
79 { 0xBB667143u, 390min },
80 { 0x9E2B78C9u, 420min },
81 { 0x1C377816u, 450min },
82 { 0x1A4440E3u, 480min },
83 { 0xB49DF789u, 525min },
84 { 0xC3A28C54u, 540min },
85 { 0x35A9FB8Fu, 570min },
86 { 0x889BD751u, 600min },
87 { 0x8CAAE827u, 660min },
88 { 0x7285EE60u, 690min },
89 { 0x1CC2DEF4u, 720min },
90 { 0x89B8FD2Fu, 765min },
91 { 0x98DBA70Eu, 780min },
92 { 0xC59585BBu, 840min }
93 };
94#endif
95
96 return hashToOffset;
97}
98
99std::unordered_map<uint32, Minutes, std::identity> const& GetTimezoneOffsetsByHash()
100{
101 static std::unordered_map<uint32, Minutes, std::identity> timezoneMap = InitTimezoneHashDb();
102 return timezoneMap;
103}
104
105using ClientSupportedTimezone = std::pair<Minutes, std::string>;
106std::array<ClientSupportedTimezone, 11> const _clientSupportedTimezones =
107{{
108 { -480min, "America/Los_Angeles" },
109 { -420min, "America/Denver" },
110 { -360min, "America/Chicago" },
111 { -300min, "America/New_York" },
112 { -180min, "America/Sao_Paulo" },
113 { 0min, "Etc/UTC" },
114 { 60min, "Europe/Paris" },
115 { 480min, "Asia/Shanghai" },
116 { 480min, "Asia/Taipei" },
117 { 540min, "Asia/Seoul" },
118 { 600min, "Australia/Melbourne" },
119}};
120}
121
123{
125{
126 if (Minutes const* offset = Containers::MapGetValuePtr(GetTimezoneOffsetsByHash(), hash))
127 return *offset;
128
129 return 0min;
130}
131
133{
134 Seconds offset;
135#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
136 offset = std::chrono::current_zone()->get_info(date).offset;
137#else
138 tm buf = TimeBreakdown(std::chrono::system_clock::to_time_t(date));
139 offset = Seconds(buf.tm_gmtoff);
140#endif
141 return std::chrono::duration_cast<Minutes>(offset);
142}
143
144Minutes GetSystemZoneOffset(bool applyDst /*= true*/)
145{
146 std::chrono::system_clock::time_point date = std::chrono::system_clock::from_time_t(std::time_t(0));
147 if (applyDst)
148 date = std::chrono::system_clock::now();
149
150 return GetSystemZoneOffsetAt(date);
151}
152
153std::string GetSystemZoneName()
154{
155#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
156 return std::string(std::chrono::current_zone()->name());
157#else
158 std::unique_ptr<boost::locale::abstract_calendar> p(std::use_facet<class boost::locale::calendar_facet>(Locale::GetCalendarLocale()).create_calendar());
159 return p->get_timezone();
160#endif
161}
162
163std::string_view FindClosestClientSupportedTimezone(std::string_view currentTimezone, Minutes currentTimezoneOffset)
164{
165 // try exact match
166 auto itr = std::find_if(_clientSupportedTimezones.begin(), _clientSupportedTimezones.end(), [currentTimezone](ClientSupportedTimezone const& tz)
167 {
168 return tz.second == currentTimezone;
169 });
170 if (itr != _clientSupportedTimezones.end())
171 return itr->second;
172
173 // try closest offset
174 itr = std::min_element(_clientSupportedTimezones.begin(), _clientSupportedTimezones.end(), [currentTimezoneOffset](ClientSupportedTimezone const& left, ClientSupportedTimezone const& right)
175 {
176 Minutes leftDiff = left.first - currentTimezoneOffset;
177 Minutes rightDiff = right.first - currentTimezoneOffset;
178 return std::abs(leftDiff.count()) < std::abs(rightDiff.count());
179 });
180
181 return itr->second;
182}
183}
uint32_t uint32
Definition: Define.h:142
std::chrono::system_clock::time_point SystemTimePoint
Definition: Duration.h:42
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition: Duration.h:32
std::chrono::minutes Minutes
Minutes shorthand typedef.
Definition: Duration.h:35
tm TimeBreakdown(time_t time)
Definition: Util.cpp:93
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition: MapUtils.h:29
TC_COMMON_API std::locale const & GetCalendarLocale()
Definition: Locales.cpp:48
Minutes GetOffsetByHash(uint32 hash)
Definition: Timezone.cpp:124
std::string_view FindClosestClientSupportedTimezone(std::string_view currentTimezone, Minutes currentTimezoneOffset)
Definition: Timezone.cpp:163
Minutes GetSystemZoneOffset(bool applyDst)
Definition: Timezone.cpp:144
std::string GetSystemZoneName()
Definition: Timezone.cpp:153
Minutes GetSystemZoneOffsetAt(SystemTimePoint date)
Definition: Timezone.cpp:132
std::string ToString(Type &&val, Params &&... params)
std::uint32_t HashFnv1a(std::string_view data)
Definition: Hash.h:33