mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-26 23:26:23 +00:00
feat(Core/Time): remove inherited ACE Time (#3455)
Co-authored-by: Viste <viste02@gmail.com>
This commit is contained in:
@@ -83,7 +83,6 @@
|
||||
#include <ace/Guard_T.h>
|
||||
#include <ace/RW_Thread_Mutex.h>
|
||||
#include <ace/Thread_Mutex.h>
|
||||
#include <ace/OS_NS_time.h>
|
||||
#include <ace/Stack_Trace.h>
|
||||
|
||||
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
|
||||
|
||||
@@ -4,18 +4,7 @@
|
||||
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
|
||||
*/
|
||||
|
||||
|
||||
#include "Common.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
#include <mysql.h>
|
||||
#include <mysqld_error.h>
|
||||
#include <errmsg.h>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "MySQLConnection.h"
|
||||
#include "MySQLThreading.h"
|
||||
#include "QueryResult.h"
|
||||
@@ -24,9 +13,15 @@
|
||||
#include "DatabaseWorker.h"
|
||||
#include "Timer.h"
|
||||
#include "Log.h"
|
||||
#include "Duration.h"
|
||||
#include <mysql.h>
|
||||
#include <mysqld_error.h>
|
||||
#include <errmsg.h>
|
||||
#include <thread>
|
||||
|
||||
using namespace std::this_thread;
|
||||
using namespace std::chrono;
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
MySQLConnection::MySQLConnection(MySQLConnectionInfo& connInfo) :
|
||||
m_reconnecting(false),
|
||||
@@ -113,7 +108,6 @@ bool MySQLConnection::Open()
|
||||
|
||||
// Possible improvement for future: make ATTEMPTS and SECONDS configurable values
|
||||
uint32 const ATTEMPTS = 180;
|
||||
uint32 const SECONDS = 10;
|
||||
|
||||
uint32 count = 0;
|
||||
do {
|
||||
@@ -153,7 +147,7 @@ bool MySQLConnection::Open()
|
||||
count++;
|
||||
sLog->outError("Could not connect to MySQL database at %s: %s\n", m_connectionInfo.host.c_str(), mysql_error(mysqlInit));
|
||||
sLog->outError("Retrying in 10 seconds...\n\n");
|
||||
sleep_for(seconds(SECONDS));
|
||||
std::this_thread::sleep_for(10s);
|
||||
}
|
||||
} while (!m_Mysql && count < ATTEMPTS);
|
||||
|
||||
@@ -537,7 +531,7 @@ bool MySQLConnection::_HandleMySQLErrno(uint32 errNo)
|
||||
}
|
||||
|
||||
uint32 lErrno = mysql_errno(GetHandle()); // It's possible this attempted reconnect throws 2006 at us. To prevent crazy recursive calls, sleep here.
|
||||
ACE_OS::sleep(3); // Sleep 3 seconds
|
||||
std::this_thread::sleep_for(3s); // Sleep 3 seconds
|
||||
return _HandleMySQLErrno(lErrno); // Call self (recursive)
|
||||
}
|
||||
|
||||
@@ -552,12 +546,12 @@ bool MySQLConnection::_HandleMySQLErrno(uint32 errNo)
|
||||
case ER_BAD_FIELD_ERROR:
|
||||
case ER_NO_SUCH_TABLE:
|
||||
sLog->outError("Your database structure is not up to date. Please make sure you've executed all queries in the sql/updates folders.");
|
||||
ACE_OS::sleep(10);
|
||||
std::this_thread::sleep_for(10s);
|
||||
std::abort();
|
||||
return false;
|
||||
case ER_PARSE_ERROR:
|
||||
sLog->outError("Error while parsing SQL. Core fix required.");
|
||||
ACE_OS::sleep(10);
|
||||
std::this_thread::sleep_for(10s);
|
||||
std::abort();
|
||||
return false;
|
||||
default:
|
||||
|
||||
@@ -340,7 +340,7 @@ std::string Log::GetTimestampStr()
|
||||
{
|
||||
time_t t = time(NULL);
|
||||
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)
|
||||
|
||||
@@ -9,16 +9,14 @@
|
||||
|
||||
#include "Common.h"
|
||||
#include "Errors.h"
|
||||
#include "Util.h"
|
||||
#include "ByteConverter.h"
|
||||
|
||||
#include <ace/OS_NS_time.h>
|
||||
#include <exception>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <time.h>
|
||||
|
||||
// Root of ByteBuffer exception hierarchy
|
||||
class ByteBufferException : public std::exception
|
||||
@@ -489,7 +487,7 @@ class ByteBuffer
|
||||
void AppendPackedTime(time_t time)
|
||||
{
|
||||
tm lt;
|
||||
ACE_OS::localtime_r(&time, <);
|
||||
localtime_r(&time, <);
|
||||
append<uint32>((lt.tm_year - 100) << 24 | lt.tm_mon << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min);
|
||||
}
|
||||
|
||||
|
||||
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