MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::calendars::Segments Struct Reference

Represents time in segments (hour, minute, etc.). More...

#include <time.hpp>

Public Member Functions

constexpr Result< void, TimeValidationErrortry_validate () const
 Tries to validate.
 
constexpr operator NanoSeconds () const
 
constexpr operator DayFraction () const
 
constexpr auto operator<=> (const Segments &o) const
 
bool operator== (const Segments &o) const
 
bool operator!= (const Segments &o) const
 
bool operator< (const Segments &o) const
 
bool operator> (const Segments &o) const
 
bool operator<= (const Segments &o) const
 
bool operator>= (const Segments &o) const
 
constexpr f64 to_fraction_unchecked () const
 Converts to a day fraction without checking if it's valid.
 

Public Attributes

u8 hour = 0
 
u8 minute = 0
 
u8 second = 0
 
u32 nano = 0
 

Detailed Description

Represents time in segments (hour, minute, etc.).

Definition at line 45 of file calendars/mtcore_calendars/time.hpp.

Member Function Documentation

◆ operator DayFraction()

mtcore::calendars::Segments::operator DayFraction ( ) const
explicitnodiscardconstexpr

Definition at line 190 of file calendars/mtcore_calendars/time.hpp.

190 {
191 const auto nanos = static_cast<NanoSeconds>(*this);
192 return static_cast<DayFraction>(nanos);
193 }

◆ operator NanoSeconds()

mtcore::calendars::Segments::operator NanoSeconds ( ) const
explicitnodiscardconstexpr

Definition at line 180 of file calendars/mtcore_calendars/time.hpp.

180 {
181 ensure(try_validate().is_success());
182 const auto hours = static_cast<u64>(this->hour);
183 const auto minutes = 60 * hours + static_cast<u64>(this->minute);
184 const auto seconds = 60 * minutes + static_cast<u64>(this->second);
185 const auto nano = seconds * NANOS_PER_SECOND + static_cast<u64>(this->nano);
186 const auto res = NanoSeconds{.nano = nano};
187 ensure(res.try_validate().is_success());
188 return res;
189 }
constexpr i32 NANOS_PER_SECOND
Number of nanoseconds per second.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
uint64_t u64
Alias for 64-bit unsigned ints.
constexpr Result< void, TimeValidationError > try_validate() const
Tries to validate.
Here is the call graph for this function:

◆ operator!=()

bool mtcore::calendars::Segments::operator!= ( const Segments & o) const
inlinenodiscard

Definition at line 92 of file calendars/mtcore_calendars/time.hpp.

92{ return (*this <=> o) != std::strong_ordering::equal; }

◆ operator<()

bool mtcore::calendars::Segments::operator< ( const Segments & o) const
inlinenodiscard

Definition at line 93 of file calendars/mtcore_calendars/time.hpp.

93{ return (*this <=> o) == std::strong_ordering::less; }

◆ operator<=()

bool mtcore::calendars::Segments::operator<= ( const Segments & o) const
inlinenodiscard

Definition at line 95 of file calendars/mtcore_calendars/time.hpp.

95 {
96 const auto cmp = (*this <=> o);
97 return cmp == std::strong_ordering::equal || cmp == std::strong_ordering::less;
98 }

◆ operator<=>()

auto mtcore::calendars::Segments::operator<=> ( const Segments & o) const
inlinenodiscardconstexpr

Definition at line 75 of file calendars/mtcore_calendars/time.hpp.

75 {
76 if (hour != o.hour) {
77 return hour <=> o.hour;
78 }
79 if (minute != o.minute) {
80 return minute <=> o.minute;
81 }
82 if (second != o.second) {
83 return second <=> o.second;
84 }
85 if (nano != o.nano) {
86 return nano <=> o.nano;
87 }
88 return std::strong_ordering::equal;
89 }

◆ operator==()

bool mtcore::calendars::Segments::operator== ( const Segments & o) const
inlinenodiscard

Definition at line 91 of file calendars/mtcore_calendars/time.hpp.

91{ return (*this <=> o) == std::strong_ordering::equal; }

◆ operator>()

bool mtcore::calendars::Segments::operator> ( const Segments & o) const
inlinenodiscard

Definition at line 94 of file calendars/mtcore_calendars/time.hpp.

94{ return (*this <=> o) == std::strong_ordering::greater; }

◆ operator>=()

bool mtcore::calendars::Segments::operator>= ( const Segments & o) const
inlinenodiscard

Definition at line 99 of file calendars/mtcore_calendars/time.hpp.

99 {
100 const auto cmp = (*this <=> o);
101 return cmp == std::strong_ordering::equal || cmp == std::strong_ordering::greater;
102 }

◆ to_fraction_unchecked()

f64 mtcore::calendars::Segments::to_fraction_unchecked ( ) const
nodiscardconstexpr

Converts to a day fraction without checking if it's valid.

Prefer casting to a DayFraction. Used with internal conversion methods.

Definition at line 195 of file calendars/mtcore_calendars/time.hpp.

195 {
196 const auto hours = static_cast<u64>(this->hour);
197 const auto minutes = 60 * hours + static_cast<u64>(this->minute);
198 const auto seconds = 60 * minutes + static_cast<u64>(this->second);
199 const auto nano = seconds * NANOS_PER_SECOND + static_cast<u64>(this->nano);
200 const auto nanoFloat = static_cast<f80>(nano);
201 const auto frac = nanoFloat / static_cast<f80>(NANOS_PER_DAY);
202 return static_cast<f64>(frac);
203 }
constexpr i64 NANOS_PER_DAY
Number of nanoseconds per day.
double f64
Alias for 64-bit floats.
long double f80
Alias for 80-bit floats.

◆ try_validate()

Result< void, TimeValidationError > mtcore::calendars::Segments::try_validate ( ) const
inlinenodiscardconstexpr

Tries to validate.

Will return validation error describing what's wrong if invalid.

Definition at line 52 of file calendars/mtcore_calendars/time.hpp.

52 {
53 if (hour >= 24) {
55 }
56
57 if (minute >= 60) {
59 }
60
61 if (second >= 60) {
63 }
64
65 if (nano >= NANOS_PER_SECOND) {
67 }
68
69 return success();
70 }
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ hour

u8 mtcore::calendars::Segments::hour = 0

Definition at line 46 of file calendars/mtcore_calendars/time.hpp.

◆ minute

u8 mtcore::calendars::Segments::minute = 0

Definition at line 47 of file calendars/mtcore_calendars/time.hpp.

◆ nano

u32 mtcore::calendars::Segments::nano = 0

Definition at line 49 of file calendars/mtcore_calendars/time.hpp.

◆ second

u8 mtcore::calendars::Segments::second = 0

Definition at line 48 of file calendars/mtcore_calendars/time.hpp.


The documentation for this struct was generated from the following file: