TrinityCore
WowTime.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 "WowTime.h"
19#include "ByteBuffer.h"
20#include "Errors.h"
21#include "Util.h"
22
24{
25 return ((_year % 100) & 0x1F) << 24
26 | (_month & 0xF) << 20
27 | (_monthDay & 0x3F) << 14
28 | (_weekDay & 0x7) << 11
29 | (_hour & 0x1F) << 6
30 | (_minute & 0x3F)
31 | (_flags & 0x3) << 29;
32}
33
35{
36 _year = (packedTime >> 24) & 0x1F;
37 if (_year == 31)
38 _year = -1;
39
40 _month = (packedTime >> 20) & 0xF;
41 if (_month == 15)
42 _month = -1;
43
44 _monthDay = (packedTime >> 14) & 0x3F;
45 if (_monthDay == 63)
46 _monthDay = -1;
47
48 _weekDay = (packedTime >> 11) & 0x7;
49 if (_weekDay == 7)
50 _weekDay = -1;
51
52 _hour = (packedTime >> 6) & 0x1F;
53 if (_hour == 31)
54 _hour = -1;
55
56 _minute = packedTime & 0x3F;
57 if (_minute == 63)
58 _minute = -1;
59
60 _flags = (packedTime >> 29) & 0x3;
61 if (_flags == 3)
62 _flags = -1;
63}
64
66{
67 if (_year < 0 || _month < 0 || _monthDay < 0)
68 return 0;
69
70 std::tm buf{};
71 buf.tm_year = _year + 100;
72 buf.tm_mon = _month;
73 buf.tm_mday = _monthDay + 1;
74 if (_hour >= 0)
75 {
76 buf.tm_hour = _hour;
77 if (_minute >= 0)
78 buf.tm_min = _minute;
79 }
80 buf.tm_isdst = -1;
81 buf.tm_wday = _weekDay;
82
83 return timegm(&buf);
84}
85
86void WowTime::SetUtcTimeFromUnixTime(std::time_t unixTime)
87{
88 std::tm buf;
89 if (!::gmtime_r(&unixTime, &buf))
90 return;
91
92 _year = (buf.tm_year - 100) % 100;
93 _month = buf.tm_mon;
94 _monthDay = buf.tm_mday - 1;
95 _weekDay = buf.tm_wday;
96 _hour = buf.tm_hour;
97 _minute = buf.tm_min;
98}
99
101{
102 ASSERT(year == -1 || (year >= 0 && year < 32));
103 _year = year;
104}
105
107{
108 ASSERT(month == -1 || (month >= 0 && month < 12));
109 _month = month;
110}
111
113{
114 ASSERT(monthDay == -1 || (monthDay >= 0 && monthDay < 32));
115 _monthDay = monthDay;
116}
117
119{
120 ASSERT(weekDay == -1 || (weekDay >= 0 && weekDay < 7));
121 _weekDay = weekDay;
122}
123
125{
126 ASSERT(hour == -1 || (hour >= 0 && hour < 24));
127 _hour = hour;
128}
129
131{
132 ASSERT(minute == -1 || (minute >= 0 && minute < 60));
133 _minute = minute;
134}
135
137{
138 ASSERT(flags == -1 || (flags >= 0 && flags < 3));
139 _flags = flags;
140}
141
142std::strong_ordering operator<=>(WowTime const& left, WowTime const& right)
143{
144 auto compareFieldIfSet = [&]<typename T>(T WowTime::*field) -> std::strong_ordering
145 {
146 if (left.*field < 0 || right.*field < 0)
147 return std::strong_ordering::equal;
148
149 return left.*field <=> right.*field;
150 };
151
152 if (std::strong_ordering cmp = compareFieldIfSet(&WowTime::_year); advstd::is_neq(cmp))
153 return cmp;
154
155 if (std::strong_ordering cmp = compareFieldIfSet(&WowTime::_month); advstd::is_neq(cmp))
156 return cmp;
157
158 if (std::strong_ordering cmp = compareFieldIfSet(&WowTime::_monthDay); advstd::is_neq(cmp))
159 return cmp;
160
161 if (std::strong_ordering cmp = compareFieldIfSet(&WowTime::_weekDay); advstd::is_neq(cmp))
162 return cmp;
163
164 if (std::strong_ordering cmp = compareFieldIfSet(&WowTime::_year); advstd::is_neq(cmp))
165 return cmp;
166
167 if (std::strong_ordering cmp = compareFieldIfSet(&WowTime::_hour); advstd::is_neq(cmp))
168 return cmp;
169
170 return std::strong_ordering::equal;
171}
172
173bool WowTime::IsInRange(WowTime const& from, WowTime const& to) const
174{
175 if (from > to)
176 return *this >= from || *this < to;
177
178 return *this >= from && *this < to;
179}
180
182{
183 time_t unixTime = GetUnixTimeFromUtcTime();
184 unixTime += seconds.count();
185 SetUtcTimeFromUnixTime(unixTime);
186 return *this;
187}
188
190{
191 return WowTime(*this) += seconds;
192}
193
195{
196 time_t unixTime = GetUnixTimeFromUtcTime();
197 unixTime -= seconds.count();
198 SetUtcTimeFromUnixTime(unixTime);
199 return *this;
200}
201
203{
204 return WowTime(*this) -= seconds;
205}
206
208{
209 data << uint32(wowTime.GetPackedTime());
210 return data;
211}
212
214{
215 uint32 packedTime = 0;
216 data >> packedTime;
217 wowTime.SetPackedTime(packedTime);
218 return data;
219}
int8_t int8
Definition: Define.h:140
int32_t int32
Definition: Define.h:138
uint32_t uint32
Definition: Define.h:142
uint16 flags
Definition: DisableMgr.cpp:49
std::chrono::seconds Seconds
Seconds shorthand typedef.
Definition: Duration.h:32
#define ASSERT
Definition: Errors.h:68
ByteBuffer & operator>>(ByteBuffer &data, WowTime &wowTime)
Definition: WowTime.cpp:213
ByteBuffer & operator<<(ByteBuffer &data, WowTime const &wowTime)
Definition: WowTime.cpp:207
std::strong_ordering operator<=>(WowTime const &left, WowTime const &right)
Definition: WowTime.cpp:142
void SetPackedTime(uint32 packedTime)
Definition: WowTime.cpp:34
std::time_t GetUnixTimeFromUtcTime() const
Definition: WowTime.cpp:65
void SetYear(int32 year)
Definition: WowTime.cpp:100
int8 _flags
Definition: WowTime.h:86
void SetHour(int8 hour)
Definition: WowTime.cpp:124
int8 _hour
Definition: WowTime.h:84
void SetWeekDay(int8 weekDay)
Definition: WowTime.cpp:118
int8 _weekDay
Definition: WowTime.h:83
int32 _year
Definition: WowTime.h:80
WowTime & operator+=(Seconds seconds)
Definition: WowTime.cpp:181
void SetMonth(int8 month)
Definition: WowTime.cpp:106
WowTime operator-(Seconds seconds) const
Definition: WowTime.cpp:202
void SetMonthDay(int8 monthDay)
Definition: WowTime.cpp:112
int8 _monthDay
Definition: WowTime.h:82
WowTime & operator-=(Seconds seconds)
Definition: WowTime.cpp:194
uint32 GetPackedTime() const
Definition: WowTime.cpp:23
void SetFlags(int8 flags)
Definition: WowTime.cpp:136
void SetMinute(int8 minute)
Definition: WowTime.cpp:130
WowTime operator+(Seconds seconds) const
Definition: WowTime.cpp:189
int8 _month
Definition: WowTime.h:81
void SetUtcTimeFromUnixTime(std::time_t unixTime)
Definition: WowTime.cpp:86
bool IsInRange(WowTime const &from, WowTime const &to) const
Definition: WowTime.cpp:173
int8 _minute
Definition: WowTime.h:85
constexpr bool is_neq(std::partial_ordering cmp) noexcept
Definition: advstd.h:35