Skip to content

Date class

Colin Girling edited this page Feb 19, 2017 · 9 revisions

Types

typedef uint32_t size_type;
typedef int32_t diff_type;
typedef uint8_t day_type;
typedef uint8_t day_of_week_type;
typedef uint8_t month_type;
typedef uint16_t year_type;

Constants

static day_type const MIN_DAY = 1U;
static day_type const MAX_DAY = 31U;
static day_type const DAYS_IN_LEAP_FEBRUARY = 29U;

static size_type const DAYS_PER_YEAR = 365U;
static size_type const DAYS_PER_LEAP_YEAR = 366U;

static day_type const DAYS_IN_JANUARY = 31U;
static day_type const DAYS_IN_FEBRUARY = 28U;
static day_type const DAYS_IN_MARCH = 31U;
static day_type const DAYS_IN_APRIL = 30U;
static day_type const DAYS_IN_MAY = 31U;
static day_type const DAYS_IN_JUNE = 30U;
static day_type const DAYS_IN_JULY = 31U;
static day_type const DAYS_IN_AUGUST = 31U;
static day_type const DAYS_IN_SEPTEMBER = 30U;
static day_type const DAYS_IN_OCTOBER = 31U;
static day_type const DAYS_IN_NOVEMBER = 30U;
static day_type const DAYS_IN_DECEMBER = 31U;

static size_type const MONTHS_PER_YEAR = 12U;

static year_type const DEFAULT_YEAR = 1970U;

static day_of_week_type const SUNDAY = 0U;
static day_of_week_type const MONDAY = 1U;
static day_of_week_type const TUESDAY = 2U;
static day_of_week_type const WEDNESDAY = 3U;
static day_of_week_type const THURSDAY = 4U;
static day_of_week_type const FRIDAY = 5U;
static day_of_week_type const SATURDAY = 6U;

static month_type const JANUARY = 1U;
static month_type const FEBRUARY = 2U;
static month_type const MARCH = 3U;
static month_type const APRIL = 4U;
static month_type const MAY = 5U;
static month_type const JUNE = 6U;
static month_type const JULY = 7U;
static month_type const AUGUST = 8U;
static month_type const SEPTEMBER = 9U;
static month_type const OCTOBER = 10U;
static month_type const NOVEMBER = 11U;
static month_type const DECEMBER = 12U;

static month_type const MIN_MONTH = JANUARY;
static month_type const MAX_MONTH = DECEMBER;

Constructors

Date() throw(); // Default to 1st January 1970

Date(day_type day,
     month_type month,
     year_type year) throw();

Date(Date const& date) throw();

Operators

Date& operator =(Date const& date) throw();
bool operator <(Date const& other) const throw();
bool operator <=(Date const& other) const throw();
bool operator >(Date const& other) const throw();
bool operator >=(Date const& other) const throw();
bool operator ==(Date const& other) const throw();
bool operator !=(Date const& other) const throw();
Date& operator +=(size_type days_to_add) throw(); // Add milliseconds.
Date& operator -=(size_type days_to_subtract) throw(); // Subtract milliseconds.
Date operator +(size_type days) throw(); // Add milliseconds.
Date operator -(size_type days) const throw(); // Subtract milliseconds.
diff_type operator -(Date const& other) const throw(); // Get difference between two dates.
Date operator++() throw(); // Prefix add milliseconds.
Date operator++(int) throw(); // Postfix add milliseconds.
Date operator--() throw(); // Prefix subtract milliseconds.
Date operator--(int) throw(); // Postfix subtract milliseconds.

Static member functions

static uint32_t GetDate(day_type day, month_type month, year_type year) throw();
static void SetDate(uint32_t date, day_type& day, month_type& month, year_type& year) throw();
static bool IsLeapYear(year_type year) throw();
static bool IsLeapMonth(month_type month, year_type year) throw();

// Get days in the month, ignoring leap year.
static day_type GetDaysInMonth(month_type month) throw();

// Get days in the month, adjusting for the leap year and month.
static day_type GetDaysInMonth(month_type month, year_type year) throw();

/// Get number of days in the months from start to end month, adjusting for the leap year.
static size_type GetDaysInMonths(month_type start_month, month_type end_month, year_type year) throw();

static month_type GetNextMonth(month_type month) throw();
static void SetToNextMonth(month_type& month, year_type& year) throw();
static month_type GetPreviousMonth(month_type month) throw();
static void SetToPreviousMonth(month_type& month, year_type& year) throw();
static void SetToStartOfYear(day_type& day, month_type& month); // Set to 1st January.
static void SetToEndOfYear(day_type& day, month_type& month); // Set to 31st December.
static day_type GetDaysToEndOfMonth(day_type day, month_type month) throw();

// Get number of days to end of the month, adjusting for leap year.
static day_type GetDaysToEndOfMonth(day_type day,
                                    month_type month,
                                    year_type year) throw();

// Get number of days in the year, adjusting for the leap year.
static size_type GetDaysInYear(year_type year) throw();

// Get number of days in the years from start to end year, adjusting for the leap year.
static size_type GetDaysInYears(year_type start_year, year_type end_year) throw();

// Get days from 1st of January to current day and month for the year,
// adjusting for the leap year and month.
static size_type GetDaysFromStartOfYear(day_type day,
                                        month_type month,
                                        year_type year) throw();

// Get number of days to end of year from current date, adjusting for leap year.
static size_type GetDaysToEndOfYear(day_type day, month_type month, year_type year) throw();

// If the next day is less than the provided day, then it will be 1,
// as it has wrapped past end of month, ignoring leap year.
static day_type GetNextDay(day_type day, month_type month) throw();

// If the next day is less than the provided day, then it will be 1,
// as it has wrapped past end of month, adjusting for the leap year and month.
static day_type GetNextDay(day_type day, month_type month, year_type year) throw();

// If the previous day is more than the provided day,
// then it will be maximum day for the previous month,
// as it has wrapped past start of month, ignoring leap year.
static day_type GetPreviousDay(day_type day, month_type month) throw();

// If the previous day is more than the provided day,
// then it will be maximum day for the previous month,
// as it has wrapped past start of month, adjusting for the leap year and month.
static day_type GetPreviousDay(day_type day, month_type month, year_type year) throw();

// Calculates the number of days for next whole years,
// and days to end of the current year.
// If the user doesn't want to include the current year,
// then start from the 1st of January of the following year.
// Note that year will be updated to be the year past the counted days.
// The days returned will be the number of days up to the 1st of January,
// where there were not enough days remaining to complete a full year.
static size_type AddDaysForYears(size_type days,
                                 day_type &day,
                                 month_type& month,
                                 year_type& year) throw();

// Calculates the number of days for next whole months,
// and days to end of the current month.
// If the user doesn't want to include the current month,
// then start from the 1st of the following month.
// Note that month will be updated to be the month past the counted days.
// The days returned will be the number of days up to the 1st of the last month,
// where there were not enough days remaining to complete a full month.
static size_type AddDaysForMonths(size_type days,
                                  day_type& day,
                                  month_type &month,
                                  year_type& year) throw();

// Calculates the number of days for previous whole years,
// and days to start of the current year.
// If the user doesn't want to include the current year,
// then start from the 1st of January of the current year.
// Note that year will be updated to be the year before the counted days.
// The days returned will be the number of days up to the 1st of January,
// where there were not enough days remaining to complete a full year.
static size_type SubtractDaysForYears(size_type days,
                                      day_type& day,
                                      month_type& month,
                                      year_type& year) throw();

// Calculates the number of days for previous whole months,
// and days to start of the current month.
// If the user doesn't want to include the current month,
// then start from the 1st of the current month.
// Note that month will be updated to be the month past the counted days.
// The days returned will be the number of days up to the 1st of the last month,
// where there were not enough days remaining to complete a full month.
static size_type SubtractDaysForMonths(size_type days,
                                       day_type& day,
                                       month_type& month,
                                       year_type& year) throw();

// Get number of days between two dates for the same year.
// First date must be less or equal to second date.
static size_type GetDaysDifference(day_type first_day,
                                   month_type first_month,
                                   day_type second_day,
                                   month_type second_month,
                                   year_type year) throw();

// Get number of days between two dates.
// First date must be less or equal to second date.
static size_type GetDaysDifference(day_type first_day,
                                   month_type first_month,
                                   year_type first_year,
                                   day_type second_day,
                                   month_type second_month,
                                   year_type second_year) throw();

Member functions

day_type GetDay() const throw();
void SetDay(day_type day) throw();
month_type GetMonth() const throw();
void SetMonth(month_type month) throw();
year_type GetYear() const throw();
void SetYear(year_type year) throw();
bool IsLeapYear() const throw();
year_type GetNextLeapYear() const throw();
day_type GetDaysInMonth() const throw();
size_type GetDaysToEndOfMonth() const throw();
size_type GetDaysFromStartOfYear() const throw();
size_type GetDaysToEndOfYear() const throw();
size_type GetDaysInYear() const throw();
size_type GetDifferenceInDays(Date const& other) throw();
void SetStartOfYear() throw();
void SetStartOfYear(size_type& days_to_start) throw();
void SetEndtOfYear() throw();
void SetEndtOfYear(size_type& days_to_end) throw();
uint32_t GetDate() const throw(); // Get date as 32-bit value.
void SetDate(uint32_t date) throw(); // Set date from 32-bit value.
void SetDate(Date const& date) throw();
void SetDate(day_type day, month_type month, year_type year) throw();
void IncrementDay() throw();
void DecrementDay() throw();
void IncrementMonth() throw();
void DecrementMonth() throw();
void AddDays(size_type days);
void SubtractDays(size_type days);
int Compare(Date const& date) const throw();
Clone this wiki locally