Dv::Date Class Reference

A simple Date class. More...

#include <date.h>

List of all members.

Public Member Functions

 Date ()
 Default constructor, initializes to ``now''.
 Date (time_t secs)
 Date(time_t t) assumes that t is number of seconds since Jan 1, 1970.
 Date (unsigned int year, unsigned int month, unsigned int day, unsigned int hrs=0, unsigned int min=0, unsigned int sec=0)
 Initialize Date, note that january = 0.
 Date (const std::string &s) throw (std::runtime_error)
 Construct a date from a string representation.
std::string str (const char *fmt=0) const
 Produce a string representing the date, according to format.
std::string gmt (const char *fmt=0) const
 Like str() but formats in GMT time zone, e.g.
long int gmt_offset () const
 Seconds east of UTC (GMT) of this date.
unsigned short year () const
 Return number of years.
unsigned short month () const
 Return month of Date.
int isdst () const
 Is daylight savings time (DST) in effect? See man mktime.
unsigned short day () const
 Return day-in-month of Date.
unsigned short hours () const
 Return the hour in the day of Date.
unsigned short minutes () const
 Return the minutes in the hour of this Date.
unsigned short seconds () const
 Return the seconds in the minute of this Date.
unsigned short wday (bool monday_is_0=false) const
 Return the day in the week of this Date.
unsigned short yday () const
 Return the day in the year of this Date.
unsigned short yweek () const
 Return the number of the week in the year of this Date.
unsigned long week_since_epoch () const
 Return the number of the week since the epoch.
unsigned long days_since_epoch () const
 Return the number days since the epoch.
void year (unsigned short yr)
 Set year.
void month (unsigned short mo)
 Set month.
void day (unsigned short day)
 Set day in month.
void hours (unsigned short hrs)
 Set hours.
void minutes (unsigned short mins)
 Set minutes.
void seconds (unsigned short secs)
 Set seconds.
void yday (unsigned short yday)
 Set date to date with this yday in same year.
time_t time () const
 Returns number of secs since 1/1/1970.
void time (time_t time)
 Set time.
bool operator< (const Date &d) const
 Date comparison respects chronological order.
bool operator== (const Date &d) const
 Date comparison respects chronological order.
bool operator> (const Date &d) const
 Date comparison respects chronological order.
bool operator!= (const Date &d) const
 Date comparison respects chronological order.
bool operator>= (const Date &d) const
 Date comparison respects chronological order.
bool operator<= (const Date &d) const
 Date comparison respects chronological order.
Dateoperator+= (const Duration &duration)
 Add a duration to a Date.

Private Attributes

time_t time_

Detailed Description

A simple Date class.

It provides a convenient interface to system functions such as locatime_r() etc. Its only data member is a time_t value. Note that only dates since Jan 1, 1970 can be represented.

The default copy constructor and assignment operations are available.

Definition at line 26 of file date.h.


Constructor & Destructor Documentation

Dv::Date::Date (  ) 

Default constructor, initializes to ``now''.

Dv::Date::Date ( time_t  secs  ) 

Date(time_t t) assumes that t is number of seconds since Jan 1, 1970.

Dv::Date::Date ( unsigned int  year,
unsigned int  month,
unsigned int  day,
unsigned int  hrs = 0,
unsigned int  min = 0,
unsigned int  sec = 0 
)

Initialize Date, note that january = 0.

Dv::Date::Date ( const std::string &  s  )  throw (std::runtime_error)

Construct a date from a string representation.

This constructor uses the gnu getdate parsing function which accepts a number of formats, e.g.

     * 1970-09-17           # ISO 8601.
     * 1970-09-17 20:02:00  
     * 70-9-17              # This century assumed by default.
     * 70-09-17             # Leading zeros are ignored.
     * 9/17/72              # Common U.S. writing.
     * 24 September 1972
     * 24 Sept 72           # September has a special abbreviation.
     * 24 Sep 72            # Three-letter abbreviations always allowed.
     * Sep 24, 1972
     * 24-sep-72
     * 24sep72
     *
     * 20:02:0
     * 20:02
     * 8:02pm
     * 20:02-0500		# In EST (Eastern U.S. Standard Time)
     * 

and many others (see the gnu fileutils documentation).

As a special case, it also accepts the Mysql timestamp format, i.e. strings of the form YYYYMMDDHHMMSS, e.g. "20051023170328".


Member Function Documentation

std::string Dv::Date::str ( const char *  fmt = 0  )  const

Produce a string representing the date, according to format.

The format is any string acceptable to strftime. If fmt=0, the default is "\%a \%b \%d \%H:\%M:\%S \%Y" e.g.

  "Wed Aug 12 18:38:10 1998"

The default format is acceptable to the parser.

std::string Dv::Date::gmt ( const char *  fmt = 0  )  const

Like str() but formats in GMT time zone, e.g.

03:00 MET DST is 02:00 GMT.

long int Dv::Date::gmt_offset (  )  const

Seconds east of UTC (GMT) of this date.

Returns:
the offset of the current timezone in seconds east from UTC
unsigned short Dv::Date::year (  )  const

Return number of years.

Returns:
the number of years of this Date.
unsigned short Dv::Date::month (  )  const

Return month of Date.

Returns:
the month (january = 0) of this Date.
int Dv::Date::isdst (  )  const

Is daylight savings time (DST) in effect? See man mktime.

Returns:
-1 if unknown
0 if not
>0 if DST is in effect
unsigned short Dv::Date::day (  )  const

Return day-in-month of Date.

Returns:
the day in the month (1..31) of this Date.
unsigned short Dv::Date::hours (  )  const

Return the hour in the day of Date.

Returns:
the hour in the day (0..23) of this Date.
unsigned short Dv::Date::minutes (  )  const

Return the minutes in the hour of this Date.

Returns:
the minutes (0..59) in the hour of this Date.
unsigned short Dv::Date::seconds (  )  const

Return the seconds in the minute of this Date.

Returns:
the seconds (0..59) in the minute of this Date.
unsigned short Dv::Date::wday ( bool  monday_is_0 = false  )  const

Return the day in the week of this Date.

Parameters:
monday_is_0 if true, use (monday = 0, tuesday = 1, .., sunday = 6) representation, otherwise use (sunday = 0, monday = 1, tuesday = 1, .., saturday = 6) representation.
Returns:
number of the day in the week of this Date, using the specified convention.
unsigned short Dv::Date::yday (  )  const

Return the day in the year of this Date.

Returns:
the day in the year (0..365) of this Date.
unsigned short Dv::Date::yweek (  )  const

Return the number of the week in the year of this Date.

From http://www.merlyn.demon.co.uk/: All ISO weeks are Monday = 1 to Sunday = 7, and week 01 of the numbering year is the one containing the first Thursday of the Gregorian year. Thus up to three days of the first and last weeks for a year number can be in the adjacent Gregorian year; December 29 to January 03 can have a numbering year differing from the Gregorian.

Returns:
The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
unsigned long Dv::Date::week_since_epoch (  )  const

Return the number of the week since the epoch.

Jan 1st 1970 is a thursday, so the first ISO week (with number 1) starts on monday Jan 5th 1970.

Returns:
the number of the week since the epoch (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET).
unsigned long Dv::Date::days_since_epoch (  )  const

Return the number days since the epoch.

Jan 1st 1970 is day 0. From http://www.merlyn.demon.co.uk/: All ISO weeks are Monday = 1 to Sunday = 7, and week 01 of the numbering year is the one containing the first Thursday of the Gregorian year. Thus up to three days of the first and last weeks for a year number can be in the adjacent Gregorian year; December 29 to January 03 can have a numbering year differing from the Gregorian.

Returns:
the number days since the epoch (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET).
void Dv::Date::year ( unsigned short  yr  ) 

Set year.

Functions that set part of the Date object. Values that are too large will be converted, e.g. to next day. E.g.

 Date d("Mon Jan 01 23:00:00 CET 2001");
 d.hours(36);
 cout<< d.str();

will print "Tue Jan 02 12:00:00 CET 2001".

Warning:
daylight savings time may cause strange behaviour
void Dv::Date::month ( unsigned short  mo  ) 

Set month.

Functions that set part of the Date object. Values that are too large will be converted, e.g. to next day. E.g.

 Date d("Mon Jan 01 23:00:00 CET 2001");
 d.hours(36);
 cout<< d.str();

will print "Tue Jan 02 12:00:00 CET 2001".

Parameters:
mo month (jan=0, .., dec=11)
Warning:
daylight savings time may cause strange behaviour
void Dv::Date::day ( unsigned short  day  ) 

Set day in month.

Functions that set part of the Date object. Values that are too large will be converted, e.g. to next day. E.g.

 Date d("Mon Jan 01 23:00:00 CET 2001");
 d.hours(36);
 cout<< d.str();

will print "Tue Jan 02 12:00:00 CET 2001".

Parameters:
day (1..31)
Warning:
daylight savings time may cause strange behaviour
void Dv::Date::hours ( unsigned short  hrs  ) 

Set hours.

Functions that set part of the Date object. Values that are too large will be converted, e.g. to next day. E.g.

 Date d("Mon Jan 01 23:00:00 CET 2001");
 d.hours(36);
 cout<< d.str();

will print "Tue Jan 02 12:00:00 CET 2001".

Parameters:
hrs (0..23)
Warning:
daylight savings time may cause strange behaviour
void Dv::Date::minutes ( unsigned short  mins  ) 

Set minutes.

Functions that set part of the Date object. Values that are too large will be converted, e.g. to next day. E.g.

 Date d("Mon Jan 01 23:00:00 CET 2001");
 d.hours(36);
 cout<< d.str();

will print "Tue Jan 02 12:00:00 CET 2001".

Parameters:
mins (0..59)
Warning:
daylight savings time may cause strange behaviour
void Dv::Date::seconds ( unsigned short  secs  ) 

Set seconds.

Functions that set part of the Date object. Values that are too large will be converted, e.g. to next day. E.g.

 Date d("Mon Jan 01 23:00:00 CET 2001");
 d.hours(36);
 cout<< d.str();

will print "Tue Jan 02 12:00:00 CET 2001".

Parameters:
secs (0..59)
Warning:
daylight savings time may cause strange behaviour
void Dv::Date::yday ( unsigned short  yday  ) 

Set date to date with this yday in same year.

Values that are too large will be converted, e.g. to next day. E.g.

 Date d("Mon Jan 01 23:00:00 CET 2001");
 d.hours(36);
 cout<< d.str();

will print "Tue Jan 02 12:00:00 CET 2001".

Parameters:
yday (0..365)
Warning:
daylight savings time may cause strange behaviour
time_t Dv::Date::time (  )  const [inline]

Returns number of secs since 1/1/1970.

Returns:
number of secs since the epoch (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET).

Definition at line 282 of file date.h.

References time_.

Referenced by operator<(), and operator==().

void Dv::Date::time ( time_t  time  ) 

Set time.

Parameters:
time number of secs since the epoch (Jan 1st 1970 00:00 GMT, or Jan 1st 1970 01:00 MET).
bool Dv::Date::operator< ( const Date d  )  const [inline]

Date comparison respects chronological order.

Definition at line 291 of file date.h.

References time().

bool Dv::Date::operator== ( const Date d  )  const [inline]

Date comparison respects chronological order.

Definition at line 293 of file date.h.

References time().

bool Dv::Date::operator> ( const Date d  )  const [inline]

Date comparison respects chronological order.

Definition at line 295 of file date.h.

bool Dv::Date::operator!= ( const Date d  )  const [inline]

Date comparison respects chronological order.

Definition at line 297 of file date.h.

bool Dv::Date::operator>= ( const Date d  )  const [inline]

Date comparison respects chronological order.

Definition at line 299 of file date.h.

bool Dv::Date::operator<= ( const Date d  )  const [inline]

Date comparison respects chronological order.

Definition at line 301 of file date.h.

Date& Dv::Date::operator+= ( const Duration duration  ) 

Add a duration to a Date.

Parameters:
duration to add

Member Data Documentation

time_t Dv::Date::time_ [private]

Definition at line 312 of file date.h.

Referenced by time().


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

dvutil-1.0.10 [ 5 December, 2009]