diff options
author | Shauren <shauren.trinity@gmail.com> | 2021-04-06 19:10:19 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2021-04-06 19:10:19 +0200 |
commit | a8e6282ff5a6a6dfdb98bdbf74dc9ed5a911d991 (patch) | |
tree | c01e9cbcb0feeda58e0dfbaaa8ecf525872d6b5f /src | |
parent | 5faecb6c67c3b34e237ae7a645bed7359f025fd7 (diff) |
Core/PacketIO: Add new time related utility classes for conversion from time_t/std::chrono types
Diffstat (limited to 'src')
-rw-r--r-- | src/server/game/Server/Packets/PacketUtilities.h | 82 |
1 files changed, 82 insertions, 0 deletions
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 <boost/container/static_vector.hpp> +#include <ctime> namespace WorldPackets { @@ -100,6 +102,86 @@ namespace WorldPackets private: storage_type _storage; }; + + template<typename Underlying = uint32> + 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<Underlying>(_value); + } + + friend ByteBuffer& operator<<(ByteBuffer& data, Timestamp timestamp) + { + data << static_cast<Underlying>(timestamp._value); + return data; + } + + friend ByteBuffer& operator>>(ByteBuffer& data, Timestamp& timestamp) + { + timestamp._value = data.read<time_t, Underlying>(); + return data; + } + + private: + time_t _value = time_t(0); + }; + + template<typename ChronoDuration, typename Underlying = uint32> + 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<Underlying>(duration._value.count()); + return data; + } + + friend ByteBuffer& operator>>(ByteBuffer& data, Duration& duration) + { + duration._value = ChronoDuration(data.read<Underlying>()); + return data; + } + + private: + ChronoDuration _value = ChronoDuration::zero(); + }; } #endif // PacketUtilities_h__ |