Public Member Functions | Static Public Member Functions

base::DateTime Class Reference

A class that holds a date and time. More...

#include <libppbase/base/DateTime.h>

List of all members.

Public Member Functions

 DateTime ()
 Constructor for an empty object (null date).
 DateTime (const String &s, bool utc=false)
 Constructor that constructs a new date from a string.
void addDays (int n)
 Increases this date by days.
void addHours (int n)
 Increases this date by hours.
void addMinutes (int n)
 Increases this date by minutes.
void addMonths (int n)
 Increases this date by months.
void addSeconds (int n)
 Increases this date by seconds.
void addYears (int n)
 Increases this date by years.
unsigned char getDay () const
 Gets the day.
unsigned char getDayOfWeek () const
 Gets the number of the day in the week.
unsigned getDayOfYear () const
 Gets the number of the day in the year.
unsigned char getHour () const
 Gets the hour.
unsigned getMilliSecondOfDay () const
 Gets the current millisecond of the day.
unsigned short getMilliSeconds () const
 Gets the milliseconds.
unsigned char getMinute () const
 Gets the minute.
unsigned char getMonth () const
 Gets the month.
unsigned char getMonthDays () const
 Returns the number of days for the current month in year.
unsigned char getSecond () const
 Gets the second.
unsigned getSecondOfDay () const
 Gets the second of the day.
short getYear () const
 Gets the year.
unsigned short getYearDays () const
 Returns the number of days for the current year.
bool isLeapYear () const
 Checks if the year of this date is a leap year. Leap years have 366 instead of 365 days.
bool isNull () const
 Checks if this date is a null date (00.00.0000).
bool isUTC () const
 Checks if the stored date and time is UTC time.
void nextDay ()
 Increases the date by one day.
void nextHour ()
 Increases the date by one hour.
void nextMinute ()
 Increases the date by one minute.
void nextMonth ()
 Increases the date by one month.
void nextYear ()
 Increases the date by one year.
void prevDay ()
 Decreases the date by one day.
void prevHour ()
 Decreases the date by one hour.
void prevMinute ()
 Decreases the date by one minute.
void prevMonth ()
 Decreases the date by one month.
void prevYear ()
 Decreases the date by one year.
bool set (const String &str, bool utc=false)
 Sets the date.
void setCurrent (bool utc=false, bool millis=false)
 Sets this object to the system's current date and time.
void setDate (unsigned char day, unsigned char month, short year)
 Sets the date part.
void setUTC (bool utc)
 Sets the utc information.
void setTime (unsigned char hour, unsigned char minute, unsigned char second, unsigned short millis=0)
 Sets the time part.
String toString (const char *format=NULL) const
 Returns the date/time in string representation.
bool valid () const
 Checks if the object contains a valid date and time.
void operator= (const String &s)
 Assignment operator.
bool operator! () const
 Null operator.
bool operator== (const DateTime &d) const
 Equal operator.
bool operator!= (const DateTime &d) const
 Not equal operator.
bool operator< (const DateTime &d) const
 Less operator.
bool operator<= (const DateTime &d) const
 Less or equal operator.
bool operator> (const DateTime &d) const
 Greater operator.
bool operator>= (const DateTime &d) const
 Greater or equal operator.

Static Public Member Functions

static int compare (const DateTime &lhs, const DateTime &rhs, bool time=true, bool millis=true)
 Compares two dates.
static int compareDate (const DateTime &lhs, const DateTime &rhs)
 Compares the date part of two objects.
static int compareTime (const DateTime &lhs, const DateTime &rhs, bool millis=true)
 Compares the time part of two objects.
static bool diff (const DateTime &lhs, const DateTime &rhs, unsigned *rdays, unsigned char *rhours=NULL, unsigned char *rminutes=NULL, unsigned char *rseconds=NULL, unsigned short *rmillis=NULL)
 Calculates the difference between two dates.
static int diffSeconds (const DateTime &lhs, const DateTime &rhs)
 Calculates the difference between two dates in seconds.
static unsigned getDayOfYear (unsigned char day, unsigned char month, short year)
 Gets the number of the day in the year.
static unsigned char getMonthDays (unsigned char month, short year)
 Returns the number of days for the given month and year.
static unsigned short getYearDays (short year)
 Returns the number of days for the given year.
static bool isLeapYear (short year)
 Checks if a year is a leap year. Leap years have 366 instead of 365 days.

Detailed Description

A class that holds a date and time.

The rules for leap years are related to protestant Britian. They adopted the gregorian calendar in 1752. This means that 1752 was the first year to adopt the new leap year rules, while all years before 1752 have the old leap year rules. This also means that September 1752 has only 19 days, with September 3rd to September 13th missing.

Author:
Christoph Schwarz
Version:
$Id: DateTime.h 21 2010-09-05 04:18:17Z cschwarz1 $

Constructor & Destructor Documentation

base::DateTime::DateTime ( const String s,
bool  utc = false 
)

Constructor that constructs a new date from a string.

Parameters:
s string containing the date
utc true for UTC time, false for local time

Member Function Documentation

void base::DateTime::addDays ( int  n  ) 

Increases this date by days.

Parameters:
n number of days, a negative value decreases the date
void base::DateTime::addHours ( int  n  ) 

Increases this date by hours.

Parameters:
n number of hours, a negative value decreases the date
void base::DateTime::addMinutes ( int  n  ) 

Increases this date by minutes.

Parameters:
n number of minutes, a negative value decreases the date
void base::DateTime::addMonths ( int  n  ) 

Increases this date by months.

Parameters:
n number of months, a negative value decreases the date
void base::DateTime::addSeconds ( int  n  ) 

Increases this date by seconds.

Parameters:
n number of seconds, a negative value decreases the date
void base::DateTime::addYears ( int  n  ) 

Increases this date by years.

Parameters:
n number of years, a negative value decreases the date
static int base::DateTime::compare ( const DateTime lhs,
const DateTime rhs,
bool  time = true,
bool  millis = true 
) [static]

Compares two dates.

Parameters:
lhs the first date
rhs the second date
time flag: compare time too
millis flag: compare milliseconds too
Returns:
-1 if lhs is older than rhs, 1 if lhs is newer than rhs, 0 if the dates are equal
static int base::DateTime::compareDate ( const DateTime lhs,
const DateTime rhs 
) [static]

Compares the date part of two objects.

Parameters:
lhs the first object
rhs the second object
Returns:
-1 if lhs is older than rhs, 1 if lhs is newer than rhs, 0 if the dates are equal
static int base::DateTime::compareTime ( const DateTime lhs,
const DateTime rhs,
bool  millis = true 
) [static]

Compares the time part of two objects.

Parameters:
lhs the first object
rhs the second object
millis,: flag: compare milliseconds too
Returns:
-1 if lhs is older than rhs, 1 if lhs is newer than rhs, 0 if the times are equal
static bool base::DateTime::diff ( const DateTime lhs,
const DateTime rhs,
unsigned *  rdays,
unsigned char *  rhours = NULL,
unsigned char *  rminutes = NULL,
unsigned char *  rseconds = NULL,
unsigned short *  rmillis = NULL 
) [static]

Calculates the difference between two dates.

Calculates abs(lhs - rhs)

Parameters:
lhs the first date
rhs the second date
rdays returns the day difference
rhours returns the hours difference
rminutes returns the minutes difference
rseconds returns the seconds difference
rmillis returns the milliseconds difference
Returns:
true if lhs is greater than or equal to lhs, false if not
static int base::DateTime::diffSeconds ( const DateTime lhs,
const DateTime rhs 
) [static]

Calculates the difference between two dates in seconds.

Calculates lhs - rhs

Parameters:
lhs the first date
rhs the second date
Returns:
the difference in seconds
unsigned char base::DateTime::getDay (  )  const

Gets the day.

Returns:
the day
unsigned char base::DateTime::getDayOfWeek (  )  const

Gets the number of the day in the week.

Returns:
number of the day in the week (0 - 6, 0 = sunday)
static unsigned base::DateTime::getDayOfYear ( unsigned char  day,
unsigned char  month,
short  year 
) [static]

Gets the number of the day in the year.

Parameters:
day the day of the month
month the month
year the year
Returns:
number of the day in the year, from 0 (january 1st) to 364 (december 31st regular year) or 365 (december 31st leap year)
unsigned base::DateTime::getDayOfYear (  )  const

Gets the number of the day in the year.

Returns:
number of the day in the year, from 0 (january 1st) to 364 (december 31st regular year) or 365 (december 31st leap year)
unsigned char base::DateTime::getHour (  )  const

Gets the hour.

Returns:
the hour
unsigned base::DateTime::getMilliSecondOfDay (  )  const

Gets the current millisecond of the day.

Returns:
the millisecond of the day, 0 to 86399999
unsigned short base::DateTime::getMilliSeconds (  )  const

Gets the milliseconds.

Returns:
the milliseconds
unsigned char base::DateTime::getMinute (  )  const

Gets the minute.

Returns:
the minute
unsigned char base::DateTime::getMonth (  )  const

Gets the month.

Returns:
the month
unsigned char base::DateTime::getMonthDays (  )  const

Returns the number of days for the current month in year.

Returns:
the number of days (28 to 31), 0 for invalid month
static unsigned char base::DateTime::getMonthDays ( unsigned char  month,
short  year 
) [static]

Returns the number of days for the given month and year.

Parameters:
month the given month
year the given year
Returns:
the number of days (28 to 31), 0 for invalid month
unsigned char base::DateTime::getSecond (  )  const

Gets the second.

Returns:
the second
unsigned base::DateTime::getSecondOfDay (  )  const

Gets the second of the day.

Returns:
the second of the day, 0 to 86399
short base::DateTime::getYear (  )  const

Gets the year.

Returns:
the year
unsigned short base::DateTime::getYearDays (  )  const

Returns the number of days for the current year.

Returns:
the number of days (mostly 365 or 366), 355 for year 1752
static unsigned short base::DateTime::getYearDays ( short  year  )  [static]

Returns the number of days for the given year.

Parameters:
year the given year
Returns:
the number of days (mostly 365 or 366), 355 for year 1752
bool base::DateTime::isLeapYear (  )  const

Checks if the year of this date is a leap year. Leap years have 366 instead of 365 days.

Returns:
true if the year is a leap year, false if not
static bool base::DateTime::isLeapYear ( short  year  )  [static]

Checks if a year is a leap year. Leap years have 366 instead of 365 days.

Parameters:
year the year to check
Returns:
true if the year is a leap year, false if not
bool base::DateTime::isNull (  )  const

Checks if this date is a null date (00.00.0000).

Returns:
true if it is a null date
bool base::DateTime::isUTC (  )  const

Checks if the stored date and time is UTC time.

Returns:
true if the stored time is UTC time, false if it is local time
bool base::DateTime::operator! (  )  const

Null operator.

Returns:
true if this date is null, false if it is not null
bool base::DateTime::operator!= ( const DateTime d  )  const

Not equal operator.

Parameters:
d date to compare this date to
Returns:
true if the dates are not equal, false if they are equal
bool base::DateTime::operator< ( const DateTime d  )  const

Less operator.

Parameters:
d date to compare this date to
Returns:
true if this date is older than d
bool base::DateTime::operator<= ( const DateTime d  )  const

Less or equal operator.

Parameters:
d date to compare this date to
Returns:
true if this date is older than or the same age as d
void base::DateTime::operator= ( const String s  ) 

Assignment operator.

Parameters:
s new date and time, recognized formats are YYYY-MM-DD [HH:MM:SS[.MS]] and DD.MM.YYYY [HH:MM:SS[.MS]]
bool base::DateTime::operator== ( const DateTime d  )  const

Equal operator.

Parameters:
d date to compare this date to
Returns:
true if the dates are equal
bool base::DateTime::operator> ( const DateTime d  )  const

Greater operator.

Parameters:
d date to compare this date to
Returns:
true if this date is newer than d
bool base::DateTime::operator>= ( const DateTime d  )  const

Greater or equal operator.

Parameters:
d date to compare this date to
Returns:
true if this date is newer than or the same age as d
bool base::DateTime::set ( const String str,
bool  utc = false 
)

Sets the date.

Parameters:
str new date and time, recognized formats are YYYY-MM-DD [HH:MM:SS] and DD.MM.YYYY [HH:MM:SS]
utc true for UTC time, false for local time
Returns:
true if this date could be parsed and is valid, false if not
void base::DateTime::setCurrent ( bool  utc = false,
bool  millis = false 
)

Sets this object to the system's current date and time.

Parameters:
utc true to store the UTC time, false to store the local time
millis true to store milliseconds, false to set the to 0
void base::DateTime::setDate ( unsigned char  day,
unsigned char  month,
short  year 
)

Sets the date part.

Parameters:
day the day
month the month
year the year
void base::DateTime::setTime ( unsigned char  hour,
unsigned char  minute,
unsigned char  second,
unsigned short  millis = 0 
)

Sets the time part.

Parameters:
hour the day
minute the month
second the year
millis the milliseconds
void base::DateTime::setUTC ( bool  utc  ) 

Sets the utc information.

Parameters:
utc true if the stored time is UTC time, false if it is local time zone
String base::DateTime::toString ( const char *  format = NULL  )  const

Returns the date/time in string representation.

Parameters:
format the format to use, a subset of the strftime format (NULL to use a default format)
Returns:
the date/time in string representation
bool base::DateTime::valid (  )  const

Checks if the object contains a valid date and time.

Returns:
true if the date and time are valid

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