mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-25 22:56:24 +00:00
feat(Core/Time): remove inherited ACE Time (#3455)
Co-authored-by: Viste <viste02@gmail.com>
This commit is contained in:
33
src/common/Utilities/Duration.h
Normal file
33
src/common/Utilities/Duration.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-GPL2
|
||||
* Copyright (C) 2008-2020 TrinityCore <http://www.trinitycore.org/>
|
||||
*/
|
||||
|
||||
#ifndef _DURATION_H_
|
||||
#define _DURATION_H_
|
||||
|
||||
#include <chrono>
|
||||
|
||||
/// Microseconds shorthand typedef.
|
||||
typedef std::chrono::microseconds Microseconds;
|
||||
|
||||
/// Milliseconds shorthand typedef.
|
||||
typedef std::chrono::milliseconds Milliseconds;
|
||||
|
||||
/// Seconds shorthand typedef.
|
||||
typedef std::chrono::seconds Seconds;
|
||||
|
||||
/// Minutes shorthand typedef.
|
||||
typedef std::chrono::minutes Minutes;
|
||||
|
||||
/// Hours shorthand typedef.
|
||||
typedef std::chrono::hours Hours;
|
||||
|
||||
/// time_point shorthand typedefs
|
||||
typedef std::chrono::steady_clock::time_point TimePoint;
|
||||
typedef std::chrono::system_clock::time_point SystemTimePoint;
|
||||
|
||||
/// Makes std::chrono_literals globally available.
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
#endif
|
||||
@@ -7,13 +7,23 @@
|
||||
#ifndef ACORE_TIMER_H
|
||||
#define ACORE_TIMER_H
|
||||
|
||||
#include "ace/OS_NS_sys_time.h"
|
||||
#include "Common.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()
|
||||
{
|
||||
static const ACE_Time_Value ApplicationStartTime = ACE_OS::gettimeofday();
|
||||
return (ACE_OS::gettimeofday() - ApplicationStartTime).msec();
|
||||
using namespace std::chrono;
|
||||
|
||||
return uint32(duration_cast<milliseconds>(steady_clock::now() - GetApplicationStartTime()).count());
|
||||
}
|
||||
|
||||
inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
|
||||
@@ -25,6 +35,14 @@ inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
|
||||
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());
|
||||
|
||||
@@ -102,6 +102,45 @@ Tokenizer::Tokenizer(const std::string &src, const char sep, uint32 vectorReserv
|
||||
}
|
||||
}
|
||||
|
||||
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
|
||||
struct tm* localtime_r(time_t const* time, struct tm* result)
|
||||
{
|
||||
localtime_s(result, time);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
tm TimeBreakdown(time_t time)
|
||||
{
|
||||
tm timeLocal;
|
||||
localtime_r(&time, &timeLocal);
|
||||
return timeLocal;
|
||||
}
|
||||
|
||||
time_t LocalTimeToUTCTime(time_t time)
|
||||
{
|
||||
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
|
||||
return time + _timezone;
|
||||
#else
|
||||
return time + timezone;
|
||||
#endif
|
||||
}
|
||||
|
||||
time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime)
|
||||
{
|
||||
tm timeLocal = TimeBreakdown(time);
|
||||
timeLocal.tm_hour = 0;
|
||||
timeLocal.tm_min = 0;
|
||||
timeLocal.tm_sec = 0;
|
||||
time_t midnightLocal = mktime(&timeLocal);
|
||||
time_t hourLocal = midnightLocal + hour * HOUR;
|
||||
|
||||
if (onlyAfterTime && hourLocal <= time)
|
||||
hourLocal += DAY;
|
||||
|
||||
return hourLocal;
|
||||
}
|
||||
|
||||
void stripLineInvisibleChars(std::string &str)
|
||||
{
|
||||
static std::string const invChars = " \t\7\n";
|
||||
@@ -227,7 +266,7 @@ uint32 TimeStringToSecs(const std::string& timestring)
|
||||
std::string TimeToTimestampStr(time_t t)
|
||||
{
|
||||
tm aTm;
|
||||
ACE_OS::localtime_r(&t, &aTm);
|
||||
localtime_r(&t, &aTm);
|
||||
// YYYY year
|
||||
// MM month (2 digits 01-12)
|
||||
// DD day (2 digits 01-31)
|
||||
@@ -245,6 +284,15 @@ std::string TimeToTimestampStr(time_t t)
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
std::string TimeToHumanReadable(time_t t)
|
||||
{
|
||||
tm time;
|
||||
localtime_r(&t, &time);
|
||||
char buf[30];
|
||||
strftime(buf, 30, "%c", &time);
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
/// Check if the string is a valid ip address representation
|
||||
bool IsIPAddress(char const* ipaddress)
|
||||
{
|
||||
|
||||
@@ -56,6 +56,11 @@ private:
|
||||
StorageType m_storage;
|
||||
};
|
||||
|
||||
struct tm* localtime_r(time_t const* time, struct tm* result);
|
||||
time_t LocalTimeToUTCTime(time_t time);
|
||||
time_t GetLocalHourTimestamp(time_t time, uint8 hour, bool onlyAfterTime = true);
|
||||
tm TimeBreakdown(time_t t);
|
||||
|
||||
void stripLineInvisibleChars(std::string &src);
|
||||
|
||||
int32 MoneyStringToMoney(const std::string& moneyString);
|
||||
@@ -63,6 +68,7 @@ int32 MoneyStringToMoney(const std::string& moneyString);
|
||||
std::string secsToTimeString(uint64 timeInSecs, bool shortText = false);
|
||||
uint32 TimeStringToSecs(const std::string& timestring);
|
||||
std::string TimeToTimestampStr(time_t t);
|
||||
std::string TimeToHumanReadable(time_t t);
|
||||
|
||||
/* Return a random number in the range min..max. */
|
||||
int32 irand(int32 min, int32 max);
|
||||
|
||||
Reference in New Issue
Block a user