date
a C++20 date library
Loading...
Searching...
No Matches
date.h
Go to the documentation of this file.
1#ifndef CGR_DATE_H
2#define CGR_DATE_H
3
8
9#include <compare>
10#include <cstddef>
11#include <ctime>
12#include <exception>
13#include <iosfwd>
14#include <string>
15#include <string_view>
16
17namespace cgr
18{
20 class Date {
21 public:
22 static constexpr int MIN_YEAR{ 1900 };
23 static constexpr int MAX_YEAR{ 9999 };
24
26 class DateError : public std::exception {
27 public:
40
46 DateError(Reason reason, std::string message);
47
49 [[nodiscard]] const char *what() const noexcept override;
50
52 [[nodiscard]] Reason GetReason() const noexcept;
53 private:
54 Reason m_reason;
55 std::string m_message;
56 };
57
59 struct ISOWeek {
60 int year;
61 int week;
62 [[nodiscard]] bool operator==(const ISOWeek &rhs) const noexcept = default;
63 };
64
69 [[nodiscard]] static Date Today();
70
78 [[nodiscard]] static Date RandomDate(int minYear = MIN_YEAR, int maxYear = Today().Year());
79
81 Date() = default;
82
93 Date(int day, int month, int year);
94
104 explicit Date(std::string_view str);
105
112 explicit Date(std::time_t timer);
113
115 Date(std::nullptr_t) = delete;
116
118 [[nodiscard]] int Day() const noexcept;
119
121 [[nodiscard]] int Month() const noexcept;
122
124 [[nodiscard]] int Year() const noexcept;
125
127 [[nodiscard]] int DayOfYear() const noexcept;
128
130 [[nodiscard]] int Weekday() const noexcept;
131
133 [[nodiscard]] ISOWeek WeekOfYear() const noexcept;
134
141 Date &Day(int day) &;
142
149 Date &Month(int month) &;
150
157 Date &Year(int year) &;
158
166 Date &operator+=(int days) &;
167
175 Date &operator-=(int days) &;
176
182 Date &operator++() &;
183
190 Date operator++(int) &;
191
197 Date &operator--() &;
198
205 Date operator--(int) &;
206
208 [[nodiscard]] std::strong_ordering operator<=>(const Date &rhs) const noexcept;
209
211 [[nodiscard]] bool operator==(const Date &rhs) const noexcept = default;
212
218 [[nodiscard]] static bool IsLeap(int year);
219 private:
220 int m_day{ 1 };
221 int m_month{ 1 };
222 int m_year{ MIN_YEAR };
223 };
224
233 [[nodiscard]] Date operator+(const Date &d, int days);
234
243 [[nodiscard]] Date operator+(int days, const Date &d);
244
253 [[nodiscard]] Date operator-(const Date &d, int days);
254
259 [[nodiscard]] int operator-(const Date &lhs, const Date &rhs) noexcept;
260
265 std::istream &operator>>(std::istream &is, Date &d);
266
271 std::ostream &operator<<(std::ostream &os, const Date &d);
272}
273
274#endif // CGR_DATE_H
Reason GetReason() const noexcept
Returns the exception reason.
DateError(Reason reason, std::string message)
Constructs a DateError from a reason and an error message.
Reason
Indicates the reason for a DateError exception.
Definition date.h:29
@ INVALID_DATE
Invalid day/month/year combination.
Definition date.h:33
@ INVALID_FORMAT
Invalid date string format.
Definition date.h:34
@ INVALID_MONTH
Month is out of valid range [1, 12].
Definition date.h:31
@ EPOCH_FAILURE
Failed time_t conversion.
Definition date.h:36
@ SYSTEM_FAILURE
Failed system operation.
Definition date.h:38
@ INVALID_DAY
Day is out of valid range [1, 31].
Definition date.h:30
@ OUT_OF_RANGE
Resulting date is out of [01/01/MIN_YEAR, 31/12/MAX_YEAR].
Definition date.h:35
@ INVALID_YEAR
Year is out of valid range [MIN_YEAR, MAX_YEAR].
Definition date.h:32
@ LOGIC_ERROR
Logically invalid argument.
Definition date.h:37
const char * what() const noexcept override
Returns the exception message.
static bool IsLeap(int year)
Queries whether a year is leap.
int Month() const noexcept
Returns the month.
Date(int day, int month, int year)
Constructs a date from a day, a month and a year.
int operator-(const Date &lhs, const Date &rhs) noexcept
Returns the number of days between two dates.
int Year() const noexcept
Returns the year.
int Day() const noexcept
Returns the day.
ISOWeek WeekOfYear() const noexcept
Returns the week of year according to ISO 8601.
Date(std::string_view str)
Constructs a date by parsing a string in dd/mm/yyyy format.
static constexpr int MAX_YEAR
Maximum supported year.
Definition date.h:23
Date operator+(const Date &d, int days)
Returns the result of moving a date forward by the given number of days.
Date()=default
Constructs a date set to 01/01/1900.
int DayOfYear() const noexcept
Returns the day of year.
static Date RandomDate(int minYear=MIN_YEAR, int maxYear=Today().Year())
Returns a random date within the given year range.
Date operator-(const Date &d, int days)
Returns the result of moving a date back by the given number of days.
static Date Today()
Returns today's date.
std::istream & operator>>(std::istream &is, Date &d)
Extracts a date in dd/mm/yyyy format from an input stream.
Date(std::time_t timer)
Constructs a date from a time since epoch.
Date operator+(int days, const Date &d)
Returns the result of moving a date forward by the given number of days.
Date(std::nullptr_t)=delete
Prevents construction from nullptr.
int Weekday() const noexcept
Returns the weekday.
bool operator==(const Date &rhs) const noexcept=default
Checks two dates for equality.
static constexpr int MIN_YEAR
Minimum supported year.
Definition date.h:22
std::ostream & operator<<(std::ostream &os, const Date &d)
Inserts a date into an output stream (e.g. 12 December 2024 Thursday).
ISO 8601 week date.
Definition date.h:59
int year
ISO week-numbering year.
Definition date.h:60
int week
ISO week number [1, 53].
Definition date.h:61