plugify 1.0.0.0
Loading...
Searching...
No Matches
date_time.hpp
1#pragma once
2
3#include <chrono>
4#include <sstream>
5#include <iomanip>
6#include <cmath>
7
8namespace plugify {
9 using namespace std::chrono_literals;
10
11 class DateTime {
12 public:
13 DateTime() = default;
14
21 template<typename Rep, typename Period>
22 constexpr DateTime(const std::chrono::duration<Rep, Period>& duration) noexcept : _value{std::chrono::duration_cast<std::chrono::microseconds>(duration).count()} {}
23
30 template<typename T = float>
31 constexpr static DateTime Seconds(const T& seconds) noexcept { return {std::chrono::duration<T>(seconds)}; }
32
39 template<typename T = double>
40 constexpr static DateTime Milliseconds(const T& milliseconds) noexcept { return {std::chrono::duration<T, std::micro>(milliseconds)}; }
41
48 template<typename T = uint64_t>
49 constexpr static DateTime Microseconds(const T& microseconds) noexcept { return {std::chrono::duration<T, std::micro>(microseconds)}; }
50
56 template<typename T = float>
57 constexpr auto AsSeconds() const noexcept { return static_cast<T>(_value.count()) / static_cast<T>(1000000); }
58
64 template<typename T = double>
65 constexpr auto AsMilliseconds() const noexcept { return static_cast<T>(_value.count()) / static_cast<T>(1000); }
66
72 template<typename T = uint64_t>
73 constexpr auto AsMicroseconds() const noexcept { return static_cast<T>(_value.count()); }
74
79 static DateTime Now() noexcept {
80 static const auto localEpoch = std::chrono::high_resolution_clock::now();
81 return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - localEpoch);
82 }
83
89 static std::string Get(std::string_view format = "%Y-%m-%d %H:%M:%S") {
90 auto now = std::chrono::system_clock::now();
91 auto timeT = std::chrono::system_clock::to_time_t(now);
92 std::tm localTime{};
93#if _WIN32
94 localtime_s(&localTime, &timeT); // Windows-specific
95#else
96 localtime_r(&timeT, &localTime); // POSIX-compliant
97#endif
98 std::stringstream ss;
99 ss << std::put_time(&localTime, format.data());
100 return ss.str();
101 }
102
109 template<typename Rep, typename Period>
110 constexpr explicit operator std::chrono::duration<Rep, Period>() const noexcept {
111 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(_value);
112 }
113
114 //constexpr auto operator<=>(const DateTime& rhs) const { return _value <=> rhs._value; }
120 constexpr bool operator==(const DateTime& rhs) const noexcept { return _value == rhs._value; }
126 constexpr bool operator!=(const DateTime& rhs) const noexcept { return _value != rhs._value; }
132 constexpr bool operator<(const DateTime& rhs) const noexcept { return _value < rhs._value; }
138 constexpr bool operator<=(const DateTime& rhs) const noexcept { return _value <= rhs._value; }
144 constexpr bool operator>(const DateTime& rhs) const noexcept { return _value > rhs._value; }
150 constexpr bool operator>=(const DateTime& rhs) const noexcept { return _value >= rhs._value; }
155 constexpr DateTime operator-() const noexcept { return { -_value}; }
156
163 constexpr friend DateTime operator+(const DateTime& lhs, const DateTime& rhs) noexcept { return lhs._value + rhs._value; }
170 constexpr friend DateTime operator-(const DateTime& lhs, const DateTime& rhs) noexcept { return lhs._value - rhs._value; }
177 constexpr friend DateTime operator*(const DateTime& lhs, float rhs) noexcept { return lhs._value * rhs; }
184 constexpr friend DateTime operator*(const DateTime& lhs, int64_t rhs) noexcept { return lhs._value * rhs; }
191 constexpr friend DateTime operator*(float lhs, const DateTime& rhs) noexcept { return rhs * lhs; }
198 constexpr friend DateTime operator*(int64_t lhs, const DateTime& rhs) noexcept { return rhs * lhs; }
205 constexpr friend DateTime operator/(const DateTime& lhs, float rhs) noexcept { return lhs._value / rhs; }
212 constexpr friend DateTime operator/(const DateTime& lhs, int64_t rhs) noexcept { return lhs._value / rhs; }
219 constexpr friend double operator/(const DateTime& lhs, const DateTime& rhs) noexcept { return static_cast<double>(lhs._value.count()) / static_cast<double>(rhs._value.count()); }
220
228 template<typename Period = std::ratio<1, 1>>
229 constexpr friend double operator%(const DateTime& lhs, const DateTime& rhs) {
230 return std::modf(std::chrono::duration_cast<std::chrono::duration<double, Period>>(lhs._value), std::chrono::duration_cast<std::chrono::duration<double, Period>>(rhs._value));
231 }
232
238 constexpr DateTime& operator+=(const DateTime& rhs) noexcept { return *this = *this + rhs; }
244 constexpr DateTime& operator-=(const DateTime& rhs) noexcept { return *this = *this - rhs; }
250 constexpr DateTime& operator*=(float rhs) noexcept { return *this = *this * rhs; }
256 constexpr DateTime& operator*=(int64_t rhs) noexcept{ return *this = *this * rhs; }
262 constexpr DateTime& operator/=(float rhs) noexcept { return *this = *this / rhs; }
268 constexpr DateTime& operator/=(int64_t rhs) noexcept { return *this = *this / rhs; }
269
270 private:
271 std::chrono::microseconds _value{};
272 };
273}
constexpr friend DateTime operator*(const DateTime &lhs, int64_t rhs) noexcept
Multiplies a DateTime object by an integer value.
constexpr friend DateTime operator/(const DateTime &lhs, int64_t rhs) noexcept
Divides a DateTime object by an integer value.
constexpr friend DateTime operator/(const DateTime &lhs, float rhs) noexcept
Divides a DateTime object by a floating-point value.
constexpr bool operator>=(const DateTime &rhs) const noexcept
Compares if one DateTime object is greater than or equal to another.
constexpr auto AsMicroseconds() const noexcept
Converts the time duration to microseconds.
Definition date_time.hpp:73
static constexpr DateTime Microseconds(const T &microseconds) noexcept
Creates a DateTime object representing microseconds.
Definition date_time.hpp:49
static constexpr DateTime Milliseconds(const T &milliseconds) noexcept
Creates a DateTime object representing milliseconds.
Definition date_time.hpp:40
constexpr bool operator<=(const DateTime &rhs) const noexcept
Compares if one DateTime object is less than or equal to another.
constexpr auto AsSeconds() const noexcept
Converts the time duration to seconds.
Definition date_time.hpp:57
constexpr DateTime operator-() const noexcept
Negates the DateTime value.
constexpr DateTime & operator*=(float rhs) noexcept
Multiplies this DateTime object by a floating-point value.
constexpr DateTime(const std::chrono::duration< Rep, Period > &duration) noexcept
Constructs a DateTime object from a duration.
Definition date_time.hpp:22
constexpr friend DateTime operator+(const DateTime &lhs, const DateTime &rhs) noexcept
Adds two DateTime objects.
constexpr DateTime & operator+=(const DateTime &rhs) noexcept
Adds another DateTime object to this one.
constexpr friend double operator%(const DateTime &lhs, const DateTime &rhs)
Computes the modulo (remainder) of one DateTime object divided by another.
static std::string Get(std::string_view format="%Y-%m-%d %H:%M:%S")
Gets the current system time formatted as a string.
Definition date_time.hpp:89
constexpr DateTime & operator/=(float rhs) noexcept
Divides this DateTime object by a floating-point value.
constexpr bool operator<(const DateTime &rhs) const noexcept
Compares if one DateTime object is less than another.
constexpr DateTime & operator*=(int64_t rhs) noexcept
Multiplies this DateTime object by an integer value.
constexpr DateTime & operator/=(int64_t rhs) noexcept
Divides this DateTime object by an integer value.
static DateTime Now() noexcept
Gets the current time since a local epoch.
Definition date_time.hpp:79
static constexpr DateTime Seconds(const T &seconds) noexcept
Creates a DateTime object representing seconds.
Definition date_time.hpp:31
constexpr friend DateTime operator*(int64_t lhs, const DateTime &rhs) noexcept
Multiplies an integer value by a DateTime object.
constexpr bool operator!=(const DateTime &rhs) const noexcept
Compares if two DateTime objects are not equal.
constexpr friend double operator/(const DateTime &lhs, const DateTime &rhs) noexcept
Divides one DateTime object by another.
constexpr friend DateTime operator*(const DateTime &lhs, float rhs) noexcept
Multiplies a DateTime object by a floating-point value.
constexpr bool operator==(const DateTime &rhs) const noexcept
Compares if two DateTime objects are equal.
constexpr auto AsMilliseconds() const noexcept
Converts the time duration to milliseconds.
Definition date_time.hpp:65
constexpr DateTime & operator-=(const DateTime &rhs) noexcept
Subtracts another DateTime object from this one.
constexpr friend DateTime operator-(const DateTime &lhs, const DateTime &rhs) noexcept
Subtracts one DateTime object from another.
constexpr bool operator>(const DateTime &rhs) const noexcept
Compares if one DateTime object is greater than another.
constexpr friend DateTime operator*(float lhs, const DateTime &rhs) noexcept
Multiplies a floating-point value by a DateTime object.