#include <Pt/Date.h>
Date expressed in year, month, and day. More...
Public Member Functions | |
Date () | |
Default constructor. More... | |
Date (const Date &date) | |
Copy constructor. | |
Date (int y, unsigned m, unsigned d) | |
Constructs a Date from given values. More... | |
Date (unsigned julianDays) | |
Constructs a Date from a julian day. | |
void | addDays (int n) |
Adds n days to the date. | |
void | addMonths (int n) |
Adds n months to the date. | |
void | addYears (int n) |
Adds n years to the date. | |
unsigned | day () const |
Returns the day-part of the date. | |
unsigned | dayOfWeek () const |
Return day of the week, starting with sunday. | |
unsigned | dayOfYear () const |
Returns the day of the year. | |
unsigned | daysInMonth () const |
Returns the days of the month of the date. | |
void | get (int &year, unsigned &month, unsigned &day) const |
Gets the year, month and day. | |
bool | isLeapYear () const |
Returns true if the date is in a leap year. | |
unsigned | julian () const |
Returns the Date as a julian day. | |
unsigned | month () const |
Returns the month-part of the date. | |
Date & | operator++ () |
Increments the date by one day. | |
Date & | operator+= (int days) |
Add days to the date. | |
Date & | operator-- () |
Decrements the date by one day. | |
Date & | operator-= (int days) |
Substract days from the date. | |
Date & | operator= (const Date &date) |
Assignment operator. | |
void | set (int y, unsigned m, unsigned d) |
Sets the date to a year, month and day. More... | |
void | setJulian (unsigned d) |
Sets the Date to a julian day. | |
std::string | toIsoString () const |
Returns the date in ISO-format. More... | |
int | year () const |
Returns the year-part of the date. | |
Static Public Member Functions | |
static Date | fromIsoString (const std::string &s) |
Interprets a string as a date-string in ISO-format. More... | |
static bool | isLeapYear (int year) |
Returns true if the year is in a leap year. | |
static bool | isValid (int y, int m, int d) |
Returns true if values describe a valid date. | |
Static Public Attributes | |
static const unsigned | DaysOfApr = 30 |
The number of days of a April. | |
static const unsigned | DaysOfAug = 31 |
The number of days of a August. | |
static const unsigned | DaysOfDec = 31 |
The number of days of a December. | |
static const unsigned | DaysOfFeb = 28 |
The number of days of a February. | |
static const unsigned | DaysOfJan = 31 |
The number of days of a January. | |
static const unsigned | DaysOfJul = 31 |
The number of days of a July. | |
static const unsigned | DaysOfJun = 30 |
The number of days of a June. | |
static const unsigned | DaysOfLeapFeb = 29 |
The number of days of a February in a leap year. | |
static const unsigned | DaysOfMar = 31 |
The number of days of a March. | |
static const unsigned | DaysOfMay = 31 |
The number of days of a May. | |
static const unsigned | DaysOfNov = 30 |
The number of days of a November. | |
static const unsigned | DaysOfOct = 31 |
The number of days of a October. | |
static const unsigned | DaysOfSep = 30 |
The number of days of a September. | |
static const unsigned | DaysPerLeapYear = 366 |
The number of days of a leap year. | |
static const unsigned | DaysPerYear = 365 |
The number of days of an ordinary year. | |
Related Functions | |
bool | operator!= (const Date &a, const Date &b) |
Returns true if the dates are not equal. | |
Date | operator+ (const Date &d, int days) |
Add days to a date. | |
Date | operator+ (int days, const Date &d) |
Add days to a date. | |
int | operator- (const Date &a, const Date &b) |
Subtract two dates. | |
bool | operator< (const Date &a, const Date &b) |
Less-than comparison operator. | |
void | operator<<= (SerializationInfo &si, const Date &date) |
Serialize a date. | |
bool | operator<= (const Date &a, const Date &b) |
Less-than-equal comparison operator. | |
bool | operator== (const Date &a, const Date &b) |
Returns true if the dates are equal. | |
bool | operator> (const Date &a, const Date &b) |
Greater-than comparison operator. | |
bool | operator>= (const Date &a, const Date &b) |
Greater-than-equal comparison operator. | |
void | operator>>= (const SerializationInfo &si, Date &date) |
Deserialize a date. | |
Pt::Date is an easy way to handle calendar dates. It can be constructed from the days, month and year components or from an ISO string using fromIsoString(). The date components can be accessed with day(), month() and year(). Once a Date object is created, calendar information can be accessed, for example with dayOfYear(), dayOfWeek() or isLeapYear(). Dates can be compared and a Date is considered less if it is earlier than another Date. It is also possible to add or subtract days from a Date, which yields a new date or modifies it. Dates can be subtracted, which yields the number of days between them, as shown in the next example:
Pt::InvalidDate is thrown, if a date could not be constructed, for example if one of the date components is out of range. To avoid an exception, date components can be validated with isValid().
Date | ( | ) |
The default constructed date is undefined.
Date | ( | int | y, |
unsigned | m, | ||
unsigned | d | ||
) |
Sets the date to a new year, month and day. InvalidDate is thrown if any of the values is out of range
void set | ( | int | y, |
unsigned | m, | ||
unsigned | d | ||
) |
Sets the date to a new year, month and day. InvalidDate is thrown if any of the values is out of range
std::string toIsoString | ( | ) | const |
Converts the date in ISO-format (yyyy-mm-dd).