Xfce Foundation Classes
Main Page  | IndexNamespace List  |  Alphabetical List  |  Class List  |  File List


Public Member Functions | Static Public Member Functions | Static Public Attributes
Xfc::G::Date Class Reference

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

#include <xfc/glib/date.hh>

Inheritance diagram for Xfc::G::Date:
Xfc::Object Xfc::Trackable

List of all members.

Public Member Functions

Constructors

Static Public Member Functions

Static Public Attributes


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

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

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

Construct a date and set the value for the date.

Parameters:
dayThe day of the month.
monthThe month of the year.
yearThe year.

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

Xfc::G::Date::Date ( unsigned int  julian_day)

Construct a date and set the value for the date.

Parameters:
julian_dayThe 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.

Xfc::G::Date::Date ( const GDate &  date)

Construct a date from an existing GDate.

Parameters:
dateA valid GDate.
Xfc::G::Date::Date ( const Date src)

Copy constructor.

Parameters:
srcAnother Date.

Member Function Documentation

void Xfc::G::Date::add_days ( unsigned int  n_days)

Increments the date some number of days.

Parameters:
n_daysThe number of days to move the date forward.

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

void Xfc::G::Date::add_months ( unsigned int  n_months)

Increments the date by some number of months.

Parameters:
n_monthsThe 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 Xfc::G::Date::add_years ( unsigned int  n_years)

Increments the date by some number of years.

Parameters:
n_yearsThe 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 Xfc::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_dateThe minimum accepted value for the date, or null.
max_dateThe maximum accepted value for the date, or null.

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

Clears a date.

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

int Xfc::G::Date::compare ( const Date other) const

A qsort()-style comparsion method for dates.

Parameters:
otherAnother 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 Xfc::G::Date::days_between ( const Date date1,
const Date date2 
)

Computes the number of days between two dates.

Parameters:
date1The first date.
date2The 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 Xfc::G::Date::format ( const String date_format)

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

Parameters:
date_formatThe 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.

Gets the day of the month.

Returns:
The day of the month.

The date must be valid.

unsigned int Xfc::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.

static unsigned char Xfc::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:
monthThe month.
yearThe year.
Returns:
The number of days in month during the year.
unsigned int Xfc::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 Xfc::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.

static unsigned char Xfc::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:
yearA 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.)

Gets the month of the year.

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

The date must be valid.

unsigned int Xfc::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.

static unsigned char Xfc::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:
yearA 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.)

Gets the day of the week for the date.

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

The date must be valid.

Gets the year of the date.

Returns:
The year in which the date falls.

The date must be valid.

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.

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.

static bool Xfc::G::Date::is_leap_year ( DateYear  year) [static]

Determines whether the year is a leap year.

Parameters:
yearThe year to check.
Returns:
true if the year is a leap year.
bool Xfc::G::Date::operator!= ( const Date other) const

Inequality operator.

Returns:
true if this date and other are not equal.
bool Xfc::G::Date::operator< ( const Date other) const

Less-than operator.

Returns:
true if this date is less than other.
bool Xfc::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& Xfc::G::Date::operator= ( const Date src)

Assignment operator.

Parameters:
srcAnother Date.
Returns:
A reference to the Date.
bool Xfc::G::Date::operator== ( const Date other) const

Equality operator.

Returns:
true if this date and other are equal.
bool Xfc::G::Date::operator> ( const Date other) const

Greater-than operator.

Returns:
true if this date is greater than other.
bool Xfc::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 Xfc::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:
otherAnother date.

Sets the day of the month for the date.

Parameters:
dayThe day to set.

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

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

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

Parameters:
dayThe day to set.
monthThe month to set.
yearThe 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 Xfc::G::Date::set_julian ( unsigned int  julian_date)

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

Parameters:
julian_dateThe Julian day number (days since January 1, Year 1).

Sets the month for the date.

Parameters:
monthThe month to set.

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

void Xfc::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:
strThe 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 Xfc::G::Date::set_time ( Time  time)

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

Parameters:
timeThe Time value to set.

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

< date->set_time(Time(0));
<

Sets the year for the date.

Parameters:
yearThe year to set.

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

void Xfc::G::Date::subtract_days ( unsigned int  n_days)

Moves the date some number of days into the past.

Parameters:
n_daysThe number of days to move.

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

void Xfc::G::Date::subtract_months ( unsigned int  n_months)

Moves the date some number of months into the past.

Parameters:
n_monthsThe 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 Xfc::G::Date::subtract_years ( unsigned int  n_years)

Moves a date some number of years into the past.

Parameters:
n_yearsThe 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 Xfc::G::Date::to_struct_tm ( struct tm *  tm)

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

Parameters:
tmThe struct tm to fill.

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

bool Xfc::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.

static bool Xfc::G::Date::valid_day ( DateDay  day) [static]

Determines whether the day of the month is valid.

Parameters:
dayThe day.
Returns:
true if the day is valid.

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

static bool Xfc::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:
dayThe day.
monthThe month.
yearThe year.
Returns:
true if the date is a valid one.
static bool Xfc::G::Date::valid_julian ( unsigned int  julian_date) [static]

Determines whether the Julian day is valid.

Parameters:
julian_dateThe 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.

static bool Xfc::G::Date::valid_month ( DateMonth  month) [static]

Determines whether the month value is valid.

Parameters:
monthThe month.
Returns:
true if the month is valid.

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

static bool Xfc::G::Date::valid_weekday ( DateWeekday  weekday) [static]

Determines whether the weekday is valid.

Parameters:
weekdayThe weekday.
Returns:
true if the weekday is valid.

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

static bool Xfc::G::Date::valid_year ( DateYear  year) [static]

Determines whether the year is valid.

Parameters:
yearThe 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:
Xfce Foundation Classes
Copyright © 2004-2005 The XFC Development Team XFC