date
a C++20 date library
Loading...
Searching...
No Matches
cgr::Date Class Reference

Represents a calendar date. More...

#include <date.h>

Classes

class  DateError
 Exception thrown by the Date class. More...
struct  ISOWeek
 ISO 8601 week date. More...

Public Member Functions

 Date ()=default
 Constructs a date set to 01/01/1900.
 Date (int day, int month, int year)
 Constructs a date from a day, a month and a year.
 Date (std::string_view str)
 Constructs a date by parsing a string in dd/mm/yyyy format.
 Date (std::time_t timer)
 Constructs a date from a time since epoch.
 Date (std::nullptr_t)=delete
 Prevents construction from nullptr.
int Day () const noexcept
 Returns the day.
int Month () const noexcept
 Returns the month.
int Year () const noexcept
 Returns the year.
int DayOfYear () const noexcept
 Returns the day of year.
int Weekday () const noexcept
 Returns the weekday.
ISOWeek WeekOfYear () const noexcept
 Returns the week of year according to ISO 8601.
DateDay (int day) &
 Sets the day.
DateMonth (int month) &
 Sets the month.
DateYear (int year) &
 Sets the year.
Dateoperator+= (int days) &
 Moves the date forward by the given number of days.
Dateoperator-= (int days) &
 Moves the date back by the given number of days.
Dateoperator++ () &
 Increments the date by one day.
Date operator++ (int) &
 Increments the date by one day.
Dateoperator-- () &
 Decrements the date by one day.
Date operator-- (int) &
 Decrements the date by one day.
std::strong_ordering operator<=> (const Date &rhs) const noexcept
 Compares two dates chronologically.
bool operator== (const Date &rhs) const noexcept=default
 Checks two dates for equality.

Static Public Member Functions

static Date Today ()
 Returns today's date.
static Date RandomDate (int minYear=MIN_YEAR, int maxYear=Today().Year())
 Returns a random date within the given year range.
static bool IsLeap (int year)
 Queries whether a year is leap.

Static Public Attributes

static constexpr int MIN_YEAR { 1900 }
 Minimum supported year.
static constexpr int MAX_YEAR { 9999 }
 Maximum supported year.

(Note that these are not member symbols.)

Date operator+ (const Date &d, int days)
 Returns the result of moving a date forward by the given number of days.
Date operator+ (int days, const Date &d)
 Returns the result of moving a date forward by the given number of days.
Date operator- (const Date &d, int days)
 Returns the result of moving a date back by the given number of days.
int operator- (const Date &lhs, const Date &rhs) noexcept
 Returns the number of days between two dates.
std::istream & operator>> (std::istream &is, Date &d)
 Extracts a date in dd/mm/yyyy format from an input stream.
std::ostream & operator<< (std::ostream &os, const Date &d)
 Inserts a date into an output stream (e.g. 12 December 2024 Thursday).

Detailed Description

Represents a calendar date.

Constructor & Destructor Documentation

◆ Date() [1/5]

cgr::Date::Date ( )
default

Constructs a date set to 01/01/1900.

◆ Date() [2/5]

cgr::Date::Date ( int day,
int month,
int year )

Constructs a date from a day, a month and a year.

Parameters
dayThe day.
monthThe month.
yearThe year.
Exceptions
DateErrorIf day is out of valid range [1, 31] (Reason::INVALID_DAY).
DateErrorIf month is out of valid range [1, 12] (Reason::INVALID_MONTH).
DateErrorIf year is out of valid range [MIN_YEAR, MAX_YEAR] (Reason::INVALID_YEAR).
DateErrorIf day/month/year combination is invalid (Reason::INVALID_DATE).

◆ Date() [3/5]

cgr::Date::Date ( std::string_view str)
explicit

Constructs a date by parsing a string in dd/mm/yyyy format.

Parameters
strThe date string.
Exceptions
DateErrorIf str does not match the dd/mm/yyyy format (Reason::INVALID_FORMAT).
DateErrorIf day is out of valid range [1, 31] (Reason::INVALID_DAY).
DateErrorIf month is out of valid range [1, 12] (Reason::INVALID_MONTH).
DateErrorIf year is out of valid range [MIN_YEAR, MAX_YEAR] (Reason::INVALID_YEAR).
DateErrorIf day/month/year combination is invalid (Reason::INVALID_DATE).

◆ Date() [4/5]

cgr::Date::Date ( std::time_t timer)
explicit

Constructs a date from a time since epoch.

Parameters
timerThe time since epoch.
Exceptions
DateErrorIf conversion from the time since epoch to a date fails (Reason::EPOCH_FAILURE).
DateErrorIf the resulting date is out of [01/01/MIN_YEAR, 31/12/MAX_YEAR] (Reason::OUT_OF_RANGE).

◆ Date() [5/5]

cgr::Date::Date ( std::nullptr_t )
delete

Prevents construction from nullptr.

Member Function Documentation

◆ Today()

Date cgr::Date::Today ( )
staticnodiscard

Returns today's date.

Exceptions
DateErrorIf today's date cannot be obtained (Reason::SYSTEM_FAILURE).

◆ RandomDate()

Date cgr::Date::RandomDate ( int minYear = MIN_YEAR,
int maxYear = Today().Year() )
staticnodiscard

Returns a random date within the given year range.

Parameters
minYearThe minimum year.
maxYearThe maximum year.
Exceptions
DateErrorIf minYear or maxYear is out of [MIN_YEAR, MAX_YEAR] (Reason::INVALID_YEAR).
DateErrorIf minYear > maxYear (Reason::LOGIC_ERROR).

◆ Day() [1/2]

int cgr::Date::Day ( ) const
nodiscardnoexcept

Returns the day.

◆ Month() [1/2]

int cgr::Date::Month ( ) const
nodiscardnoexcept

Returns the month.

◆ Year() [1/2]

int cgr::Date::Year ( ) const
nodiscardnoexcept

Returns the year.

◆ DayOfYear()

int cgr::Date::DayOfYear ( ) const
nodiscardnoexcept

Returns the day of year.

◆ Weekday()

int cgr::Date::Weekday ( ) const
nodiscardnoexcept

Returns the weekday.

◆ WeekOfYear()

ISOWeek cgr::Date::WeekOfYear ( ) const
nodiscardnoexcept

Returns the week of year according to ISO 8601.

◆ Day() [2/2]

Date & cgr::Date::Day ( int day) &

Sets the day.

Parameters
dayThe day.
Exceptions
DateErrorIf day is out of valid range [1, 31] (Reason::INVALID_DAY).
Note
Strong exception guarantee.

◆ Month() [2/2]

Date & cgr::Date::Month ( int month) &

Sets the month.

Parameters
monthThe month.
Exceptions
DateErrorIf month is out of valid range [1, 12] (Reason::INVALID_MONTH).
Note
Strong exception guarantee.

◆ Year() [2/2]

Date & cgr::Date::Year ( int year) &

Sets the year.

Parameters
yearThe year.
Exceptions
DateErrorIf year is out of valid range [MIN_YEAR, MAX_YEAR] (Reason::INVALID_YEAR).
Note
Strong exception guarantee.

◆ operator+=()

Date & cgr::Date::operator+= ( int days) &

Moves the date forward by the given number of days.

Parameters
daysThe number of days.
Exceptions
DateErrorIf days is negative (Reason::LOGIC_ERROR).
DateErrorIf the resulting date exceeds 31/12/MAX_YEAR (Reason::OUT_OF_RANGE).
Note
Strong exception guarantee.

◆ operator-=()

Date & cgr::Date::operator-= ( int days) &

Moves the date back by the given number of days.

Parameters
daysThe number of days.
Exceptions
DateErrorIf days is negative (Reason::LOGIC_ERROR).
DateErrorIf the resulting date falls below 01/01/MIN_YEAR (Reason::OUT_OF_RANGE).
Note
Strong exception guarantee.

◆ operator++() [1/2]

Date & cgr::Date::operator++ ( ) &

Increments the date by one day.

Exceptions
DateErrorIf the resulting date exceeds 31/12/MAX_YEAR (Reason::OUT_OF_RANGE).
Note
Strong exception guarantee.

◆ operator++() [2/2]

Date cgr::Date::operator++ ( int ) &

Increments the date by one day.

Returns
The date before the increment.
Exceptions
DateErrorIf the resulting date exceeds 31/12/MAX_YEAR (Reason::OUT_OF_RANGE).
Note
Strong exception guarantee.

◆ operator--() [1/2]

Date & cgr::Date::operator-- ( ) &

Decrements the date by one day.

Exceptions
DateErrorIf the resulting date falls below 01/01/MIN_YEAR (Reason::OUT_OF_RANGE).
Note
Strong exception guarantee.

◆ operator--() [2/2]

Date cgr::Date::operator-- ( int ) &

Decrements the date by one day.

Returns
The date before the decrement.
Exceptions
DateErrorIf the resulting date falls below 01/01/MIN_YEAR (Reason::OUT_OF_RANGE).
Note
Strong exception guarantee.

◆ operator<=>()

std::strong_ordering cgr::Date::operator<=> ( const Date & rhs) const
nodiscardnoexcept

Compares two dates chronologically.

◆ operator==()

bool cgr::Date::operator== ( const Date & rhs) const
nodiscarddefaultnoexcept

Checks two dates for equality.

◆ IsLeap()

bool cgr::Date::IsLeap ( int year)
staticnodiscard

Queries whether a year is leap.

Parameters
yearThe year.
Exceptions
DateErrorIf the year is out of range [MIN_YEAR, MAX_YEAR] (Reason::INVALID_YEAR).

◆ operator+() [1/2]

Date operator+ ( const Date & d,
int days )
related

Returns the result of moving a date forward by the given number of days.

Parameters
dThe date.
daysThe number of days.
Exceptions
DateErrorIf days is negative (Reason::LOGIC_ERROR).
DateErrorIf the resulting date exceeds 31/12/MAX_YEAR (Reason::OUT_OF_RANGE).

◆ operator+() [2/2]

Date operator+ ( int days,
const Date & d )
related

Returns the result of moving a date forward by the given number of days.

Parameters
daysThe number of days.
dThe date.
Exceptions
DateErrorIf days is negative (Reason::LOGIC_ERROR).
DateErrorIf the resulting date exceeds 31/12/MAX_YEAR (Reason::OUT_OF_RANGE).

◆ operator-() [1/2]

Date operator- ( const Date & d,
int days )
related

Returns the result of moving a date back by the given number of days.

Parameters
dThe date.
daysThe number of days.
Exceptions
DateErrorIf days is negative (Reason::LOGIC_ERROR).
DateErrorIf the resulting date falls below 01/01/MIN_YEAR (Reason::OUT_OF_RANGE).

◆ operator-() [2/2]

int operator- ( const Date & lhs,
const Date & rhs )
related

Returns the number of days between two dates.

◆ operator>>()

std::istream & operator>> ( std::istream & is,
Date & d )
related

Extracts a date in dd/mm/yyyy format from an input stream.

◆ operator<<()

std::ostream & operator<< ( std::ostream & os,
const Date & d )
related

Inserts a date into an output stream (e.g. 12 December 2024 Thursday).

Member Data Documentation

◆ MIN_YEAR

int cgr::Date::MIN_YEAR { 1900 }
staticconstexpr

Minimum supported year.

◆ MAX_YEAR

int cgr::Date::MAX_YEAR { 9999 }
staticconstexpr

Maximum supported year.


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