Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Related Functions

# Glib::Date Class Reference

Julian calendar date. More...

`#include <glibmm/date.h>`

List of all members.

## Public Types
enum | Month { BAD_MONTH, JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER }
enum | Weekday { BAD_WEEKDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }
enum | DMY { DAY, MONTH, YEAR }
typedef guint8 | Day
typedef guint16 | Year
## Public Member Functions
| Date ()
| Construct an undefined date.
| Date (Day day, Month month, Year year)
| Construct a date with the given day, month and year.
| Date (guint32 julian_day)
| Construct a date from a julian day.
| Date (const GDate& castitem)
| Construct a Glib::Date by copying the contents of a GDate.
| Date (const Date& other)
| Construct a Glib::Date from another.
Date& | operator= (const Date& other)
| Assign another date to this one.
GDate* | gobj ()
| Provides access to the underlying C instance.
const GDate* | gobj () const
| Provides access to the underlying C instance.
void | clear ()
| Clear the date.
void | set_parse (const Glib::ustring& str)
| Parses a user-inputted string str, and try to figure out what date it represents, taking the current locale into account.
void | set_time (time_t timet)
| Sets the value of a date from a <type>time_t</type> value.
void | set_time (const GTimeVal& timeval)
| Sets the value of a date from a GTimeVal value.
void | set_time_current ()
| Set this Glib::Date to the current time.
void | set_month (Month month)
| Sets the month of the year.
void | set_day (Day day)
| Sets the day of the month.
void | set_year (Year year)
| Sets the year.
void | set_dmy (Day day, Month month, Year year)
| Sets the value of a day, month, and year.
void | set_julian (guint32 julian_day)
| Sets the value of a GDate from a Julian day number.
Date& | add_days (int n_days)
| Add a number of days to a Date.
Date& | subtract_days (int n_days)
| Subtract n_days to a Date.
Date& | add_months (int n_months)
| Add n_months to a Date.
Date& | subtract_months (int n_months)
| Subtract n_months to a Date.
Date& | add_years (int n_years)
| Add n_days to a Date.
Date& | subtract_years (int n_years)
| Subtract n_years to a Date.
int | days_between (const Date& rhs) const
| Calculate days between two dates.
int | compare (const Date& rhs) const
| Compare two dates.
Date& | clamp (const Date& min_date, const Date& max_date)
| If date is prior to min_date, sets date equal to min_date.
Date& | clamp_min (const Date& min_date)
| If date is prior to min_date, sets date equal to min_date.
Date& | clamp_max (const Date& max_date)
| If date falls after max_date, sets date equal to max_date.
void | order (Date& other)
| Checks if date is less than or equal to other date, and swap the values if this is not the case.
Weekday | get_weekday () const
| Returns the day of the week for a Date.
Month | get_month () const
| Returns the month of the year.
Year | get_year () const
| Returns the year of a Date.
Day | get_day () const
| Returns the day of the month.
guint32 | get_julian () const
| Returns the Julian day or "serial number" of the Date.
unsigned int | get_day_of_year () const
| Returns the day of the year, where Jan 1 is the first day of the year.
unsigned int | get_monday_week_of_year () const
| Returns the week of the year, where weeks are understood to start on Monday.
unsigned int | get_sunday_week_of_year () const
| Returns the week of the year during which this date falls, if weeks are understood to being on Sunday.
unsigned int | get_iso8601_week_of_year () const
| Returns the week of the year, where weeks are interpreted according to ISO 8601.
bool | is_first_of_month () const
| Returns true if the date is on the first of a month.
bool | is_last_of_month () const
| Returns true if the date is the last day of the month.
Glib::ustring | format_string (const Glib::ustring& format) const
| Convert date to string.
void | to_struct_tm (struct tm& dest) const
| Fills in the date-related bits of a struct tm using the date value.
bool | valid () const
| Returns true if the Date represents an existing day.
## Static Public Member Functions
static guint8 | get_days_in_month (Month month, Year year)
| Returns the number of days in a month, taking leap years into account.
static guint8 | get_monday_weeks_in_year (Year year)
| Returns the number of weeks in the year, where weeks are taken to start on Monday.
static guint8 | get_sunday_weeks_in_year (Year year)
| Returns the number of weeks in the year, where weeks are taken to start on Sunday.
static bool | is_leap_year (Year year)
| Returns true if the year is a leap year.
static bool | valid_day (Day day)
| Returns true if the day of the month is valid (a day is valid if it's between 1 and 31 inclusive).
static bool | valid_month (Month month)
| Returns true if the month value is valid.
static bool | valid_year (Year year)
| Returns true if the year is valid.
static bool | valid_weekday (Weekday weekday)
| Returns true if the weekday is valid.
static bool | valid_julian (guint32 julian_day)
| Returns true if the Julian day is valid.
static bool | valid_dmy (Day day, Month month, Year year)
| Returns true if 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).
## Static Public Attributes
static const Day | BAD_DAY = 0
static const Year | BAD_YEAR = 0
static const guint32 | BAD_JULIAN = 0
## Related Functions
(Note that these are not member functions.)
bool | operator== (const Date& lhs, const Date& rhs)
bool | operator!= (const Date& lhs, const Date& rhs)
bool | operator< (const Date& lhs, const Date& rhs)
bool | operator> (const Date& lhs, const Date& rhs)
bool | operator<= (const Date& lhs, const Date& rhs)
bool | operator>= (const Date& lhs, const Date& rhs)

## Detailed Description

Julian calendar date.

## Member Typedef Documentation

typedef guint8 Glib::Date::Day

typedef guint16 Glib::Date::Year

## Constructor & Destructor Documentation

Glib::Date::Date | ( | ) |

Construct an undefined date.

Glib::Date::Date | ( | Day | day,
|  | Month | month,
|  | Year | year
| ) |  |

Construct a date with the given day, month and year.

Parameters:

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

Glib::Date::Date | ( | guint32 | julian_day ) | `[explicit]`

Construct a date from a julian day.

Parameters:

  julian_day | The julian day (guint32).

Glib::Date::Date | ( | const GDate & | castitem ) | `[explicit]`

Construct a Glib::Date by copying the contents of a GDate.

Parameters:

  castitem | The GDate.

Since glibmm 2.18:

Glib::Date::Date | ( | const Date& | other ) |

Construct a Glib::Date from another.

Parameters:

  other | the other Glib::Date.

Since glibmm 2.18:

## Member Function Documentation

Date& Glib::Date::add_days | ( | int | n_days ) |

Add a number of days to a Date.

Parameters:

  n_days | Days to add.

Returns:
  Resulting Date

Date& Glib::Date::add_months | ( | int | n_months ) |

Add n_months to a Date.

Parameters:

  n_months | Months to add.

Returns:
  Resulting Date

Date& Glib::Date::add_years | ( | int | n_years ) |

Add n_days to a Date.

Parameters:

  n_years | Years to add.

Returns:
  Resulting Date

Date& Glib::Date::clamp | ( | const Date& | min_date,
|  | const Date& | max_date
| ) |  |

If date is prior to min_date, sets date equal to min_date.

If date falls after max_date, sets date equal to max_date. All dates must be valid. See also clamp_min() and clamp_max().

Parameters:

  min_date | Date minimum value.
  max_date | Date maximum value.

Returns:
  Date in interval.

Date& Glib::Date::clamp_max | ( | const Date& | max_date ) |

If date falls after max_date, sets date equal to max_date.

See also clamp(), and clamp_min().

Parameters:

  max_date | Date maximum value.

Returns:
  Date in interval.

Date& Glib::Date::clamp_min | ( | const Date& | min_date ) |

If date is prior to min_date, sets date equal to min_date.

See also clamp(), and clamp_max().

Parameters:

  min_date | Date minimum value.

Returns:
  Date in interval.

void Glib::Date::clear | ( | ) |

Clear the date.

The cleared dates will not represent an existing date, but will not contain garbage.

int Glib::Date::compare | ( | const Date& | rhs ) | const

Compare two dates.

Parameters:

  rhs | Date to compare.

Returns:
  Result of comparition.

int Glib::Date::days_between | ( | const Date& | rhs ) | const

Calculate days between two dates.

Parameters:

  rhs | Date.

Returns:
  Numbers of days.

Glib::ustring Glib::Date::format_string | ( | const Glib::ustring& | format ) | const

Convert date to string.

Parameters:

  format | A format string as used by `strftime()`, in UTF-8 encoding. Only date formats are allowed, the result of time formats is undefined.

Returns:
  The formatted date string.

Exceptions:

  Glib::ConvertError |

Day Glib::Date::get_day | ( | ) | const

Returns the day of the month.

The date must be valid.

Returns:
  Day of the month..

unsigned int Glib::Date::get_day_of_year | ( | ) | const

Returns the day of the year, where Jan 1 is the first day of the year.

The date must be valid.

Returns:
  Julian day.

static guint8 Glib::Date::get_days_in_month | ( | Month | month,
|  | Year | year
| ) |  | `[static]`

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

Parameters:

  month | Month.
  year | Year.

Returns:
  Number of days in month during the year.

unsigned int Glib::Date::get_iso8601_week_of_year | ( | ) | const

Returns the week of the year, where weeks are interpreted according to ISO 8601.

The date must be valid.

Returns:
  ISO 8601 week number of the year.

Since glibmm 2.22:

guint32 Glib::Date::get_julian | ( | ) | const

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

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

Returns:
  Julian day.

unsigned int Glib::Date::get_monday_week_of_year | ( | ) | const

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

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

Returns:
  Week of the year.

static guint8 Glib::Date::get_monday_weeks_in_year | ( | Year | year ) | `[static]`

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

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

Parameters:

  year | Year to count weeks in.

Returns:
  Number of weeks.

Month Glib::Date::get_month | ( | ) | const

Returns the month of the year.

The date must be valid.

Returns:
  Month of the year as a Date::Month.

unsigned int Glib::Date::get_sunday_week_of_year | ( | ) | const

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

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

Returns:
  Week of the year.

static guint8 Glib::Date::get_sunday_weeks_in_year | ( | Year | year ) | `[static]`

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

Will be 52 or 53. (Years always have 52 7-day periods, plus 1 or 2 extra days depending on whether it's a leap year. This function is basically telling you how many Sundays are in the year, i.e. there are 53 Sundays if one of the extra days happens to be a Sunday.)

Parameters:

  year | Year to count weeks in.

Returns:
  Number of weeks.

Weekday Glib::Date::get_weekday | ( | ) | const

Returns the day of the week for a Date.

The date must be valid.

Returns:
  Day of the week as a Date::Weekday.

Year Glib::Date::get_year | ( | ) | const

Returns the year of a Date.

The date must be valid.

Returns:
  Year in which the date falls.

GDate* Glib::Date::gobj | ( | ) | `[inline]`

Provides access to the underlying C instance.

const GDate* Glib::Date::gobj | ( | ) | const`[inline]`

Provides access to the underlying C instance.

bool Glib::Date::is_first_of_month | ( | ) | const

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

The date must be valid.

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

bool Glib::Date::is_last_of_month | ( | ) | const

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

The date must be valid.

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

static bool Glib::Date::is_leap_year | ( | Year | year ) | `[static]`

Returns true if the year is a leap year.

Parameters:

  year | Year to check.

Returns:
  true if the year is a leap year.

Date& Glib::Date::operator= | ( | const Date& | other ) |

Assign another date to this one.

For example:

     ...
     Glib::Date my_date;
     my_date = other_date;

Parameters:

  other | The other Glib::Date.

Since glibmm 2.18:

void Glib::Date::order | ( | Date& | other ) |

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

Parameters:

  other | Date ro compare.

Returns:
  Date.

void Glib::Date::set_day | ( | Day | day ) |

Sets the day of the month.

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

Parameters:

  day | Day to set.

void Glib::Date::set_dmy | ( | Day | day,
|  | Month | month,
|  | Year | year
| ) |  |

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

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

Parameters:

  day | Day to set.
  month | Month to set.
  year | Year to set.

void Glib::Date::set_julian | ( | guint32 | julian_day ) |

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

Parameters:

  julian_day | Julian day to set.

void Glib::Date::set_month | ( | Month | month ) |

Sets the month of the year.

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

Parameters:

  month | Month to set.

void Glib::Date::set_parse | ( | const Glib::ustring& | str ) |

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

If the string is successfully parsed, the date will be valid after the call. Otherwise, it will be invalid. This function is 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).

Parameters:

  str | String to parse.

void Glib::Date::set_time | ( | time_t | timet ) |

Sets the value of a date from a <type>time_t</type> value.

Parameters:

  timet | time_t value to set

See also:
  set_time_current()

Since: 2.10

void Glib::Date::set_time | ( | const GTimeVal & | timeval ) |

Sets the value of a date from a GTimeVal value.

Note that the tv_usec member is ignored, because Glib::Date can't make use of the additional precision.

See also:
  set_time_current()

Parameters:

  timeval | GTimeVal value to set

Since: 2.10

void Glib::Date::set_time_current | ( | ) |

Set this Glib::Date to the current time.

void Glib::Date::set_year | ( | Year | year ) |

Sets the year.

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

Parameters:

  year | Year to set.

Date& Glib::Date::subtract_days | ( | int | n_days ) |

Subtract n_days to a Date.

Parameters:

  n_days | Days to subtract.

Returns:
  Resulting Date

Date& Glib::Date::subtract_months | ( | int | n_months ) |

Subtract n_months to a Date.

Parameters:

  n_months | Months to subtract.

Returns:
  Resulting Date

Date& Glib::Date::subtract_years | ( | int | n_years ) |

Subtract n_years to a Date.

Parameters:

  n_years | Years to subtract.

Returns:
  Resulting Date

void Glib::Date::to_struct_tm | ( | struct tm & | dest ) | const

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

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

Parameters:

  dest | Struct tm to fill.

bool Glib::Date::valid | ( | ) | const

Returns true if the Date represents an existing day.

Returns:
  true if the date is valid.

static bool Glib::Date::valid_day | ( | Day | day ) | `[static]`

Returns true if the day of the month is valid (a day is valid if it's between 1 and 31 inclusive).

Parameters:

  day | Day to check.

Returns:
  true if the day is valid.

static bool Glib::Date::valid_dmy | ( | Day | day,
|  | Month | month,
|  | Year | year
| ) |  | `[static]`

Returns true if 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 | Day to check.
  month | Month to check.
  year | Year to check.

Returns:
  true if the date is a valid one.

static bool Glib::Date::valid_julian | ( | guint32 | julian_day ) | `[static]`

Returns true if the Julian day is valid.

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

Parameters:

  julian_day | Julian day to check.

Returns:
  true if the Julian day is valid.

static bool Glib::Date::valid_month | ( | Month | month ) | `[static]`

Returns true if the month value is valid.

The 12 Date::Month enumeration values are the only valid months.

Parameters:

  month | Month to check.

Returns:
  true if the month is valid.

static bool Glib::Date::valid_weekday | ( | Weekday | weekday ) | `[static]`

Returns true if the weekday is valid.

The 7 Date::Weekday enumeration values are the only valid.

Parameters:

  weekday | Weekday to check.

Returns:
  true if the weekday is valid.

static bool Glib::Date::valid_year | ( | Year | year ) | `[static]`

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.

Parameters:

  year | Year to check.

Returns:
  true if the year is valid.

## Friends And Related Function Documentation

bool operator!= | ( | const Date& | lhs,
|  | const Date& | rhs
| ) |  | `[related]`

bool operator< | ( | const Date& | lhs,
|  | const Date& | rhs
| ) |  | `[related]`

bool operator<= | ( | const Date& | lhs,
|  | const Date& | rhs
| ) |  | `[related]`

bool operator== | ( | const Date& | lhs,
|  | const Date& | rhs
| ) |  | `[related]`

bool operator> | ( | const Date& | lhs,
|  | const Date& | rhs
| ) |  | `[related]`

bool operator>= | ( | const Date& | lhs,
|  | const Date& | rhs
| ) |  | `[related]`

## Member Data Documentation

const Day Glib::Date::BAD_DAY = 0`[static]`

const guint32 Glib::Date::BAD_JULIAN = 0`[static]`

const Year Glib::Date::BAD_YEAR = 0`[static]`