Reference Manual

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# Inti::G::Date Class Reference

A GDate C++ wrapper interface. More...

`#include <inti/glib/date.h>`

Inheritance diagram for Inti::G::Date:

List of all members.

## Public Member Functions

Constructors

- Date ()
    Constructs a date and initializes it to a sane state.
- Date (DateDay day, DateMonth month, DateYear year)
    Construct a date and set the value for the date.
- Date (unsigned int julian_day)
    Construct a date and set the value for the date.
- Date (const GDate &date)
    Construct a date from an existing GDate.
- Date (const Date &src)
    Copy constructor.
- virtual ~Date ()
    Destructor.
- Date & operator= (const Date &src)
    Assignment operator.

Accessors

- GDate * g_date () const
    Get a pointer to the GDate object.
- operator GDate * () const
    Conversion operator; Safely converts a G::Date object into a GDate pointer.
- bool valid () const
    Determines whether the date represents an existing day.
- DateWeekday get_weekday () const
    Gets the day of the week for the date.
- DateDay get_day () const
    Gets the day of the month.
- DateMonth get_month () const
    Gets the month of the year.
- DateYear get_year () const
    Gets the year of the date.
- unsigned int get_julian () const
    Returns the Julian day or "serial number" of the date.
- unsigned int get_day_of_year () const
    Gets the day of the year, where January 1 is the first day of the year.
- unsigned int get_monday_week_of_year () const
    Gets the week of the year, where weeks are understood to start on Monday.
- unsigned int get_sunday_week_of_year () const
    Gets the week of the year during which this date falls, if weeks are understood to being on Sunday.
- bool is_first_of_month () const
    Determines whether the date is on the first of a month.
- bool is_last_of_month () const
    Determines whether the date is the last day of the month.
- int compare (const Date &other) const
    A qsort()-style comparsion method for dates.
- bool operator== (const Date &other) const
    Equality operator.
- bool operator!= (const Date &other) const
    Inequality operator.
- bool operator< (const Date &other) const
    Less-than operator.
- bool operator<= (const Date &other) const
    Less-than-or-equal-to operator.
- bool operator> (const Date &other) const
    Greater-than operator.
- bool operator>= (const Date &other) const
    Greater-than-or-equal-to operator.

Methods

- void clear ()
    Clears a date.
- void set_parse (const String &str)
    Parses the user-inputted string str, and try to figure out what date it represents, taking the current locale into account.
- void set_time (Time time)
    Sets the value of a date from a G::Time (time_t) value.
- void set_day (DateDay day)
    Sets the day of the month for the date.
- void set_month (DateMonth month)
    Sets the month for the date.
- void set_year (DateYear year)
    Sets the year for the date.
- void set_dmy (DateDay day, DateMonth month, DateYear year)
    Sets the value of the date from a day, month, and year.
- void set_julian (unsigned int julian_date)
    Sets the value of the date from a Julian day number.
- void add_days (unsigned int n_days)
    Increments the date some number of days.
- void subtract_days (unsigned int n_days)
    Moves the date some number of days into the past.
- void add_months (unsigned int n_months)
    Increments the date by some number of months.
- void subtract_months (unsigned int n_months)
    Moves the date some number of months into the past.
- void add_years (unsigned int n_years)
    Increments the date by some number of years.
- void subtract_years (unsigned int n_years)
    Moves a date some number of years into the past.
- int days_between (const Date &date1, const Date &date2)
    Computes the number of days between two dates.
- void to_struct_tm (struct tm *tm)
    Fills in the date-related bits of a struct tm using the date value.
- void clamp (const Date *min_date, const Date *max_date)
    If the date is prior to min_date, sets date equal to min_date; If date falls after max_date, sets date equal to max_date.
- void order (Date &other)
    Checks if the date is less than or equal to other, and swap the values if this is not the case.
- String format (const String &date_format)
    Generates a printed representation of the date, in a locale-specific way.

## Static Public Member Functions

- bool valid_day (DateDay day)
    Determines whether the day of the month is valid.
- bool valid_month (DateMonth month)
    Determines whether the month value is valid.
- bool valid_year (DateYear year)
    Determines whether the year is valid.
- bool valid_weekday (DateWeekday weekday)
    Determines whether the weekday is valid.
- bool valid_julian (unsigned int julian_date)
    Determines whether the Julian day is valid.
- bool valid_dmy (DateDay day, DateMonth month, DateYear year)
    Determines whether the day-month-year triplet forms a valid, existing day in the range of days Date understands (Year 1 or later, no more than a few thousand years in the future).
- bool is_leap_year (DateYear year)
    Determines whether the year is a leap year.
- unsigned char get_days_in_month (DateMonth month, DateYear year)
    Gets the number of days in a month, taking leap years into account.
- unsigned char get_monday_weeks_in_year (DateYear year)
    Gets the number of weeks in the year, where weeks are taken to start on Monday.
- unsigned char get_sunday_weeks_in_year (DateYear year)
    Gets the number of weeks in the year, where weeks are taken to start on Sunday.

## Static Public Attributes

- const unsigned int BAD_JULIAN = G_DATE_BAD_JULIAN
    Represents an invalid Julian day number.
- const unsigned char BAD_DAY = G_DATE_BAD_DAY
    Represents an invalid day of the month.
- const unsigned short BAD_YEAR = G_DATE_BAD_YEAR
    Represents an invalid year.

## Detailed Description

A GDate C++ wrapper interface.

The Date object represents a day between January 1, Year 1, and sometime a few thousand years in the future (right now it will go to the year 65535 or so, but set_parse() only parses up to the year 8000 or so - just count on "a few thousand"). Date is meant to represent everyday dates, not astronomical dates or historical dates or ISO timestamps or the like. It extrapolates the current Gregorian calendar forward and backward in time; there is no attempt to change the calendar to match time periods or locations. Date does not store time information; it represents a day.

The Date implementation has several nice features; it is only a 64-bit struct, so storing large numbers of dates is very efficient. It can keep both a Julian and day-month-year representation of the date, since some calculations are much easier with one representation or the other. A Julian representation is simply a count of days since some fixed day in the past; for Date the fixed day is January 1, 1 AD. ("Julian" dates in the Date API aren't really Julian dates in the technical sense; technically, Julian dates count from the start of the Julian period, Jan 1, 4713 BC).

Date is simple to use. First you need a "blank" date; you can dynamically allocated date, or you can declare an automatic variable. Dates are cleared on construction and therefore sane; it's safe to call set_dmy() and the other mutator functions to initialize the value of a cleared date. However, a cleared date is initially invalid, meaning that it doesn't represent a day that exists. It is undefined to call any of the date calculation routines on an invalid date. If you obtain a date from a user or other unpredictable source, you should check its validity with the valid() predicate. valid() is also used to check for errors with set_parse() and other functions that can fail. Dates can be invalidated at any time and reused by calling clear().

It is very important to use the API to access the underlying GDate struct. Often only the day-month-year or only the Julian representation is valid. Sometimes neither is valid. Use the API.

## Constructor & Destructor Documentation

Inti::G::Date::Date | ( |  | ) |

| Constructs a date and initializes it to a sane state. The new date will be cleared (as if you'd called clear()) but invalid (it won't represent an existing day).

Inti::G::Date::Date | ( | DateDay | day,
|  | DateMonth | month,
|  | DateYear | year
| ) |

Construct a date and set the value for the date.

Parameters:

  day | The day of the month.
  month | The month of the year.
  year | The year.

Assuming the day-month-year triplet you pass in represents an existing day, the date will be valid.

Inti::G::Date::Date | ( | unsigned int | julian_day | ) |

Construct a date and set the value for the date.

Parameters:

  julian_day | The days since January 1, Year 1.

Assuming the Julian day number you pass in is valid (greater than 0, less than an unreasonably large number), the date will be valid.

Inti::G::Date::Date | ( | const GDate & | date | ) |

Construct a date from an existing GDate.

Parameters:

  date | A valid GDate.

Inti::G::Date::Date | ( | const Date & | src | ) |

Copy constructor.

Parameters:

  src | Another Date.

## Member Function Documentation

void Inti::G::Date::add_days | ( | unsigned int | n_days | ) |

Increments the date some number of days.

Parameters:

  n_days | The number of days to move the date forward.

To move forward by weeks, add weeks*7 days. The date must be valid.

void Inti::G::Date::add_months | ( | unsigned int | n_months | ) |

Increments the date by some number of months.

Parameters:

  n_months | The number of months to move forward.

If the day of the month is greater than 28, this routine may change the day of the month (because the destination month may not have the current day in it). The date must be valid.

void Inti::G::Date::add_years | ( | unsigned int | n_years | ) |

Increments the date by some number of years.

Parameters:

  n_years | The number of years to move forward.

If the date is February 29, and the destination year is not a leap year, the date will be changed to February 28. The date must be valid.

void Inti::G::Date::clamp | ( | const Date * | min_date,
|  | const Date * | max_date
| ) |

If the date is prior to min_date, sets date equal to min_date; If date falls after max_date, sets date equal to max_date.

Parameters:

  min_date | The minimum accepted value for the date, or null.
  max_date | The maximum accepted value for the date, or null.

Either min_date and max_date may be null. All non-null dates must be valid.

void Inti::G::Date::clear | ( |  | ) |

| Clears a date. Cleared dates will not represent an existing date, but will not contain garbage. Validity can be tested with valid().

int Inti::G::Date::compare | ( | const Date & | other | ) | const

A qsort()-style comparsion method for dates.

Parameters:

  other | Another Date to comare.

Returns:
  0 for equal, less than zero if this is less than other, greater than zero if this date is greater than other.

Both dates must be valid.

int Inti::G::Date::days_between | ( | const Date & | date1,
|  | const Date & | date2
| ) |

Computes the number of days between two dates.

Parameters:

  date1 | The first date.
  date2 | The second date.

Returns:
  The number of days between date1 and date2.

If date2 is prior to date1, the returned value is negative. Both dates must be valid.

String Inti::G::Date::format | ( | const String & | date_format | ) |

Generates a printed representation of the date, in a locale-specific way.

Parameters:

  date_format | The date-related only format string.

Returns:
  The formatted string representation of the date.

Works just like the standard C strftime() function, but only accepts date-related formats; time-related formats give undefined results. The date must be valid.

DateDay Inti::G::Date::get_day | ( |  | ) | const

Gets the day of the month.

Returns:
  The day of the month.

The date must be valid.

unsigned int Inti::G::Date::get_day_of_year | ( |  | ) | const

Gets the day of the year, where January 1 is the first day of the year.

Returns:
  The day of the year.

The date must be valid.

unsigned char Inti::G::Date::get_days_in_month | ( | DateMonth | month,
|  | DateYear | year
| ) | `[static]`

Gets the number of days in a month, taking leap years into account.

Parameters:

  month | The month.
  year | The year.

Returns:
  The number of days in month during the year.

unsigned int Inti::G::Date::get_julian | ( |  | ) | const

Returns the Julian day or "serial number" of the date.

Returns:
  The Julian day.

The Julian day is simply the number of days since January 1, Year 1; that is, January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2, etc. The date must be valid.

unsigned int Inti::G::Date::get_monday_week_of_year | ( |  | ) | const

Gets the week of the year, where weeks are understood to start on Monday.

Returns:
  The week of the year.

If the date is before the first Monday of the year, return 0. The date must be valid.

unsigned char Inti::G::Date::get_monday_weeks_in_year | ( | DateYear | year | ) | `[static]`

Gets the number of weeks in the year, where weeks are taken to start on Monday.

Parameters:

  year | A year to count weeks in.

Returns:
  The number of weeks (or Mondays) in the year.

The return value Will be 52 or 53. The date must be valid. (Years always have 52 7-day periods, plus 1 or 2 extra days depending on whether it's a leap year. This method is basically telling you how many Mondays are in the year, that is, there are 53 Mondays if one of the extra days happens to be a Monday.)

DateMonth Inti::G::Date::get_month | ( |  | ) | const

Gets the month of the year.

Returns:
  The month of the year as a G::DateMonth.

The date must be valid.

unsigned int Inti::G::Date::get_sunday_week_of_year | ( |  | ) | const

Gets the week of the year during which this date falls, if weeks are understood to being on Sunday.

Returns:
  The week number.

The date must be valid. This method can return 0 if the day is before the first Sunday of the year.

unsigned char Inti::G::Date::get_sunday_weeks_in_year | ( | DateYear | year | ) | `[static]`

Gets the number of weeks in the year, where weeks are taken to start on Sunday.

Parameters:

  year | A year to count weeks in.

Returns:
  The number of weeks (or Sundays) in the year.

The return value will be 52 or 53. The date must be valid. (Years always have 52 7-day periods, plus 1 or 2 extra days depending on whether it's a leap year. This method is basically telling you how many Sundays are in the year, that is, there are 53 Sundays if one of the extra days happens to be a Sunday.)

DateWeekday Inti::G::Date::get_weekday | ( |  | ) | const

Gets the day of the week for the date.

Returns:
  The day of the week as a G::DateWeekday.

The date must be valid.

DateYear Inti::G::Date::get_year | ( |  | ) | const

Gets the year of the date.

Returns:
  The year in which the date falls.

The date must be valid.

bool Inti::G::Date::is_first_of_month | ( |  | ) | const

Determines whether the date is on the first of a month.

Returns:
  true if the date is the first of a month.

The date must be valid.

bool Inti::G::Date::is_last_of_month | ( |  | ) | const

Determines whether the date is the last day of the month.

Returns:
  true if the date is the last day of the month.

The date must be valid.

bool Inti::G::Date::is_leap_year | ( | DateYear | year | ) | `[static]`

Determines whether the year is a leap year.

Parameters:

  year | The year to check.

Returns:
  true if the year is a leap year.

bool Inti::G::Date::operator!= | ( | const Date & | other | ) | const

Inequality operator.

Returns:
  true if this date and other are not equal.

bool Inti::G::Date::operator< | ( | const Date & | other | ) | const

Less-than operator.

Returns:
  true if this date is less than other.

bool Inti::G::Date::operator<= | ( | const Date & | other | ) | const

Less-than-or-equal-to operator.

Returns:
  true if this date is less than or equal to other.

Date& Inti::G::Date::operator= | ( | const Date & | src | ) |

Assignment operator.

Parameters:

  src | Another Date.

Returns:
  A reference to the Date.

bool Inti::G::Date::operator== | ( | const Date & | other | ) | const

Equality operator.

Returns:
  true if this date and other are equal.

bool Inti::G::Date::operator> | ( | const Date & | other | ) | const

Greater-than operator.

Returns:
  true if this date is greater than other.

bool Inti::G::Date::operator>= | ( | const Date & | other | ) | const

Greater-than-or-equal-to operator.

Returns:
  true if this date is greater than or equal to other.

void Inti::G::Date::order | ( | Date & | other | ) |

Checks if the date is less than or equal to other, and swap the values if this is not the case.

Parameters:

  other | Another date.

void Inti::G::Date::set_day | ( | DateDay | day | ) |

Sets the day of the month for the date.

Parameters:

  day | The day to set.

If the resulting day-month-year triplet is invalid, the date will be invalid.

void Inti::G::Date::set_dmy | ( | DateDay | day,
|  | DateMonth | month,
|  | DateYear | year
| ) |

Sets the value of the date from a day, month, and year.

Parameters:

  day | The day to set.
  month | The month to set.
  year | The year to set.

The day-month-year triplet must be valid; if you aren't sure it is, call valid_dmy() to check before you set it.

void Inti::G::Date::set_julian | ( | unsigned int | julian_date | ) |

Sets the value of the date from a Julian day number.

Parameters:

  julian_date | The Julian day number (days since January 1, Year 1).

void Inti::G::Date::set_month | ( | DateMonth | month | ) |

Sets the month for the date.

Parameters:

  month | The month to set.

If the resulting day-month-year triplet is invalid, the date will be invalid.

void Inti::G::Date::set_parse | ( | const String & | str | ) |

Parses the user-inputted string str, and try to figure out what date it represents, taking the current locale into account.

Parameters:

  str | The string to parse.

If the string is successfully parsed, the date will be valid after the call. Otherwise, it will be invalid. You should check using valid() to see whether the parsing succeeded. This methods not appropriate for file formats and the like; it isn't very precise, and its exact behavior varies with the locale. It's intended to be a heuristic routine that guesses what the user means by a given string (and it does work pretty well in that capacity).

void Inti::G::Date::set_time | ( | Time | time | ) |

Sets the value of a date from a G::Time (time_t) value.

Parameters:

  time | The Time value to set.

Example:To set the value of a date to the current day, you could write:

                 date->set_time(Time(0));

void Inti::G::Date::set_year | ( | DateYear | year | ) |

Sets the year for the date.

Parameters:

  year | The year to set.

If the resulting day-month-year triplet is invalid, the date will be invalid.

void Inti::G::Date::subtract_days | ( | unsigned int | n_days | ) |

Moves the date some number of days into the past.

Parameters:

  n_days | The number of days to move.

To move by weeks, just move by weeks*7 days. The date must be valid.

void Inti::G::Date::subtract_months | ( | unsigned int | n_months | ) |

Moves the date some number of months into the past.

Parameters:

  n_months | The number of months to move.

If the current day of the month doesn't exist in the destination month, the day of the month may change. The date must be valid.

void Inti::G::Date::subtract_years | ( | unsigned int | n_years | ) |

Moves a date some number of years into the past.

Parameters:

  n_years | The number of years to move.

If the current day doesn't exist in the destination year (that is, it's February 29 and you move to a non-leap-year) then the day is changed to February 28. The date must be valid.

void Inti::G::Date::to_struct_tm | ( | struct tm * | tm | ) |

Fills in the date-related bits of a struct tm using the date value.

Parameters:

  tm | The struct tm to fill.

Initializes the non-date parts with something sane but meaningless.

bool Inti::G::Date::valid | ( |  | ) | const

Determines whether the date represents an existing day.

Returns:
  true if the date is valid.

The date must not contain garbage; it should have been initialized with clear() if it wasn't allocated by one of the constructor variants.

bool Inti::G::Date::valid_day | ( | DateDay | day | ) | `[static]`

Determines whether the day of the month is valid.

Parameters:

  day | The day.

Returns:
  true if the day is valid.

A day is valid if it's between 1 and 31 inclusive.

bool Inti::G::Date::valid_dmy | ( | DateDay | day,
|  | DateMonth | month,
|  | DateYear | year
| ) | `[static]`

Determines whether the day-month-year triplet forms a valid, existing day in the range of days Date understands (Year 1 or later, no more than a few thousand years in the future).

Parameters:

  day | The day.
  month | The month.
  year | The year.

Returns:
  true if the date is a valid one.

bool Inti::G::Date::valid_julian | ( | unsigned int | julian_date | ) | `[static]`

Determines whether the Julian day is valid.

Parameters:

  julian_date | The Julian day to check.

Returns:
  true if the Julian day is valid.

Anything greater than zero is basically a valid Julian, though there is a 32-bit limit.

bool Inti::G::Date::valid_month | ( | DateMonth | month | ) | `[static]`

Determines whether the month value is valid.

Parameters:

  month | The month.

Returns:
  true if the month is valid.

The 12 G::DateMonth enumeration values are the only valid months.

bool Inti::G::Date::valid_weekday | ( | DateWeekday | weekday | ) | `[static]`

Determines whether the weekday is valid.

Parameters:

  weekday | The weekday.

Returns:
  true if the weekday is valid.

The 7 G::DateWeekday enumeration values are the only valid weekdays.

bool Inti::G::Date::valid_year | ( | DateYear | year | ) | `[static]`

Determines whether the year is valid.

Parameters:

  year | The year.

Returns:
  true if the year is valid.

Any year greater than 0 is valid, though there is a 16-bit limit to what Date will understand.

The documentation for this class was generated from the following file:

- date.h

Main Page - Footer