19#ifndef MTCORE_CALENDARS_TIME_HPP
20#define MTCORE_CALENDARS_TIME_HPP
72 [[nodiscard]]
explicit constexpr operator NanoSeconds()
const;
73 [[nodiscard]]
explicit constexpr operator DayFraction()
const;
88 return std::strong_ordering::equal;
91 [[nodiscard]]
bool operator==(
const Segments &o)
const {
return (*this <=> o) == std::strong_ordering::equal; }
92 [[nodiscard]]
bool operator!=(
const Segments &o)
const {
return (*this <=> o) != std::strong_ordering::equal; }
93 [[nodiscard]]
bool operator<(
const Segments &o)
const {
return (*this <=> o) == std::strong_ordering::less; }
94 [[nodiscard]]
bool operator>(
const Segments &o)
const {
return (*this <=> o) == std::strong_ordering::greater; }
96 const auto cmp = (*this <=> o);
97 return cmp == std::strong_ordering::equal || cmp == std::strong_ordering::less;
100 const auto cmp = (*this <=> o);
101 return cmp == std::strong_ordering::equal || cmp == std::strong_ordering::greater;
114 if (!std::isfinite(
frac)) {
126 [[nodiscard]]
explicit constexpr operator NanoSeconds()
const;
127 [[nodiscard]]
explicit constexpr operator Segments()
const;
132 return (*this <=> o) == std::weak_ordering::equivalent;
135 return (*this <=> o) != std::weak_ordering::equivalent;
138 [[nodiscard]]
bool operator>(
const DayFraction &o)
const {
return (*this <=> o) == std::weak_ordering::greater; }
140 const auto cmp = (*this <=> o);
141 return cmp == std::weak_ordering::equivalent || cmp == std::weak_ordering::less;
144 const auto cmp = (*this <=> o);
145 return cmp == std::weak_ordering::equivalent || cmp == std::weak_ordering::greater;
161 [[nodiscard]]
explicit constexpr operator DayFraction()
const;
162 [[nodiscard]]
explicit constexpr operator Segments()
const;
168 [[nodiscard]]
bool operator<(
const NanoSeconds &o)
const {
return (*this <=> o) == std::strong_ordering::less; }
169 [[nodiscard]]
bool operator>(
const NanoSeconds &o)
const {
return (*this <=> o) == std::strong_ordering::greater; }
171 const auto cmp = (*this <=> o);
172 return cmp == std::strong_ordering::equal || cmp == std::strong_ordering::less;
175 const auto cmp = (*this <=> o);
176 return cmp == std::strong_ordering::equal || cmp == std::strong_ordering::greater;
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);
187 ensure(res.try_validate().is_success());
191 const auto nanos =
static_cast<NanoSeconds>(*this);
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);
200 const auto nanoFloat =
static_cast<f80>(
nano);
202 return static_cast<f64>(frac);
209 ensure(res.try_validate().is_success());
214 const auto nanos =
static_cast<NanoSeconds>(*this);
215 return static_cast<Segments>(nanos);
222 ensure(res.try_validate().is_success());
245 .hour =
static_cast<u8>(val),
251 ensure(res.try_validate().is_success());
259 template<WriterImpl WI>
268 template<WriterImpl WI>
277 template<WriterImpl WI>
285 constexpr auto chars =
":. ";
289#define MTCORE_CAL_TIME_SEG_FMT_TRY_WRITE(FMTSTR, VAL) \
290 if (auto r = io::print(writer, (FMTSTR), (VAL)); r.is_error()) { \
294 written += r.value(); \
297 while (splits.next().copy_if_present(cur)) {
298 const auto [sub, sep] = cur;
299 if (sep.has_value()) {
300 if (
auto r = writer.write(sep.value()); r.is_error()) {
#define MTCORE_CAL_TIME_SEG_FMT_TRY_WRITE(FMTSTR, VAL)
constexpr i64 NANOS_PER_DAY
Number of nanoseconds per day.
TimeValidationError
Common try_validate errors for time.
constexpr i32 NANOS_PER_SECOND
Number of nanoseconds per second.
SplitOneOfIter< T > split_one_of(const Slice< std::add_const_t< T > > &needles, const Slice< T > &haystack)
Splits a slice into smaller sub slices.
constexpr Slice< const char32_t > slice_from(char32_t *cstr)
Creates a slice from a utf32 string in the form of a c string.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
constexpr R floor(T num) noexcept
Floors a number with support for constexpr and fast runtime compilation.
constexpr Res mod(L left, R right) noexcept
Calculates the mathematical mod of two numbers.
constexpr auto amod(L x, R y) noexcept -> Res
x mod [1..y]
Success< T > success(const T &v)
Creates a successful Result.
Success< void > success()
Creates a successful void Result object.
Error< Underlying > error(Underlying err)
Creates an error.
uint64_t u64
Alias for 64-bit unsigned ints.
int32_t i32
Alias for 32-bit ints.
int64_t i64
Alias for 64-bit ints.
uint8_t u8
Alias for 8-bit unsigned ints.
double f64
Alias for 64-bit floats.
long double f80
Alias for 80-bit floats.
uint32_t u32
Alias for 32-bit unsigned ints.
Namespace for calendaring systems.
bool str_equal(const L &left, const R &right)
Compares two string strings for ordering.
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Represents a pair of elements Useful to avoid having to have many micro structs.
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
constexpr bool empty() const noexcept
Checks if a Slice is empty.
bool operator!=(const DayFraction &o) const
constexpr auto operator<=>(const DayFraction &o) const
bool operator>(const DayFraction &o) const
constexpr Result< void, TimeValidationError > try_validate() const
Tries to validate.
bool operator<(const DayFraction &o) const
bool operator>=(const DayFraction &o) const
bool operator==(const DayFraction &o) const
bool operator<=(const DayFraction &o) const
bool operator<=(const NanoSeconds &o) const
bool operator<(const NanoSeconds &o) const
static constexpr auto max
bool operator==(const NanoSeconds &o) const
bool operator>(const NanoSeconds &o) const
constexpr Result< void, TimeValidationError > try_validate() const
Tries to validate.
bool operator!=(const NanoSeconds &o) const
bool operator>=(const NanoSeconds &o) const
constexpr auto operator<=>(const NanoSeconds &o) const
Represents time in segments (hour, minute, etc.).
bool operator>(const Segments &o) const
constexpr f64 to_fraction_unchecked() const
Converts to a day fraction without checking if it's valid.
constexpr Result< void, TimeValidationError > try_validate() const
Tries to validate.
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 auto operator<=>(const Segments &o) const
bool operator>=(const Segments &o) const
A writer that writes data to some sort of stream or buffer Note: the data elements written should be ...