diff options
Diffstat (limited to 'src/common/Utilities')
-rw-r--r-- | src/common/Utilities/Hash.h | 12 | ||||
-rw-r--r-- | src/common/Utilities/Timer.h | 190 | ||||
-rw-r--r-- | src/common/Utilities/Util.cpp | 24 | ||||
-rw-r--r-- | src/common/Utilities/Util.h | 3 |
4 files changed, 26 insertions, 203 deletions
diff --git a/src/common/Utilities/Hash.h b/src/common/Utilities/Hash.h index b63201bf07a..18f7442fb44 100644 --- a/src/common/Utilities/Hash.h +++ b/src/common/Utilities/Hash.h @@ -19,6 +19,7 @@ #define TrinityCore_Hash_h__ #include <functional> +#include <string_view> #include <utility> namespace Trinity @@ -28,6 +29,17 @@ namespace Trinity { seed ^= std::hash<T>()(val) + 0x9E3779B9 + (seed << 6) + (seed >> 2); } + + inline std::uint32_t HashFnv1a(std::string_view data) + { + std::uint32_t hash = 0x811C9DC5u; + for (char c : data) + { + hash ^= c; + hash *= 0x1000193u; + } + return hash; + } } //! Hash implementation for std::pair to allow using pairs in unordered_set or as key for unordered_map diff --git a/src/common/Utilities/Timer.h b/src/common/Utilities/Timer.h deleted file mode 100644 index e0ecb5b2d13..00000000000 --- a/src/common/Utilities/Timer.h +++ /dev/null @@ -1,190 +0,0 @@ -/* - * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef TRINITY_TIMER_H -#define TRINITY_TIMER_H - -#include "Define.h" -#include "Duration.h" - -inline TimePoint GetApplicationStartTime() -{ - using namespace std::chrono; - - static const steady_clock::time_point ApplicationStartTime = steady_clock::now(); - - return ApplicationStartTime; -} - -inline uint32 getMSTime() -{ - using namespace std::chrono; - - return uint32(duration_cast<milliseconds>(steady_clock::now() - GetApplicationStartTime()).count()); -} - -inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime) -{ - // getMSTime() have limited data range and this is case when it overflow in this tick - if (oldMSTime > newMSTime) - return (0xFFFFFFFF - oldMSTime) + newMSTime; - else - return newMSTime - oldMSTime; -} - -inline uint32 getMSTimeDiff(uint32 oldMSTime, TimePoint newTime) -{ - using namespace std::chrono; - - uint32 newMSTime = uint32(duration_cast<milliseconds>(newTime - GetApplicationStartTime()).count()); - return getMSTimeDiff(oldMSTime, newMSTime); -} - -inline uint32 GetMSTimeDiffToNow(uint32 oldMSTime) -{ - return getMSTimeDiff(oldMSTime, getMSTime()); -} - -struct IntervalTimer -{ -public: - - IntervalTimer() - : _interval(0), _current(0) - { - } - - void Update(time_t diff) - { - _current += diff; - if (_current < 0) - _current = 0; - } - - bool Passed() - { - return _current >= _interval; - } - - void Reset() - { - if (_current >= _interval) - _current %= _interval; - } - - void SetCurrent(time_t current) - { - _current = current; - } - - void SetInterval(time_t interval) - { - _interval = interval; - } - - time_t GetInterval() const - { - return _interval; - } - - time_t GetCurrent() const - { - return _current; - } - -private: - - time_t _interval; - time_t _current; -}; - -struct TimeTracker -{ -public: - TimeTracker(int32 expiry = 0) : _expiryTime(expiry) { } - TimeTracker(Milliseconds expiry) : _expiryTime(expiry) { } - - void Update(int32 diff) - { - Update(Milliseconds(diff)); - } - - void Update(Milliseconds diff) - { - _expiryTime -= diff; - } - - bool Passed() const - { - return _expiryTime <= 0s; - } - - void Reset(int32 expiry) - { - Reset(Milliseconds(expiry)); - } - - void Reset(Milliseconds expiry) - { - _expiryTime = expiry; - } - - Milliseconds GetExpiry() const - { - return _expiryTime; - } - -private: - Milliseconds _expiryTime; -}; - -struct PeriodicTimer -{ -public: - - PeriodicTimer(int32 period, int32 start_time) - : i_period(period), i_expireTime(start_time) - { - } - - bool Update(const uint32 diff) - { - if ((i_expireTime -= diff) > 0) - return false; - - i_expireTime += i_period > int32(diff) ? i_period : diff; - return true; - } - - void SetPeriodic(int32 period, int32 start_time) - { - i_expireTime = start_time; - i_period = period; - } - - // Tracker interface - void TUpdate(int32 diff) { i_expireTime -= diff; } - bool TPassed() const { return i_expireTime <= 0; } - void TReset(int32 diff, int32 period) { i_expireTime += period > diff ? period : diff; } - -private: - - int32 i_period; - int32 i_expireTime; -}; - -#endif diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index ac97e724902..d852ed655a0 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -58,9 +58,20 @@ std::vector<std::string_view> Trinity::Tokenize(std::string_view str, char sep, #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) struct tm* localtime_r(time_t const* time, struct tm *result) { - localtime_s(result, time); + if (localtime_s(result, time) != 0) + return nullptr; return result; } +struct tm* gmtime_r(time_t const* time, struct tm* result) +{ + if (gmtime_s(result, time) != 0) + return nullptr; + return result; +} +time_t timegm(struct tm* tm) +{ + return _mkgmtime(tm); +} #endif tm TimeBreakdown(time_t time) @@ -70,17 +81,6 @@ tm TimeBreakdown(time_t time) return timeLocal; } -time_t LocalTimeToUTCTime(time_t time) -{ -#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) - return time + _timezone; -#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) - return timegm(gmtime(&time)); -#else - return time + timezone; -#endif -} - time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime) { tm timeLocal = TimeBreakdown(time); diff --git a/src/common/Utilities/Util.h b/src/common/Utilities/Util.h index be4a68d7788..334b3ab6ac0 100644 --- a/src/common/Utilities/Util.h +++ b/src/common/Utilities/Util.h @@ -54,8 +54,9 @@ TC_COMMON_API Optional<int64> MoneyStringToMoney(std::string const& moneyString) #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) TC_COMMON_API struct tm* localtime_r(time_t const* time, struct tm *result); +TC_COMMON_API struct tm* gmtime_r(time_t const* time, struct tm *result); +TC_COMMON_API time_t timegm(struct tm* tm); #endif -TC_COMMON_API time_t LocalTimeToUTCTime(time_t time); TC_COMMON_API time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime = true); TC_COMMON_API tm TimeBreakdown(time_t t); |