TrinityCore
Loading...
Searching...
No Matches
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 "MapUtils.h"
21#include <algorithm>
22#include <array>
23#include <unordered_map>
24
25#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
26#include "StringConvert.h"
27#else
28#include "Locales.h"
29#include "Util.h"
30#include <boost/locale/date_time_facet.hpp>
31#include <memory>
32#endif
33
34namespace
35{
36std::unordered_map<uint32, Minutes, std::identity> InitTimezoneHashDb()
37{
38#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
39
40 // Generate our hash db to match values sent in client authentication packets
41 std::unordered_map<uint32, Minutes, std::identity> hashToOffset;
42 std::chrono::system_clock::time_point dummmy;
43 for (std::chrono::time_zone const& zone : std::chrono::get_tzdb().zones)
44 {
45 std::chrono::sys_info sysInfo = zone.get_info(dummmy);
46 Minutes offsetMinutes = std::chrono::duration_cast<Minutes>(sysInfo.offset);
47 std::string offsetStr = Trinity::ToString(offsetMinutes.count());
48 hashToOffset.emplace(Trinity::HashFnv1a<uint32>::GetHash(offsetStr), offsetMinutes);
49 }
50
51#else
52 // Pre-generated list of timezone offsets and their hashes for compilers (and their stl implementations) that dont support timezone api yet
53 std::unordered_map<uint32, Minutes, std::identity> hashToOffset =
54 {
55 { 0xAADC2D37u, -720min },
56 { 0x362F107Bu, -690min },
57 { 0x2C44C70Cu, -660min },
58 { 0xB84A209Eu, -640min },
59 { 0xBA3D57D1u, -630min },
60 { 0x4040695Au, -600min },
61 { 0xB65A75D0u, -570min },
62 { 0xC8614DEBu, -540min },
63 { 0x3A68BD26u, -510min },
64 { 0x51E8096Cu, -480min },
65 { 0x4DD8F896u, -420min },
66 { 0x674B7C0Fu, -360min },
67 { 0x633C6B39u, -300min },
68 { 0x0BAD340Au, -240min },
69 { 0x74B25683u, -225min },
70 { 0x09B9FCD7u, -210min },
71 { 0x150C169Bu, -180min },
72 { 0x191B2771u, -120min },
73 { 0xD7D3B14Eu, -60min },
74 { 0x47CE5170u, -44min },
75 { 0x350CA8AFu, 0min },
76 { 0x15E8E23Bu, 60min },
77 { 0x733864AEu, 120min },
78 { 0xF71F9C94u, 180min },
79 { 0xBDE50F54u, 210min },
80 { 0x2BDD6DB9u, 240min },
81 { 0xB1E07F42u, 270min },
82 { 0x454FF132u, 300min },
83 { 0x3F4DA929u, 330min },
84 { 0xD1554AC4u, 360min },
85 { 0xBB667143u, 390min },
86 { 0x9E2B78C9u, 420min },
87 { 0x1C377816u, 450min },
88 { 0x1A4440E3u, 480min },
89 { 0xB49DF789u, 525min },
90 { 0xC3A28C54u, 540min },
91 { 0x35A9FB8Fu, 570min },
92 { 0x889BD751u, 600min },
93 { 0x8CAAE827u, 660min },
94 { 0x7285EE60u, 690min },
95 { 0x1CC2DEF4u, 720min },
96 { 0x89B8FD2Fu, 765min },
97 { 0x98DBA70Eu, 780min },
98 { 0xC59585BBu, 840min }
99 };
100#endif
101
102 return hashToOffset;
103}
104
105std::unordered_map<uint32, Minutes, std::identity> const& GetTimezoneOffsetsByHash()
106{
107 static std::unordered_map<uint32, Minutes, std::identity> timezoneMap = InitTimezoneHashDb();
108 return timezoneMap;
109}
110
111using ClientSupportedTimezone = std::pair<Minutes, std::string>;
112std::array<ClientSupportedTimezone, 11> const _clientSupportedTimezones =
113{{
114 { -480min, "America/Los_Angeles" },
115 { -420min, "America/Denver" },
116 { -360min, "America/Chicago" },
117 { -300min, "America/New_York" },
118 { -180min, "America/Sao_Paulo" },
119 { 0min, "Etc/UTC" },
120 { 60min, "Europe/Paris" },
121 { 480min, "Asia/Shanghai" },
122 { 480min, "Asia/Taipei" },
123 { 540min, "Asia/Seoul" },
124 { 600min, "Australia/Melbourne" },
125}};
126}
127
129{
131{
132 if (Minutes const* offset = Containers::MapGetValuePtr(GetTimezoneOffsetsByHash(), hash))
133 return *offset;
134
135 return 0min;
136}
137
139{
140 Seconds offset;
141#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
142 offset = std::chrono::current_zone()->get_info(date).offset;
143#else
144 tm buf = TimeBreakdown(std::chrono::system_clock::to_time_t(date));
145 offset = Seconds(buf.tm_gmtoff);
146#endif
147 return std::chrono::duration_cast<Minutes>(offset);
148}
149
150Minutes GetSystemZoneOffset(bool applyDst /*= true*/)
151{
152 std::chrono::system_clock::time_point date = std::chrono::system_clock::from_time_t(std::time_t(0));
153 if (applyDst)
154 date = std::chrono::system_clock::now();
155
156 return GetSystemZoneOffsetAt(date);
157}
158
159std::string GetSystemZoneName()
160{
161#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS
162 return std::string(std::chrono::current_zone()->name());
163#else
164 std::unique_ptr<boost::locale::abstract_calendar> p(std::use_facet<class boost::locale::calendar_facet>(Locale::GetCalendarLocale()).create_calendar());
165 return p->get_timezone();
166#endif
167}
168
169std::string_view FindClosestClientSupportedTimezone(std::string_view currentTimezone, Minutes currentTimezoneOffset)
170{
171 // try exact match
172 auto itr = std::ranges::find(_clientSupportedTimezones, currentTimezone, Trinity::Containers::MapValue);
173 if (itr != _clientSupportedTimezones.end())
174 return itr->second;
175
176 // try closest offset
177 itr = std::ranges::min_element(_clientSupportedTimezones, std::ranges::less(),
178 [currentTimezoneOffset](ClientSupportedTimezone const& ctz) { return std::chrono::abs(ctz.first - currentTimezoneOffset); });
179
180 return itr->second;
181}
182}
uint32_t uint32
Definition Define.h:154
std::chrono::system_clock::time_point SystemTimePoint
Definition Duration.h:41
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition Duration.h:28
std::chrono::minutes Minutes
Minutes shorthand typedef.
Definition Duration.h:32
tm TimeBreakdown(time_t time)
Definition Util.cpp:94
constexpr auto MapValue
Definition MapUtils.h:77
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition MapUtils.h:37
TC_COMMON_API std::locale const & GetCalendarLocale()
Definition Locales.cpp:48
Minutes GetOffsetByHash(uint32 hash)
Definition Timezone.cpp:130
std::string_view FindClosestClientSupportedTimezone(std::string_view currentTimezone, Minutes currentTimezoneOffset)
Definition Timezone.cpp:169
Minutes GetSystemZoneOffset(bool applyDst)
Definition Timezone.cpp:150
std::string GetSystemZoneName()
Definition Timezone.cpp:159
Minutes GetSystemZoneOffsetAt(SystemTimePoint date)
Definition Timezone.cpp:138
std::string ToString(Type &&val, Params &&... params)
STL namespace.