Calendar day (RFC 3339 full-date , JSON schema and OpenAPI date format, XSD xs:date ).
More...
#include <date.hpp>
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() {
bool past = birth < today;
std::cout << end << " " << past << " " << days << std::endl;
}
- Author
- Vincent Lachenal
- Since
- 0.3
◆ 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
-
| year | the year (0 to 9999) |
| month | the month (1 to 12) |
| day | the day of the month (1 to 31) |
- Exceptions
-
| std::invalid_argument | impossible date or year out of range |
◆ Date() [3/4]
| anch::date::Date::Date |
( |
const std::chrono::year_month_day & | date | ) |
|
|
explicit |
Date constructor
- Parameters
-
- Exceptions
-
| std::invalid_argument | impossible date ( ok() is false ) or year out of range |
◆ Date() [4/4]
| anch::date::Date::Date |
( |
const std::chrono::sys_days & | days | ) |
|
|
explicit |
Date constructor
- Parameters
-
- Exceptions
-
| std::invalid_argument | year out of range |
◆ at() [1/2]
Point in time of a time of this day with its offset (no time zone is needed)
- Parameters
-
| time | the 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_argument | point in time out of DateTime range (years 1678 to 2261) |
- Since
- 0.3
◆ at() [2/2]
Point in time of a time of this day
- Parameters
-
| time | the time of day |
| zone | the 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_argument | point in time out of DateTime range (years 1678 to 2261) |
- Since
- 0.3
◆ day()
| unsigned anch::date::Date::day |
( |
| ) |
const |
|
constexprnoexcept |
Day of the month getter
- Returns
- the day of the month (1 to 31)
◆ dayOfYear()
| unsigned anch::date::Date::dayOfYear |
( |
| ) |
const |
|
noexcept |
Day of the year
- Returns
- the day of the year (1 to 366)
◆ daysUntil()
| int64_t anch::date::Date::daysUntil |
( |
const Date & | other | ) |
const |
|
noexcept |
Number of days to another date
- Parameters
-
- Returns
- the number of days from this date to
other (negative when other is before)
◆ format()
| std::string anch::date::Date::format |
( |
| ) |
const |
Format RFC 3339 full-date ( YYYY-MM-DD )
- Returns
- the formatted date
◆ isLeapYear()
| bool anch::date::Date::isLeapYear |
( |
| ) |
const |
|
constexprnoexcept |
Check leap year
- Returns
true when the year of the date is a leap year
◆ month()
| unsigned anch::date::Date::month |
( |
| ) |
const |
|
constexprnoexcept |
Month getter
- Returns
- the month (1 to 12)
◆ 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
◆ 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
◆ operator<=>()
| std::strong_ordering anch::date::Date::operator<=> |
( |
const Date & | other | ) |
const |
|
constexprnoexcept |
Compare dates
- Parameters
-
- Returns
- the chronological order
◆ operator==()
| bool anch::date::Date::operator== |
( |
const Date & | other | ) |
const |
|
constexprnoexcept |
Check dates equality
- Parameters
-
- Returns
true when dates are the same day
◆ parse()
| Date anch::date::Date::parse |
( |
std::string_view | text | ) |
|
|
static |
Parse RFC 3339 full-date ( YYYY-MM-DD )
- Parameters
-
- Returns
- the date
- Exceptions
-
| std::invalid_argument | invalid format or impossible date |
◆ plusDays()
| Date anch::date::Date::plusDays |
( |
int64_t | days | ) |
const |
Add days
- Parameters
-
| days | the number of days (negative to subtract) |
- Returns
- the new date
- Exceptions
-
| std::invalid_argument | year out of range |
◆ 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
-
| months | the number of months (negative to subtract) |
- Returns
- the new date
- Exceptions
-
| std::invalid_argument | year out of range |
◆ 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
-
| years | the number of years (negative to subtract) |
- Returns
- the new date
- Exceptions
-
| std::invalid_argument | year out of range |
◆ startOfDay()
Point in time of the start of the day (midnight ; the first existing time when a daylight saving time change skips midnight)
- Parameters
-
| zone | the time zone (default local time) |
- Returns
- the point in time
- Exceptions
-
| std::invalid_argument | point in time out of DateTime range (years 1678 to 2261) |
◆ today()
Current day
- Parameters
-
| zone | the time zone of the day (default local time) |
- Returns
- the current day
◆ 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)
◆ year()
| int32_t anch::date::Date::year |
( |
| ) |
const |
|
constexprnoexcept |
Year getter
- Returns
- the year (0 to 9999)
The documentation for this class was generated from the following file: