AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::date::Date Class Reference

Calendar day (RFC 3339 full-date , JSON schema and OpenAPI date format, XSD xs:date ). More...

#include <date.hpp>

Collaboration diagram for anch::date::Date:

Public Member Functions

constexpr Date () noexcept
 Date (int32_t year, unsigned month, unsigned day)
 Date (const std::chrono::year_month_day &date)
 Date (const std::chrono::sys_days &days)
std::string format () const
constexpr int32_t year () const noexcept
constexpr unsigned month () const noexcept
constexpr unsigned day () const noexcept
unsigned weekday () const noexcept
unsigned dayOfYear () const noexcept
constexpr bool isLeapYear () const noexcept
Date plusDays (int64_t days) const
Date plusMonths (int64_t months) const
Date plusYears (int64_t years) const
int64_t daysUntil (const Date &other) const noexcept
anch::date::DateTime startOfDay (anch::date::Zone zone=anch::date::Zone::LOCAL) const
anch::date::DateTime at (const anch::date::Time &time, anch::date::Zone zone=anch::date::Zone::LOCAL) const
anch::date::DateTime at (const anch::date::OffsetTime &time) const
constexpr std::strong_ordering operator<=> (const Date &other) const noexcept
constexpr bool operator== (const Date &other) const noexcept
constexpr operator std::chrono::year_month_day () const noexcept
 operator std::chrono::sys_days () const noexcept

Static Public Member Functions

static Date today (anch::date::Zone zone=anch::date::Zone::LOCAL)
static Date parse (std::string_view text)

Detailed Description

Calendar day (RFC 3339 full-date , JSON schema and OpenAPI date format, XSD xs:date ).

A Date is a day of the proleptic Gregorian calendar, without time zone, from 0000-01-01 to 9999-12-31: it is always valid (impossible dates are refused when it is built), so that formatting can not fail. It is not a point in time ( DateTime ): conversions between them give the time zone ( DateTime::date , startOfDay ).
Dates are ordered (comparison operators), days, months and years can be added, and dates are converted from and to std::chrono::year_month_day and std::chrono::sys_days :

#include "date/date.hpp"
bool
dateExample() {
anch::date::Date birth(2024, 2, 29); // impossible dates are refused
anch::date::Date today = anch::date::Date::today(); // local day
anch::date::Date end = birth.plusMonths(1).plusDays(-1); // 2024-03-28
bool past = birth < today; // dates are ordered
int64_t days = birth.daysUntil(anch::date::Date::parse("2025-02-28")); // 365
anch::date::DateTime midnight = birth.startOfDay(anch::date::Zone::UTC);
std::cout << end << " " << past << " " << days << std::endl; // 2024-03-28 1 365
return end.format() == "2024-03-28" && days == 365 && midnight.date(anch::date::Zone::UTC) == birth;
}
Author
Vincent Lachenal
Since
0.3

Constructor & Destructor Documentation

◆ Date() [1/4]

anch::date::Date::Date ( )
constexprnoexcept

Date default constructor: 1970-01-01

◆ Date() [2/4]

anch::date::Date::Date ( int32_t year,
unsigned month,
unsigned day )

Date constructor

Parameters
yearthe year (0 to 9999)
monththe month (1 to 12)
daythe day of the month (1 to 31)
Exceptions
std::invalid_argumentimpossible date or year out of range
Here is the call graph for this function:

◆ Date() [3/4]

anch::date::Date::Date ( const std::chrono::year_month_day & date)
explicit

Date constructor

Parameters
datethe day
Exceptions
std::invalid_argumentimpossible date ( ok() is false ) or year out of range
Here is the call graph for this function:

◆ Date() [4/4]

anch::date::Date::Date ( const std::chrono::sys_days & days)
explicit

Date constructor

Parameters
daysthe days since epoch
Exceptions
std::invalid_argumentyear out of range
Here is the call graph for this function:

Member Function Documentation

◆ at() [1/2]

anch::date::DateTime anch::date::Date::at ( const anch::date::OffsetTime & time) const

Point in time of a time of this day with its offset (no time zone is needed)

Parameters
timethe time of day and its offset from UTC
Returns
the point in time ( 2026-11-30 at 12:00+01:00 is 2026-11-30T11:00:00Z )
Exceptions
std::invalid_argumentpoint in time out of DateTime range (years 1678 to 2261)
Since
0.3
Here is the call graph for this function:

◆ at() [2/2]

anch::date::DateTime anch::date::Date::at ( const anch::date::Time & time,
anch::date::Zone zone = anch::date::Zone::LOCAL ) const

Point in time of a time of this day

Parameters
timethe time of day
zonethe time zone (default local time ; a local time which does not exist because of a daylight saving time change is normalized as std::mktime does)
Returns
the point in time
Exceptions
std::invalid_argumentpoint in time out of DateTime range (years 1678 to 2261)
Since
0.3
Here is the call graph for this function:

◆ day()

unsigned anch::date::Date::day ( ) const
constexprnoexcept

Day of the month getter

Returns
the day of the month (1 to 31)
Here is the call graph for this function:

◆ dayOfYear()

unsigned anch::date::Date::dayOfYear ( ) const
noexcept

Day of the year

Returns
the day of the year (1 to 366)
Here is the call graph for this function:

◆ daysUntil()

int64_t anch::date::Date::daysUntil ( const Date & other) const
noexcept

Number of days to another date

Parameters
otherthe other date
Returns
the number of days from this date to other (negative when other is before)
Here is the call graph for this function:

◆ format()

std::string anch::date::Date::format ( ) const

Format RFC 3339 full-date ( YYYY-MM-DD )

Returns
the formatted date
Here is the call graph for this function:

◆ isLeapYear()

bool anch::date::Date::isLeapYear ( ) const
constexprnoexcept

Check leap year

Returns
true when the year of the date is a leap year
Here is the call graph for this function:

◆ month()

unsigned anch::date::Date::month ( ) const
constexprnoexcept

Month getter

Returns
the month (1 to 12)
Here is the call graph for this function:

◆ operator std::chrono::sys_days()

anch::date::Date::operator std::chrono::sys_days ( ) const
explicitnoexcept

Convert to std::chrono::sys_days

Returns
the days since epoch
Here is the call graph for this function:

◆ operator std::chrono::year_month_day()

anch::date::Date::operator std::chrono::year_month_day ( ) const
explicitconstexprnoexcept

Convert to std::chrono::year_month_day

Returns
the day
Here is the call graph for this function:

◆ operator<=>()

std::strong_ordering anch::date::Date::operator<=> ( const Date & other) const
constexprnoexcept

Compare dates

Parameters
otherthe other date
Returns
the chronological order
Here is the call graph for this function:

◆ operator==()

bool anch::date::Date::operator== ( const Date & other) const
constexprnoexcept

Check dates equality

Parameters
otherthe other date
Returns
true when dates are the same day
Here is the call graph for this function:

◆ parse()

Date anch::date::Date::parse ( std::string_view text)
static

Parse RFC 3339 full-date ( YYYY-MM-DD )

Parameters
textthe text to parse
Returns
the date
Exceptions
std::invalid_argumentinvalid format or impossible date
Here is the call graph for this function:

◆ plusDays()

Date anch::date::Date::plusDays ( int64_t days) const

Add days

Parameters
daysthe number of days (negative to subtract)
Returns
the new date
Exceptions
std::invalid_argumentyear out of range
Here is the call graph for this function:

◆ plusMonths()

Date anch::date::Date::plusMonths ( int64_t months) const

Add months: the day is the last day of the new month when the month is shorter (2024-01-31 plus 1 month is 2024-02-29)

Parameters
monthsthe number of months (negative to subtract)
Returns
the new date
Exceptions
std::invalid_argumentyear out of range
Here is the call graph for this function:

◆ plusYears()

Date anch::date::Date::plusYears ( int64_t years) const

Add years: February 29 is February 28 when the new year is not a leap year

Parameters
yearsthe number of years (negative to subtract)
Returns
the new date
Exceptions
std::invalid_argumentyear out of range
Here is the call graph for this function:

◆ startOfDay()

anch::date::DateTime anch::date::Date::startOfDay ( anch::date::Zone zone = anch::date::Zone::LOCAL) const

Point in time of the start of the day (midnight ; the first existing time when a daylight saving time change skips midnight)

Parameters
zonethe time zone (default local time)
Returns
the point in time
Exceptions
std::invalid_argumentpoint in time out of DateTime range (years 1678 to 2261)
Here is the call graph for this function:

◆ today()

Date anch::date::Date::today ( anch::date::Zone zone = anch::date::Zone::LOCAL)
static

Current day

Parameters
zonethe time zone of the day (default local time)
Returns
the current day
Here is the call graph for this function:

◆ weekday()

unsigned anch::date::Date::weekday ( ) const
noexcept

Day of the week (ISO 8601)

Returns
the day of the week (1 is monday, 7 is sunday)
Here is the call graph for this function:

◆ year()

int32_t anch::date::Date::year ( ) const
constexprnoexcept

Year getter

Returns
the year (0 to 9999)
Here is the call graph for this function:

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