Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Date Class Reference

Represents dates as Julian day numbers. More...

#include "Date.hpp"

List of all members.

Public Methods

 Date ()
 Default constructor.

 Date (const Date &Orig)
 Copy constructor.

 Date (const time_t tSysTime)
 time_t constructor.

 Date (const char *String)
 Constructs an object from a string.

 Date (const int iDay, const int iMonth, const int iYear)
 Year/month/day constructor.

int Year (void) const
 Returns year of the date.

int Month (void) const
 Returns month of the date.

int Day (void) const
 Returns month of the date.

int DayOfYear (void) const
 Returns day of the year.

int DayOfWeek (void) const
 Returns day of the week, starting week on Sunday.

int DayOfWorkWeek (void) const
 Returns day of the workweek, starting week on Monday.

int IsLeapYear (void) const
 Is this date a leap year?.

void YMD (int *pY, int *pM, int *pD)
 Get the year, month and day values in one go.

Date & operator+= (const long Right)
 Additive operator.

Date & operator-= (const long Right)
 Additive operator.

long operator- (const Date &Right)
 Operator subtracting Dates.

void ToString (char *szBuffer) const
 Formats the Date into an ASCII representation.

Date & operator= (const Date &Orig)
 Assignment operator.

time_t ToSysTime (void) const
 Converts the Date to a time_t value representing midnight of that date.

++ and -- operator, postfix forms
Date & operator++ ()
Date & operator-- ()
++ and -- operators, prefix forms.
Date operator++ (int)
Date operator-- (int)
Comparison operators
Several comparison operators, all with the same parameter and return value.

int operator== (const Date &Right) const
int operator!= (const Date &Right) const
int operator< (const Date &Right) const
int operator<= (const Date &Right) const
int operator> (const Date &Right) const
int operator>= (const Date &Right) const

Static Public Methods

int IsLeapYear (const int iYear)
 Check if a year is a leapyear.


Friends

Date operator+ (const Date &Left, const long Right)
 Additive operator.

Date operator+ (const long Left, const Date &Right)
 Additive operator.

Date operator- (const Date &Left, const long Right)
 Subtract operator.

Date operator- (const long Left, const Date &Right)
 Subtract operator.


Detailed Description

Represents dates as Julian day numbers.

Author:
Todd Knarr
Date:
29 Nov 1995
The Date class is intended as a general representation of dates. It provides accuracy to the day, and covers all dates from 1 Jan 4713BC up through 31 Dec 9999AD. It implements addition, subtraction and the relational operators insensible ways, plus useful conversions and date-related member functions. Internally this class represents dates as a number of days from 1 Jan 4713BC, referred to by astronomers as the Julian day number.

Note:
Years AD are positive, years BC are negative. There is no year 0AD, it goes from 1BC to 1AD. A year of 0 will be treated as 1BC. No attempt is made to validate ranges. Physical errors will not result from insane day-month combinations like the 87th day of the 45th month, but the results will obviously not make much sense.

Date conversion routines by Eric bergman-Terrell, Computer Language, Dec 1990. Alternate method by David Burki, CUJ Feb 1993. Use #define JULDATE_USE_ALTERNATE_METHOD to select a method.

Adding two Dates is not defined.

Definition at line 14 of file Date.hpp.


Constructor & Destructor Documentation

Date::Date   [inline]
 

Default constructor.

Constructs a new object initialized to 1 Jan 4713BC

Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 95 of file Date.hpp.

00095 { lJulianDay = 0L; }

Date::Date const Date &    Orig [inline]
 

Copy constructor.

Parameters:
Orig The original Date.
Author:
Todd Knarr
Date:
13 Mar 1996

Definition at line 104 of file Date.hpp.

References lJulianDay.

00104 { lJulianDay = Orig.lJulianDay; }

Date::Date const time_t    tSysTime [inline]
 

time_t constructor.

Constructs an object initialized to the date represented by a system time value.

Parameters:
tSysTime system time value.
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 115 of file Date.hpp.

00116     {
00117         struct tm *ptm;
00118         int y, m, d;
00119 
00120         ptm = localtime( &tSysTime );
00121         y = ptm->tm_year + 1900;
00122         m = ptm->tm_mon + 1;
00123         d = ptm->tm_mday;
00124         lJulianDay = YmdToJd( y, m, d );
00125     }

Date::Date const char *    String [inline]
 

Constructs an object from a string.

Parameters:
String ASCII representation of the long Julian day number.
Author:
Todd Knarr
Date:
30 Nov 1995

Definition at line 134 of file Date.hpp.

00135     {
00136         lJulianDay = atol( String );
00137     }

Date::Date const int    iDay,
const int    iMonth,
const int    iYear
[inline]
 

Year/month/day constructor.

Constructs a Date initialized by the arguments.

Parameters:
iDay Day.
iMonth Month
iYear Year.
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 150 of file Date.hpp.

00151     {
00152         lJulianDay = YmdToJd( iYear, iMonth, iDay );
00153     }


Member Function Documentation

int Date::Day void    const [inline]
 

Returns month of the date.

Returns:
Day (1-31)
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 189 of file Date.hpp.

00190     {
00191         int y, m, d;
00192 
00193         JdToYmd( lJulianDay, &y, &m, &d );
00194         return d;
00195     }

int Date::DayOfWeek void    const [inline]
 

Returns day of the week, starting week on Sunday.

matching localtime()'s convention

Returns:
Day, 0 through 6
Return values:
0 Sunday
6 Saterday
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 209 of file Date.hpp.

00210     {
00211         return ( ( ( (int) ( lJulianDay % 7L ) ) + 1 ) % 7 );
00212     }

int Date::DayOfWorkWeek void    const [inline]
 

Returns day of the workweek, starting week on Monday.

Returns:
Day, 0 through 6
Return values:
0 Monday
6 Sunday
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 222 of file Date.hpp.

00223     {
00224         return ( (int) ( lJulianDay % 7L ) );
00225     }

int Date::DayOfYear void    const
 

Returns day of the year.

Returns:
Day (1-365), with 1 Jan being day 1.
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 211 of file Date.cpp.

00212 {
00213     int y, m, d;
00214     long soy;
00215 
00216     JdToYmd( lJulianDay, &y, &m, &d );
00217     soy = YmdToJd( y, 1, 1 );
00218     return (int) ( lJulianDay - soy + 1 );
00219 }

int Date::IsLeapYear void    const [inline]
 

Is this date a leap year?.

Return values:
0 a leap year
1 no leap year
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 234 of file Date.hpp.

00235     {
00236         int y, m, d;
00237         JdToYmd( lJulianDay, &y, &m, &d );
00238         return IsLeapYear( y );
00239     }

int Date::IsLeapYear const int    iYear [inline, static]
 

Check if a year is a leapyear.

Parameters:
iYear Year to check.
Return values:
1 The year given is a leap year.
0 The year given is NOT a leap year.
Author:
Todd Knarr
Date:
5 Dec 1995

Definition at line 80 of file Date.hpp.

00081     {
00082         long jd1, jd2;
00083         jd1 = YmdToJd( iYear, 2, 29 );
00084         jd2 = YmdToJd( iYear, 3, 1 );
00085         return (int) ( jd2 - jd1 );
00086     }

int Date::Month void    const [inline]
 

Returns month of the date.

Returns:
Month (1-12)
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 175 of file Date.hpp.

00176     {
00177         int y, m, d;
00178 
00179         JdToYmd( lJulianDay, &y, &m, &d );
00180         return m;
00181     }

Date Date::operator++ int    [inline]
 

Returns:
A new Date.
Author:
Todd Knarr
Date:
1 Dec 1995

Definition at line 394 of file Date.hpp.

00395     {
00396         Date Temp = *this;
00397         lJulianDay++;
00398         return Temp;
00399     }

Date& Date::operator++   [inline]
 

Returns:
Reference to this Date.
Author:
Todd Knarr
Date:
1 Dec 1995

Definition at line 374 of file Date.hpp.

00375     {
00376         lJulianDay++;
00377         return *this;
00378     }

Date& Date::operator+= const long    Right [inline]
 

Additive operator.

Parameters:
Right Number of days to add.
Returns:
Reference to this date.
Author:
Todd Knarr
Date:
30 Nov 1995

Definition at line 334 of file Date.hpp.

00335     {
00336         Add( Right );
00337         return *this;
00338     }

long Date::operator- const Date &    Right [inline]
 

Operator subtracting Dates.

Parameters:
Right The Date to subtract with.
Returns:
Difference between the two Dates in days.
Author:
Todd Knarr
Date:
30 Nov 1995

Definition at line 361 of file Date.hpp.

References lJulianDay.

00362     {
00363         return lJulianDay - Right.lJulianDay;
00364     }

Date& Date::operator-= const long    Right [inline]
 

Additive operator.

Parameters:
Right Number of days to add.
Returns:
Reference to this date.
Author:
Todd Knarr
Date:
30 Nov 1995

Definition at line 347 of file Date.hpp.

00348     {
00349         Subtract( Right );
00350         return *this;
00351     }

Date& Date::operator= const Date &    Orig [inline]
 

Assignment operator.

Parameters:
Orig The original Date.
Author:
Todd Knarr
Date:
13 Mar 1996

Definition at line 418 of file Date.hpp.

References lJulianDay.

00419     {
00420         lJulianDay = Orig.lJulianDay;
00421         return *this;
00422     }

int Date::operator== const Date &    Right const [inline]
 

Parameters:
Right The Date to compare with.
Returns:
non-zero if the comparison succeeds, 0 if it fails
Author:
Todd Knarr
Date:
1 Dec 1995

Definition at line 435 of file Date.hpp.

References lJulianDay.

00436     {
00437         return lJulianDay == Right.lJulianDay;
00438     }

void Date::ToString char *    szBuffer const
 

Formats the Date into an ASCII representation.

This is the ASCII form of the long Julian day number. The string is a fixed-length 12-character string, including the NUL terminator.

Parameters:
szBuffer Pointer to string buffer to fill in.
Author:
Todd Knarr
Date:
30 Nov 1995

Definition at line 186 of file Date.cpp.

00187 {
00188     int i;
00189     long Temp;
00190 
00191     Temp = lJulianDay;
00192     if ( Temp < 0L )
00193         szBuffer[0] = '-';
00194     else
00195         szBuffer[0] = '+';
00196     szBuffer[11] = '\0';
00197     for ( i = 10; i > 0; i-- )
00198     {
00199         szBuffer[i] = ( Temp % 10L ) + '0';
00200         Temp /= 10;
00201     }
00202 
00203     return;
00204 }

time_t Date::ToSysTime void    const
 

Converts the Date to a time_t value representing midnight of that date.

Author:
Todd Knarr
Returns:
Converted result.
Date:
29 Nov 1995

Definition at line 226 of file Date.cpp.

00227 {
00228     struct tm tmRep;
00229     int y, m, d;
00230     time_t t;
00231     
00232     JdToYmd( lJulianDay, &y, &m, &d );
00233     if ( y < 1970 )
00234     {
00235         y = 70;
00236         m = 1;
00237         d = 1;
00238     }
00239     tmRep.tm_year = y - 1900 ;
00240     tmRep.tm_mon = m-1;
00241     tmRep.tm_mday = d;
00242     tmRep.tm_hour = 0;
00243     tmRep.tm_min = 0;
00244     tmRep.tm_sec = 0;
00245     tmRep.tm_isdst = 0;
00246     
00247     t = mktime( &tmRep );
00248     return t;
00249 }

int Date::Year void    const [inline]
 

Returns year of the date.

Returns:
Year
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 161 of file Date.hpp.

00162     {
00163         int y, m, d;
00164 
00165         JdToYmd( lJulianDay, &y, &m, &d );
00166         return y;
00167     }

void Date::YMD int *    pY,
int *    pM,
int *    pD
[inline]
 

Get the year, month and day values in one go.

Puts the year, month and day values directly into three integer variables, for times when you need all three at the same time.

Parameters:
pY Pointer to int holding Year
pM Pointer to int holding Month
pD Pointer to int holding Day
Author:
Todd Knarr
Date:
29 Nov 1995

Definition at line 252 of file Date.hpp.

00253     {
00254         JdToYmd( lJulianDay, pY, pM, pD );
00255         return;
00256     }


Friends And Related Function Documentation

Date operator+ const long    Left,
const Date &    Right
[friend]
 

Additive operator.

Parameters:
Left Number of days to add.
Right The Date to add to.
Returns:
New Date.
Author:
Todd Knarr
Date:
30 Nov 1995
:

Definition at line 285 of file Date.hpp.

00286     {
00287         Date Temp = Right;
00288         Temp.Add( Left );
00289         return Temp;
00290     }

Date operator+ const Date &    Left,
const long    Right
[friend]
 

Additive operator.

Parameters:
Left The Date to add to.
Right Number of days to add.
Returns:
New Date.
Author:
Todd Knarr
Date:
30 Nov 1995
:

Definition at line 268 of file Date.hpp.

00269     {
00270         Date Temp = Left;
00271         Temp.Add( Right );
00272         return Temp;
00273     }

Date operator- const long    Left,
const Date &    Right
[friend]
 

Subtract operator.

Parameters:
Left Number of days to subtract.
Right The Date to subtract from.
Returns:
New Date.
Author:
Todd Knarr
Date:
30 Nov 1995

:

Definition at line 319 of file Date.hpp.

00320     {
00321         Date Temp = Right;
00322         Temp.Subtract( Left );
00323         return Temp;
00324     }

Date operator- const Date &    Left,
const long    Right
[friend]
 

Subtract operator.

Parameters:
Left The Date to subtract from.
Right Number of days to subtract.
Returns:
New Date.
Author:
Todd Knarr
Date:
30 Nov 1995
:

Definition at line 302 of file Date.hpp.

00303     {
00304         Date Temp = Left;
00305         Temp.Subtract( Right );
00306         return Temp;
00307     }


The documentation for this class was generated from the following files:
Generated on Mon Jan 27 21:09:54 2003 for Date Class by doxygen1.3-rc2