Date Class Reference

#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.
 
Dateoperator++ ()
 Increments the date by one day.
 
Dateoperator+= (int days)
 Add days to the date.
 
Dateoperator-- ()
 Decrements the date by one day.
 
Dateoperator-= (int days)
 Substract days from the date.
 
Dateoperator= (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.
 

Detailed Description

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:

#include <Pt/Date.h>
#include <iostream>
int daysUntilChristmas(const std::string& todayStr)
{
Pt::Date today = Pt::Date::fromIsoString(todayStr);
Pt::Date xmas(today.year(), 12, 24);
return xmas - today;
}

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().

Constructor & Destructor Documentation

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

Member Function Documentation

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).

Returns
Date as iso formated string.
static Date fromIsoString ( const std::string &  s)
static

Interprets a string as a date-string in ISO-format (yyyy-mm-dd) and returns a Date-object. When the string is not in ISO-format, an exception is thrown.

Parameters
sIso formated date string.
Returns
Date result
Exceptions
IllegalArgument