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

Base calendar system tracking the number of days since its epoch. More...

#include <fixed.hpp>

Public Member Functions

constexpr auto operator<=> (const Fixed &o) const
 Comparison method.
 
constexpr auto operator== (const Fixed &o) const -> bool
 
constexpr auto operator!= (const Fixed &o) const -> bool
 
constexpr auto operator<= (const Fixed &o) const -> bool
 
constexpr auto operator>= (const Fixed &o) const -> bool
 
constexpr auto operator< (const Fixed &o) const -> bool
 
constexpr auto operator> (const Fixed &o) const -> bool
 
constexpr Fixed to_fixed () const
 Day Calendar System contract.
 
constexpr DayOfWeek day_of_week () const
 Gets the day of week for a date.
 
constexpr Fixed add_days (const i32 days) const
 Adds n days to a 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.
 
constexpr Fixed nth_week_day (const i32 n, const DayOfWeek k) const
 Gets the nth day of week.
 
constexpr Fixed day_of_week_before (const DayOfWeek k) const
 Gets the day of week before current date.
 
constexpr Fixed day_of_week_after (const DayOfWeek k) const
 Gets the day of week after the current date.
 
constexpr Fixed day_of_week_nearest (const DayOfWeek k) const
 Gets the day of week nearest to the current date.
 
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 day_of_week_on_or_after (const DayOfWeek k) const
 Gets the day of week on or after the current date.
 
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 last_week_day (const DayOfWeek k) const
 Gets the last week day in a month (assuming current date is the end of the month)
 
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 collect all occurrences of events (e.g.
 
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 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 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 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 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 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 Public Member Functions

static constexpr Fixed from_fixed (const Fixed &o)
 Day Calendar System contract.
 

Public Attributes

i32 day
 

Static Public Attributes

static constexpr auto EPOCH = FIXED_EPOCH
 
static constexpr std::string_view name = "FIXED"
 

Detailed Description

Base calendar system tracking the number of days since its epoch.

Used as common ground for conversions between calendar systems.

Definition at line 42 of file fixed.hpp.

Member Function Documentation

◆ add_days()

Fixed mtcore::calendars::Fixed::add_days ( const i32 days) const
inlinenodiscardconstexpr

Adds n days to a date.

Definition at line 91 of file fixed.hpp.

91{ return {day + days}; }
Here is the caller graph for this function:

◆ day_difference()

i32 mtcore::calendars::Fixed::day_difference ( const Fixed & other) const
inlinenodiscardconstexpr

Difference between two dates in days.

Definition at line 97 of file fixed.hpp.

97{ return day - other.day; }
Here is the caller graph for this function:

◆ day_of_m_cycle()

i32 mtcore::calendars::Fixed::day_of_m_cycle ( i32 m,
i32 offset ) const
inlinenodiscardconstexpr

Gets which day the current date occurs in an m-length ("month") cycle.

Parameters
mLength of cycle
offsetOffset for start of cycle

Definition at line 185 of file fixed.hpp.

185{ return math::mod(day - offset, m); }
constexpr Res mod(L left, R right) noexcept
Calculates the mathematical mod of two numbers.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ day_of_week()

DayOfWeek mtcore::calendars::Fixed::day_of_week ( ) const
inlinenodiscardconstexpr

Gets the day of week for a date.

Definition at line 83 of file fixed.hpp.

83 {
84 const i32 d = day - FIXED_EPOCH - static_cast<i32>(DayOfWeek::Sunday);
85 const auto dow = math::mod(d, 7);
86 ensure(dow >= 0 && dow < 7);
87 return static_cast<DayOfWeek>(dow);
88 }
DayOfWeek
Enum class for the day of week.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
int32_t i32
Alias for 32-bit ints.
constexpr i32 FIXED_EPOCH
Definition epochs.hpp:25
Here is the call graph for this function:
Here is the caller graph for this function:

◆ day_of_week_after()

Fixed mtcore::calendars::Fixed::day_of_week_after ( const DayOfWeek k) const
inlinenodiscardconstexpr

Gets the day of week after the current date.

Definition at line 118 of file fixed.hpp.

118 {
120 }
constexpr Fixed day_of_week_on_or_before(const DayOfWeek k) const
Gets the day of week on or before the current date.
Definition fixed.hpp:128
constexpr Fixed add_days(const i32 days) const
Adds n days to a date.
Definition fixed.hpp:91
Here is the call graph for this function:
Here is the caller graph for this function:

◆ day_of_week_before()

Fixed mtcore::calendars::Fixed::day_of_week_before ( const DayOfWeek k) const
inlinenodiscardconstexpr

Gets the day of week before current date.

Definition at line 113 of file fixed.hpp.

113 {
115 }
constexpr Fixed sub_days(const i32 days) const
Subtracts n days from a date.
Definition fixed.hpp:94
Here is the call graph for this function:
Here is the caller graph for this function:

◆ day_of_week_nearest()

Fixed mtcore::calendars::Fixed::day_of_week_nearest ( const DayOfWeek k) const
inlinenodiscardconstexpr

Gets the day of week nearest to the current date.

Definition at line 123 of file fixed.hpp.

123 {
125 }
Here is the call graph for this function:

◆ day_of_week_on_or_after()

Fixed mtcore::calendars::Fixed::day_of_week_on_or_after ( const DayOfWeek k) const
inlinenodiscardconstexpr

Gets the day of week on or after the current date.

Definition at line 137 of file fixed.hpp.

137 {
139 }
Here is the call graph for this function:

◆ day_of_week_on_or_before()

Fixed mtcore::calendars::Fixed::day_of_week_on_or_before ( const DayOfWeek k) const
inlinenodiscardconstexpr

Gets the day of week on or before the current date.

Definition at line 128 of file fixed.hpp.

128 {
129 const auto dayOfWeekPrev = (Fixed{
130 .day = day - static_cast<i32>(k),
131 })
132 .day_of_week();
133 return Fixed{.day = day - static_cast<i32>(dayOfWeekPrev)};
134 }
constexpr DayOfWeek day_of_week() const
Gets the day of week for a date.
Definition fixed.hpp:83
Here is the call graph for this function:
Here is the caller graph for this function:

◆ first_week_day()

Fixed mtcore::calendars::Fixed::first_week_day ( const DayOfWeek k) const
inlinenodiscardconstexpr

Gets the first week day in a month (assuming current date is the start of the month)

Definition at line 142 of file fixed.hpp.

142{ return nth_week_day(1, k); }
constexpr Fixed nth_week_day(const i32 n, const DayOfWeek k) const
Gets the nth day of week.
Definition fixed.hpp:100
Here is the call graph for this function:

◆ from_fixed()

static constexpr Fixed mtcore::calendars::Fixed::from_fixed ( const Fixed & o)
inlinestaticnodiscardconstexpr

Day Calendar System contract.

Definition at line 78 of file fixed.hpp.

78{ return o; }

◆ kth_day_of_m_cycle_after()

auto mtcore::calendars::Fixed::kth_day_of_m_cycle_after ( i32 k,
i32 m,
i32 offset ) const -> Fixed
inlinenodiscardconstexprnoexcept

Finds the kth day of the m-cycle that occurs after the current date.

Parameters
kThe kth day of the m-cycle
mThe number of days in the m-cycle
offsetThe offset of the m-cycle
Returns
A date with the result

Definition at line 228 of file fixed.hpp.

228 {
229 return add_days(m).kth_day_of_m_cycle_on_or_before(k, m, offset);
230 }
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.
Definition fixed.hpp:194
Here is the call graph for this function:

◆ kth_day_of_m_cycle_before()

auto mtcore::calendars::Fixed::kth_day_of_m_cycle_before ( i32 k,
i32 m,
i32 offset ) const -> Fixed
inlinenodiscardconstexprnoexcept

Finds the kth day of the m-cycle that occurs before the current date.

Parameters
kThe kth day of the m-cycle
mThe number of days in the m-cycle
offsetThe offset of the m-cycle
Returns
A date with the result

Definition at line 206 of file fixed.hpp.

206 {
207 return sub_days(1).kth_day_of_m_cycle_on_or_before(k, m, offset);
208 }
Here is the call graph for this function:

◆ kth_day_of_m_cycle_nearest()

auto mtcore::calendars::Fixed::kth_day_of_m_cycle_nearest ( i32 k,
i32 m,
i32 offset ) const -> Fixed
inlinenodiscardconstexprnoexcept

Finds the kth day of the m-cycle that occurs nearest the current date.

Parameters
kThe kth day of the m-cycle
mThe number of days in the m-cycle
offsetThe offset of the m-cycle
Returns
A date with the result

Definition at line 239 of file fixed.hpp.

239 {
240 return add_days(m / 2).kth_day_of_m_cycle_on_or_before(k, m, offset);
241 }
Here is the call graph for this function:

◆ kth_day_of_m_cycle_on_or_after()

auto mtcore::calendars::Fixed::kth_day_of_m_cycle_on_or_after ( i32 k,
i32 m,
i32 offset ) const -> Fixed
inlinenodiscardconstexprnoexcept

Finds the kth day of the m-cycle that occurs on or after the current date.

Parameters
kThe kth day of the m-cycle
mThe number of days in the m-cycle
offsetThe offset of the m-cycle
Returns
A date with the result

Definition at line 217 of file fixed.hpp.

217 {
218 return add_days(m - 1).kth_day_of_m_cycle_on_or_before(k, m, offset);
219 }
Here is the call graph for this function:

◆ kth_day_of_m_cycle_on_or_before()

auto mtcore::calendars::Fixed::kth_day_of_m_cycle_on_or_before ( i32 k,
i32 m,
i32 offset ) const -> Fixed
inlinenodiscardconstexprnoexcept

Finds the kth day of the m-cycle that occurs on or before the current date.

Parameters
kThe kth day of the m-cycle
mThe number of days in the m-cycle
offsetThe offset of the m-cycle
Returns
A date with the result

Definition at line 194 of file fixed.hpp.

194 {
195 const auto mCycleDay = sub_days(k).day_of_m_cycle(m, offset);
196 return sub_days(mCycleDay);
197 }
constexpr i32 day_of_m_cycle(i32 m, i32 offset) const
Gets which day the current date occurs in an m-length ("month") cycle.
Definition fixed.hpp:185
Here is the call graph for this function:
Here is the caller graph for this function:

◆ last_week_day()

Fixed mtcore::calendars::Fixed::last_week_day ( const DayOfWeek k) const
inlinenodiscardconstexpr

Gets the last week day in a month (assuming current date is the end of the month)

Definition at line 145 of file fixed.hpp.

145{ return nth_week_day(-1, k); }
Here is the call graph for this function:

◆ nth_week_day()

Fixed mtcore::calendars::Fixed::nth_week_day ( const i32 n,
const DayOfWeek k ) const
inlinenodiscardconstexpr

Gets the nth day of week.

Definition at line 100 of file fixed.hpp.

100 {
101 if (n > 0) {
102 return day_of_week_before(k).add_days(7 * n);
103 }
104 else if (n < 0) {
105 return day_of_week_after(k).add_days(7 * n);
106 }
107 else {
108 return *this;
109 }
110 }
constexpr Fixed day_of_week_before(const DayOfWeek k) const
Gets the day of week before current date.
Definition fixed.hpp:113
constexpr Fixed day_of_week_after(const DayOfWeek k) const
Gets the day of week after the current date.
Definition fixed.hpp:118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator!=()

auto mtcore::calendars::Fixed::operator!= ( const Fixed & o) const -> bool
inlinenodiscardconstexpr

Definition at line 56 of file fixed.hpp.

56 {
57 auto cmp = *this <=> o;
58 return cmp != decltype(cmp)::equivalent;
59 }

◆ operator<()

auto mtcore::calendars::Fixed::operator< ( const Fixed & o) const -> bool
inlinenodiscardconstexpr

Definition at line 68 of file fixed.hpp.

68 {
69 auto cmp = *this <=> o;
70 return cmp == decltype(cmp)::less;
71 }

◆ operator<=()

auto mtcore::calendars::Fixed::operator<= ( const Fixed & o) const -> bool
inlinenodiscardconstexpr

Definition at line 60 of file fixed.hpp.

60 {
61 auto cmp = *this <=> o;
62 return cmp == decltype(cmp)::equivalent || cmp == decltype(cmp)::less;
63 }

◆ operator<=>()

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

Comparison method.

Parameters
oType to compare against

Definition at line 51 of file fixed.hpp.

51{ return day <=> o.day; }

◆ operator==()

auto mtcore::calendars::Fixed::operator== ( const Fixed & o) const -> bool
inlinenodiscardconstexpr

Definition at line 52 of file fixed.hpp.

52 {
53 auto cmp = *this <=> o;
54 return cmp == decltype(cmp)::equivalent;
55 }

◆ operator>()

auto mtcore::calendars::Fixed::operator> ( const Fixed & o) const -> bool
inlinenodiscardconstexpr

Definition at line 72 of file fixed.hpp.

72 {
73 auto cmp = *this <=> o;
74 return cmp == decltype(cmp)::greater;
75 }

◆ operator>=()

auto mtcore::calendars::Fixed::operator>= ( const Fixed & o) const -> bool
inlinenodiscardconstexpr

Definition at line 64 of file fixed.hpp.

64 {
65 auto cmp = *this <=> o;
66 return cmp == decltype(cmp)::equivalent || cmp == decltype(cmp)::greater;
67 }

◆ positions_in_range()

auto mtcore::calendars::Fixed::positions_in_range ( Allocator & alloc,
const Fixed & end,
i32 pthMoment,
i32 cDayCycle,
i32 delta ) const -> Result<mtcore::ArrayList<Fixed>, AllocationError>
inlinenodiscard

Return the cyclic positions in the range of the current date to the end date (exclusive) Used to collect all occurrences of events (e.g.

holidays) in an interval of time (e.g. a Gregorian year)

Assumes $0 \le pthMoment \lt cDayCycle$

Parameters
endEnd of the cyclic range
pthMomentStarting pth moment search. First result on or after a given moment of the pth moment (basically the cycle start)
cDayCycleThe number of days in the occurrence day cycle (basically the cycle length)
deltaCongruent modulo of cDayCycle to the position of Fixed{0} in the repeating cycle (basically the cycle modulo offset)
Returns

Definition at line 161 of file fixed.hpp.

162 {
163 auto capacity =
164 std::max<size_t>(1, static_cast<size_t>(count_positions_in_range(end, pthMoment, cDayCycle, delta)));
165 ArrayList<Fixed> res;
166 if (const auto r = res.init(alloc, capacity); r.is_error()) {
167 return r.error();
168 }
169 auto start = this->day;
170 while (true) {
171 auto curDate = math::mod_range<i32>(pthMoment - delta, start, start + cDayCycle);
172 if (curDate >= end.day) {
173 return success(res);
174 }
175 ensure(res.push({curDate}).is_success());
176 start += cDayCycle;
177 }
178 }
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.
Definition result.hpp:398
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sub_days()

Fixed mtcore::calendars::Fixed::sub_days ( const i32 days) const
inlinenodiscardconstexpr

Subtracts n days from a date.

Definition at line 94 of file fixed.hpp.

94{ return {day - days}; }
Here is the caller graph for this function:

◆ to_fixed()

Fixed mtcore::calendars::Fixed::to_fixed ( ) const
inlinenodiscardconstexpr

Day Calendar System contract.

Definition at line 80 of file fixed.hpp.

80{ return *this; }

Member Data Documentation

◆ day

i32 mtcore::calendars::Fixed::day

Definition at line 45 of file fixed.hpp.

◆ EPOCH

auto mtcore::calendars::Fixed::EPOCH = FIXED_EPOCH
staticconstexpr

Definition at line 43 of file fixed.hpp.

◆ name

std::string_view mtcore::calendars::Fixed::name = "FIXED"
staticconstexpr

Definition at line 44 of file fixed.hpp.


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