edelib  2.1.0
Public Types | Public Member Functions | Static Public Member Functions | List of all members
Date Class Reference

A class for date manipulation. More...

#include <edelib/DateTime.h>

Public Types

enum  Month {
  Jan = 1, Feb, Mar, Apr,
  May, Jun, Jul, Aug,
  Sep, Oct, Nov, Dec,
  MonthNow
}
 Abbreviated months. More...
 
enum  Day { DayNow = 0 }
 Current day. More...
 
enum  Year { YearNow = 0 }
 Current year. More...
 

Public Member Functions

 Date ()
 
 Date (const Date &d)
 
Dateoperator= (const Date &d)
 
 ~Date ()
 
bool set (unsigned short y, unsigned char m, unsigned char d, DateType t=DATE_LOCAL)
 
bool system_set (void)
 
bool leap_year (void) const
 
unsigned char day (void) const
 
unsigned char month (void) const
 
unsigned short year (void) const
 
const char * day_name (void)
 
const char * month_name (void)
 
unsigned char days_in_month () const
 
unsigned char day_of_week () const
 
unsigned short day_of_year () const
 
Dateoperator++ ()
 
Date operator++ (int)
 
Dateoperator-- ()
 
Date operator-- (int)
 

Static Public Member Functions

static bool leap_year (unsigned short y)
 
static unsigned char days_in_month (unsigned short y, unsigned char m)
 
static bool is_valid (unsigned short y, unsigned char m, unsigned char d)
 

Detailed Description

A class for date manipulation.

This class represents a calendar date. It has ability to read system date or to use and manipulate with filled year/month/day values.

Setting date is via set() member, and it accept year/month/day form. This is sample:

Date mydate;
mydate.set(2006, 1, 1);
// following will be true
mydate.day() == 1;
mydate.month() == 1;
mydate.year() == 1;

set() can also accept abbreviations for months or shorthands for current year/month/day (in this case will consult system date)

...
mydate.set(2006, Date::Jun, 10);
// or use curren date

set() will check input, and in case of bad date (either abnormal input values or setting feb/29 in non leap year) will trigger assert.

There is a one catch using abbreviations and valid date. For example, setting:

mydate.set(0,1,0)

is perfectly valid, since Date::YearNow and Date::DayNow enumerations are zero; in this case, function will behave as it was set:

Date contains operators for comparrisons, so this can be done with those:

Date d1,d2;
d1.set(2007, 1, 1);
d2.set(2007, 2, 1);
d1 != d2; // will be true
d1 <= d2; // also true
d2 = d1; // assign d1 values
d1 == d2; // true
...

Member Enumeration Documentation

◆ Day

enum Day

Current day.

Enumerator
DayNow 

Current system date.

◆ Month

enum Month

Abbreviated months.

Enumerator
Jan 

January.

Feb 

February.

Mar 

March.

Apr 

April.

May 

May.

Jun 

June.

Jul 

July.

Aug 

August.

Sep 

September.

Oct 

October.

Nov 

November.

Dec 

December.

MonthNow 

Current system month.

◆ Year

enum Year

Current year.

Enumerator
YearNow 

Current system year.

Constructor & Destructor Documentation

◆ Date() [1/2]

Date ( )

Construct empty date. All walues will be zero. This is not valid, so is_valid() member will return false.

◆ Date() [2/2]

Date ( const Date d)

Construct date via previously declared value

◆ ~Date()

~Date ( )

Clear internal data

Member Function Documentation

◆ day()

unsigned char day ( void  ) const
inline

Return internal day value

◆ day_name()

const char* day_name ( void  )

Return day name for internal day value, eg. Monday/Tuesday/... Returned data must not be modified since is pointing to static data. You should copy it if you need to modify it.

◆ day_of_week()

unsigned char day_of_week ( ) const

Return day in week (1..7) calculated from internal values.

Note
Values are in Western format, so 1 == Sunday, 2 == Monday, ...

◆ day_of_year()

unsigned short day_of_year ( ) const

Return day of year of currently set date

◆ days_in_month() [1/2]

unsigned char days_in_month ( ) const

Return number of days of internal year/month value

◆ days_in_month() [2/2]

static unsigned char days_in_month ( unsigned short  y,
unsigned char  m 
)
static

Return number of days in given year and month. Given data must be valid date

Returns
number of days for given year/month
Parameters
yis year to check
mis month to check

◆ is_valid()

static bool is_valid ( unsigned short  y,
unsigned char  m,
unsigned char  d 
)
static

Validate given year/month/day values, including leap year check

Returns
true if date is correct, or false if not
Parameters
yis year
mis month
dis day

◆ leap_year() [1/2]

static bool leap_year ( unsigned short  y)
static

Check if given year is leap year

Returns
true if it is or false if not
Parameters
yis year to check

◆ leap_year() [2/2]

bool leap_year ( void  ) const
inline

Check internal year value if is leap year

References Date::leap_year().

Referenced by Date::leap_year().

◆ month()

unsigned char month ( void  ) const
inline

Return internal month value

◆ month_name()

const char* month_name ( void  )

Return month name for internal month value, eg. January/February/... Returned data must not be modified, since applies as for day_name().

◆ operator++() [1/2]

Date& operator++ ( )

Increase current date by one. First day value is increased, then if that day is last in month, month is increased, then if that month is last in year, year is increased.

◆ operator++() [2/2]

Date operator++ ( int  )

Suffix increment

◆ operator--() [1/2]

Date& operator-- ( )

Reverse from operator++()

◆ operator--() [2/2]

Date operator-- ( int  )

Suffix decrement

◆ operator=()

Date& operator= ( const Date d)

Assign previously declared value to this. Current content will be replaced

◆ set()

bool set ( unsigned short  y,
unsigned char  m,
unsigned char  d,
DateType  t = DATE_LOCAL 
)

Fill internal data. Data should be valid date (including leap year) or assertion will be triggered.

Input can be either in form of numbers or abbreviations for month.

Todo:
it can accept set(0,1,0); leave this or... (possible bugs ?)
Returns
todo
Parameters
yfill with year value
mfill with month value; should be 1 <= m <= 12
dfill with day value; should be 1 <= d <= 30 (or 31 for months this days number or 28 if is non-leap year)
tfill with localized or UTC date values

◆ system_set()

bool system_set ( void  )

Tries to set system date with current values. This function will try to call settimeofday() and if not exists, it will fallback to stime(). If both functions are not present, it will return false.

Since these functions requires priviledged user, if they are present but user is not allow to set date, this function will return false too. Contrary this, it will return true if able to set date.

Todo:
This should be probably a static function

◆ year()

unsigned short year ( void  ) const
inline

Return internal year value


The documentation for this class was generated from the following file:
edelib::Date::YearNow
@ YearNow
Current system year.
Definition: DateTime.h:180
edelib::Date::DayNow
@ DayNow
Current system date.
Definition: DateTime.h:172
edelib::Date::Jun
@ Jun
June.
Definition: DateTime.h:157
edelib::Date::Jan
@ Jan
January.
Definition: DateTime.h:152
edelib::Date::Date
Date()
edelib::Date::MonthNow
@ MonthNow
Current system month.
Definition: DateTime.h:164