From a8e6282ff5a6a6dfdb98bdbf74dc9ed5a911d991 Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 6 Apr 2021 19:10:19 +0200 Subject: Core/PacketIO: Add new time related utility classes for conversion from time_t/std::chrono types --- src/server/game/Server/Packets/PacketUtilities.h | 82 ++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'src/server') diff --git a/src/server/game/Server/Packets/PacketUtilities.h b/src/server/game/Server/Packets/PacketUtilities.h index 327c42923a2..522e91c43d6 100644 --- a/src/server/game/Server/Packets/PacketUtilities.h +++ b/src/server/game/Server/Packets/PacketUtilities.h @@ -19,7 +19,9 @@ #define PacketUtilities_h__ #include "ByteBuffer.h" +#include "Duration.h" #include +#include namespace WorldPackets { @@ -100,6 +102,86 @@ namespace WorldPackets private: storage_type _storage; }; + + template + class Timestamp + { + public: + Timestamp() = default; + Timestamp(time_t value) : _value(value) { } + Timestamp(std::chrono::system_clock::time_point const& systemTime) : _value(std::chrono::system_clock::to_time_t(systemTime)) { } + + Timestamp& operator=(time_t value) + { + _value = value; + return *this; + } + + Timestamp& operator=(std::chrono::system_clock::time_point const& systemTime) + { + _value = std::chrono::system_clock::to_time_t(systemTime); + return *this; + } + + operator time_t() const + { + return _value; + } + + Underlying AsUnderlyingType() const + { + return static_cast(_value); + } + + friend ByteBuffer& operator<<(ByteBuffer& data, Timestamp timestamp) + { + data << static_cast(timestamp._value); + return data; + } + + friend ByteBuffer& operator>>(ByteBuffer& data, Timestamp& timestamp) + { + timestamp._value = data.read(); + return data; + } + + private: + time_t _value = time_t(0); + }; + + template + class Duration + { + public: + Duration() = default; + Duration(ChronoDuration value) : _value(value) { } + + Duration& operator=(ChronoDuration value) + { + _value = value; + return *this; + } + + operator ChronoDuration() const + { + return _value; + } + + friend ByteBuffer& operator<<(ByteBuffer& data, Duration duration) + { + data << static_cast(duration._value.count()); + return data; + } + + friend ByteBuffer& operator>>(ByteBuffer& data, Duration& duration) + { + duration._value = ChronoDuration(data.read()); + return data; + } + + private: + ChronoDuration _value = ChronoDuration::zero(); + }; } #endif // PacketUtilities_h__ -- cgit v1.2.3