8 using namespace std::chrono_literals;
20 template<
typename Rep,
typename Period>
21 constexpr DateTime(
const std::chrono::duration<Rep, Period>& duration) noexcept : _value{std::chrono::duration_cast<std::chrono::microseconds>(duration).count()} {}
29 template<
typename T =
float>
30 constexpr static DateTime Seconds(
const T& seconds)
noexcept {
return {std::chrono::duration<T>(seconds)}; }
38 template<
typename T =
double>
39 constexpr static DateTime Milliseconds(
const T& milliseconds)
noexcept {
return {std::chrono::duration<T, std::micro>(milliseconds)}; }
47 template<
typename T = u
int64_t>
48 constexpr static DateTime Microseconds(
const T& microseconds)
noexcept {
return {std::chrono::duration<T, std::micro>(microseconds)}; }
55 template<
typename T =
float>
56 constexpr auto AsSeconds() const noexcept {
return static_cast<T
>(_value.count()) /
static_cast<T
>(1000000); }
63 template<
typename T =
double>
64 constexpr auto AsMilliseconds() const noexcept {
return static_cast<T
>(_value.count()) /
static_cast<T
>(1000); }
71 template<
typename T = u
int64_t>
72 constexpr auto AsMicroseconds() const noexcept {
return static_cast<T
>(_value.count()); }
79 return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - localEpoch);
87 static std::string
Get(std::string_view format =
"%Y-%m-%d %H:%M:%S") {
88 auto now = std::chrono::system_clock::now();
89 auto t = std::chrono::system_clock::to_time_t(now);
92 localtime_s(&time, &t);
94 localtime_r(&t, &time);
96 std::string buffer(80,
'\0');
97 size_t res = std::strftime(buffer.data(), buffer.size(), format.data(), &time);
99 return "strftime error";
110 template<
typename Rep,
typename Period>
111 constexpr explicit operator std::chrono::duration<Rep, Period>() const noexcept {
112 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(_value);
220 constexpr friend double operator/(
const DateTime& lhs,
const DateTime& rhs)
noexcept {
return static_cast<double>(lhs._value.count()) /
static_cast<double>(rhs._value.count()); }
229 template<
typename Period = std::ratio<1, 1>>
231 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));
272 std::chrono::microseconds _value{};
274 static inline const std::chrono::high_resolution_clock::time_point localEpoch = std::chrono::high_resolution_clock::now();
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.
static constexpr DateTime Microseconds(const T µseconds) noexcept
Creates a DateTime object representing microseconds.
static constexpr DateTime Milliseconds(const T &milliseconds) noexcept
Creates a DateTime object representing milliseconds.
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.
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.
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.
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.
static constexpr DateTime Seconds(const T &seconds) noexcept
Creates a DateTime object representing seconds.
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.
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.