19#ifndef MTCORE_CALENDARS_FIXED_HPP
20#define MTCORE_CALENDARS_FIXED_HPP
44 static constexpr std::string_view
name =
"FIXED";
53 auto cmp = *this <=> o;
54 return cmp ==
decltype(cmp)::equivalent;
57 auto cmp = *this <=> o;
58 return cmp !=
decltype(cmp)::equivalent;
61 auto cmp = *this <=> o;
62 return cmp ==
decltype(cmp)::equivalent || cmp ==
decltype(cmp)::less;
65 auto cmp = *this <=> o;
66 return cmp ==
decltype(cmp)::equivalent || cmp ==
decltype(cmp)::greater;
69 auto cmp = *this <=> o;
70 return cmp ==
decltype(cmp)::less;
73 auto cmp = *this <=> o;
74 return cmp ==
decltype(cmp)::greater;
86 ensure(dow >= 0 && dow < 7);
129 const auto dayOfWeekPrev = (
Fixed{
130 .day =
day -
static_cast<i32>(k),
133 return Fixed{.day =
day -
static_cast<i32>(dayOfWeekPrev)};
164 std::max<size_t>(1,
static_cast<size_t>(count_positions_in_range(end, pthMoment, cDayCycle, delta)));
166 if (
const auto r = res.
init(alloc, capacity); r.is_error()) {
169 auto start = this->day;
172 if (curDate >= end.day) {
244 [[nodiscard]]
constexpr auto count_positions_in_range(
const Fixed &end,
i32 pthMoment,
i32 cDayCycle,
245 i32 delta)
const ->
size_t {
247 auto start = this->day;
250 if (curDate >= end.day) {
266 static constexpr std::string_view
name =
"MOMENT";
288 return {
static_cast<f64>(f.
day) + mt};
DayOfWeek
Enum class for the day of week.
AllocationError
Error indicating failed allocation.
#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 mod_range(T x, i64 a, i64 b) noexcept -> T
x mod [a..b)
Success< void > success()
Creates a successful void Result object.
int32_t i32
Alias for 32-bit ints.
double f64
Alias for 64-bit floats.
Namespace for calendaring systems.
constexpr i32 FIXED_EPOCH
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
Array list is a growable, dynamic array with elements inside Elements are stored in an array,...
Result< void, AllocationError > init(Allocator &alloc, size_t initCapacity=10) noexcept
Initializes an empty ArrayList with an initial capacity.
Result< void, AllocationError > push(Allocator &alloc, const Slice< std::add_const_t< T > > &elems) noexcept
Pushes a list of items onto an array list.
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Base calendar system tracking the number of days since its epoch.
auto positions_in_range(Allocator &alloc, const Fixed &end, i32 pthMoment, i32 cDayCycle, i32 delta) const -> Result< mtcore::ArrayList< Fixed >, AllocationError >
Return the cyclic positions in the range of the current date to the end date (exclusive) Used to coll...
constexpr i32 day_of_m_cycle(i32 m, i32 offset) const
Gets which day the current date occurs in an m-length ("month") cycle.
constexpr Fixed last_week_day(const DayOfWeek k) const
Gets the last week day in a month (assuming current date is the end of the month)
constexpr auto operator>=(const Fixed &o) const -> bool
constexpr auto kth_day_of_m_cycle_on_or_before(i32 k, i32 m, i32 offset) const noexcept -> Fixed
Finds the kth day of the m-cycle that occurs on or before the current date.
constexpr auto operator<=(const Fixed &o) const -> bool
constexpr auto kth_day_of_m_cycle_nearest(i32 k, i32 m, i32 offset) const noexcept -> Fixed
Finds the kth day of the m-cycle that occurs nearest the current date.
static constexpr auto EPOCH
constexpr Fixed day_of_week_on_or_before(const DayOfWeek k) const
Gets the day of week on or before the current date.
constexpr Fixed nth_week_day(const i32 n, const DayOfWeek k) const
Gets the nth day of week.
constexpr Fixed day_of_week_on_or_after(const DayOfWeek k) const
Gets the day of week on or after the current date.
static constexpr std::string_view name
constexpr Fixed first_week_day(const DayOfWeek k) const
Gets the first week day in a month (assuming current date is the start of the month)
constexpr Fixed day_of_week_before(const DayOfWeek k) const
Gets the day of week before current date.
constexpr auto operator!=(const Fixed &o) const -> bool
constexpr auto kth_day_of_m_cycle_before(i32 k, i32 m, i32 offset) const noexcept -> Fixed
Finds the kth day of the m-cycle that occurs before the current date.
constexpr auto operator<(const Fixed &o) const -> bool
constexpr Fixed add_days(const i32 days) const
Adds n days to a date.
constexpr auto operator>(const Fixed &o) const -> bool
constexpr auto operator==(const Fixed &o) const -> bool
constexpr auto kth_day_of_m_cycle_on_or_after(i32 k, i32 m, i32 offset) const noexcept -> Fixed
Finds the kth day of the m-cycle that occurs on or after the current date.
constexpr Fixed day_of_week_after(const DayOfWeek k) const
Gets the day of week after the current date.
constexpr auto kth_day_of_m_cycle_after(i32 k, i32 m, i32 offset) const noexcept -> Fixed
Finds the kth day of the m-cycle that occurs after the current date.
constexpr Fixed to_fixed() const
Day Calendar System contract.
constexpr DayOfWeek day_of_week() const
Gets the day of week for a date.
constexpr auto operator<=>(const Fixed &o) const
Comparison method.
static constexpr Fixed from_fixed(const Fixed &o)
Day Calendar System contract.
constexpr Fixed day_of_week_nearest(const DayOfWeek k) const
Gets the day of week nearest to the current date.
constexpr Fixed sub_days(const i32 days) const
Subtracts n days from a date.
constexpr i32 day_difference(const Fixed &other) const
Difference between two dates in days.
Base calendar system tracking the number of days (and fraction of days) since its epoch.
static constexpr Moment from_moment(Moment m)
Time Calendar System contract.
constexpr auto operator<=>(const Moment &o) const
Time Calendar System contract.
constexpr bool is_valid() const
Checks that a moment is valid.
constexpr i32 day_difference(const Moment &other) const
Get the difference in days between two moments.
static constexpr Moment reconstruct(const Fixed &f, const Moment &m)
Reconstructs a moment from a fixed date and the time portion of a moment.
constexpr Moment add_days(const i32 days) const
Adds n days to a moment.
static constexpr Fixed moment_to_fixed(const Moment &m)
Converts a moment to fixed date (drops time)
constexpr Moment to_moment() const
Time Calendar System contract.
static constexpr std::string_view name
constexpr Moment sub_days(const i32 days) const
Subtracts n days from a moment.
static constexpr auto EPOCH