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

#include <gregorian.hpp>

Collaboration diagram for mtcore::calendars::systems::Gregorian:

Public Types

enum class  Month {
  January = 1 , February , March , April ,
  May , June , July , August ,
  September , October , November , December
}
 

Public Member Functions

void init (i32 year, Month month, u8 day)
 
constexpr std::strong_ordering operator<=> (const Gregorian &g) const
 
constexpr bool leap_year () const
 
constexpr Gregorian year_start () const
 
constexpr Gregorian year_end () const
 
constexpr Fixed to_fixed () const
 
constexpr u16 days_in_year () const
 
constexpr u8 days_in_month () const
 

Static Public Member Functions

static constexpr bool is_leap_year (const AstronomicalYear year)
 
static constexpr Gregorian year_start_for (const AstronomicalYear year)
 
static constexpr Gregorian year_end_for (const AstronomicalYear year)
 
static constexpr Gregorian from_fixed (const Fixed &f)
 
static constexpr u16 days_in_year_for (const AstronomicalYear year)
 
static constexpr u8 days_in_month_for (const AstronomicalYear year, const Month month)
 

Public Attributes

AstronomicalYear year = {}
 
Month month = Month::January
 
u8 day = 1
 

Static Public Attributes

static constexpr auto EPOCH = GREGORIAN_EPOCH
 
static constexpr std::string_view name = "Gregorian"
 

Detailed Description

Definition at line 28 of file gregorian.hpp.

Member Enumeration Documentation

◆ Month

Enumerator
January 
February 
March 
April 
May 
June 
July 
August 
September 
October 
November 
December 

Definition at line 32 of file gregorian.hpp.

32 {
33 January = 1,
34 February,
35 March,
36 April,
37 May,
38 June,
39 July,
40 August,
41 September,
42 October,
43 November,
44 December
45 };

Member Function Documentation

◆ days_in_month()

u8 mtcore::calendars::systems::Gregorian::days_in_month ( ) const
inlinenodiscardconstexpr

Definition at line 122 of file gregorian.hpp.

122{ return days_in_month_for(year, month); }
static constexpr u8 days_in_month_for(const AstronomicalYear year, const Month month)
Here is the call graph for this function:

◆ days_in_month_for()

static constexpr u8 mtcore::calendars::systems::Gregorian::days_in_month_for ( const AstronomicalYear year,
const Month month )
inlinestaticnodiscardconstexpr

Definition at line 124 of file gregorian.hpp.

124 {
125 switch (month) {
126 case Month::January:
127 case Month::March:
128 case Month::May:
129 case Month::July:
130 case Month::August:
131 case Month::October:
132 case Month::December:
133 return 31;
134 case Month::February:
135 return is_leap_year(year) ? 29 : 28;
136 case Month::April:
137 case Month::June:
138 case Month::September:
139 case Month::November:
140 return 30;
141 }
142 unreachable("BAD MONTH");
143 }
#define unreachable(...)
Marks code as unreachable.
static constexpr bool is_leap_year(const AstronomicalYear year)
Definition gregorian.hpp:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ days_in_year()

u16 mtcore::calendars::systems::Gregorian::days_in_year ( ) const
inlinenodiscardconstexpr

Definition at line 113 of file gregorian.hpp.

113{ return days_in_year_for(year); }
static constexpr u16 days_in_year_for(const AstronomicalYear year)
Here is the call graph for this function:

◆ days_in_year_for()

static constexpr u16 mtcore::calendars::systems::Gregorian::days_in_year_for ( const AstronomicalYear year)
inlinestaticnodiscardconstexpr

Definition at line 115 of file gregorian.hpp.

115 {
116 if (is_leap_year(year)) {
117 return 366;
118 }
119 return 365;
120 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_fixed()

static constexpr Gregorian mtcore::calendars::systems::Gregorian::from_fixed ( const Fixed & f)
inlinestaticnodiscardconstexpr

Definition at line 99 of file gregorian.hpp.

99 {
100 const auto year = year_from_fixed(f);
101 const auto yearStart = year_start_for(year);
102 const auto yearStartRdDate = yearStart.to_fixed();
103 const auto priorDays = f.day_difference(yearStartRdDate);
104 const auto marchFirst = Gregorian{year, Month::March, 1};
105 const auto marchFirstRdDate = marchFirst.to_fixed();
106 const auto correction = (f <=> marchFirstRdDate) == std::strong_ordering::less ? 0 : is_leap_year(year) ? 1 : 2;
107 const auto month = math::floor<double, Month>(static_cast<double>(12 * (priorDays + correction) + 373) / 367.0);
108 const auto firstOfMonth = Gregorian{year, month, 1};
109 const auto firstOfMonthRdDate = firstOfMonth.to_fixed();
110 return Gregorian{year, month, static_cast<u8>(f.day_difference(firstOfMonthRdDate) + 1)};
111 }
constexpr R floor(T num) noexcept
Floors a number with support for constexpr and fast runtime compilation.
uint8_t u8
Alias for 8-bit unsigned ints.
static constexpr Gregorian year_start_for(const AstronomicalYear year)
Definition gregorian.hpp:75
Here is the call graph for this function:

◆ init()

void mtcore::calendars::systems::Gregorian::init ( i32 year,
Month month,
u8 day )
inline

Definition at line 51 of file gregorian.hpp.

51 {
52 this->year = AstronomicalYear{year};
53 this->month = month;
54 this->day = day;
55 }

◆ is_leap_year()

static constexpr bool mtcore::calendars::systems::Gregorian::is_leap_year ( const AstronomicalYear year)
inlinestaticnodiscardconstexpr

Definition at line 67 of file gregorian.hpp.

67 {
68 const auto y = static_cast<YearBase>(year);
69 const auto yearMod400 = math::mod(y, 400);
70 return math::mod(y, 4) == 0 && yearMod400 != 100 && yearMod400 != 200 && yearMod400 != 300;
71 }
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:

◆ leap_year()

bool mtcore::calendars::systems::Gregorian::leap_year ( ) const
inlinenodiscardconstexpr

Definition at line 73 of file gregorian.hpp.

73{ return is_leap_year(this->year); }
Here is the call graph for this function:

◆ operator<=>()

std::strong_ordering mtcore::calendars::systems::Gregorian::operator<=> ( const Gregorian & g) const
inlinenodiscardconstexpr

Definition at line 57 of file gregorian.hpp.

57 {
58 if (const auto c = year <=> g.year; c != std::strong_ordering::equivalent) {
59 return c;
60 }
61 if (const auto c = month <=> g.month; c != std::strong_ordering::equivalent) {
62 return c;
63 }
64 return day <=> g.day;
65 }

◆ to_fixed()

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

Definition at line 87 of file gregorian.hpp.

87 {
88 return Fixed{.day = EPOCH - 1 + 365 * (static_cast<YearBase>(year) - 1) +
89 math::floor<double, i32>(static_cast<double>(static_cast<YearBase>(year) - 1) / 4.0) -
90 math::floor<double, i32>(static_cast<double>(static_cast<YearBase>(year) - 1) / 100.0) +
91 math::floor<double, i32>(static_cast<double>(static_cast<YearBase>(year) - 1) / 400.0) +
92 math::floor<double, i32>((367. * static_cast<double>(month) - 362.) / 12.0) +
93 (static_cast<i32>(month) <= 2 ? 0
94 : is_leap_year(year) ? -1
95 : -2) +
96 static_cast<i32>(day)};
97 }
int32_t i32
Alias for 32-bit ints.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ year_end()

Gregorian mtcore::calendars::systems::Gregorian::year_end ( ) const
inlinenodiscardconstexpr

Definition at line 85 of file gregorian.hpp.

85{ return year_start_for(this->year); }
Here is the call graph for this function:

◆ year_end_for()

static constexpr Gregorian mtcore::calendars::systems::Gregorian::year_end_for ( const AstronomicalYear year)
inlinestaticnodiscardconstexpr

Definition at line 81 of file gregorian.hpp.

81 {
82 return {.year = year, .month = Month::December, .day = 31};
83 }

◆ year_start()

Gregorian mtcore::calendars::systems::Gregorian::year_start ( ) const
inlinenodiscardconstexpr

Definition at line 79 of file gregorian.hpp.

79{ return year_start_for(this->year); }
Here is the call graph for this function:

◆ year_start_for()

static constexpr Gregorian mtcore::calendars::systems::Gregorian::year_start_for ( const AstronomicalYear year)
inlinestaticnodiscardconstexpr

Definition at line 75 of file gregorian.hpp.

75 {
76 return {.year = year, .month = Month::January, .day = 1};
77 }
Here is the caller graph for this function:

Member Data Documentation

◆ day

u8 mtcore::calendars::systems::Gregorian::day = 1

Definition at line 49 of file gregorian.hpp.

◆ EPOCH

auto mtcore::calendars::systems::Gregorian::EPOCH = GREGORIAN_EPOCH
staticconstexpr

Definition at line 29 of file gregorian.hpp.

◆ month

Month mtcore::calendars::systems::Gregorian::month = Month::January

Definition at line 48 of file gregorian.hpp.

◆ name

std::string_view mtcore::calendars::systems::Gregorian::name = "Gregorian"
staticconstexpr

Definition at line 30 of file gregorian.hpp.

◆ year

AstronomicalYear mtcore::calendars::systems::Gregorian::year = {}

Definition at line 47 of file gregorian.hpp.

47{};

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