home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
IBMCLASS
/
IDATE.HPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-22
|
14KB
|
301 lines
#ifndef _IDATE_
#define _IDATE_
/*******************************************************************************
* FILE NAME: idate.hpp *
* *
* DESCRIPTION: *
* Declaration of the class(es): *
* IDate - Class to represent dates *
* *
* COPYRIGHT: *
* Licensed Materials - Property of IBM *
* (C) Copyright IBM Corporation 1992, 1993 *
* All Rights Reserved *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************/
#ifndef _IBASE_
#include <ibase.hpp>
#endif
/*----------------------------------------------------------------------------*/
/* Align classes on four byte boundary. */
/*----------------------------------------------------------------------------*/
#pragma pack(4)
class IString;
class ostream;
struct _CDATE;
class IDate : public IBase {
/*******************************************************************************
* Objects of the IDate class represent specified dates. The class also *
* provides general day and date handling functions. *
* *
* Externally, dates appear to consist of three pieces of information: a year, *
* a month within that year, and a day within that month. Support is also *
* provided for simply specifying the day within the year. *
* *
* The IDate class returns language-sensitive information (names of days and *
* months) in the language defined by the current locale. See the description *
* of the Standard C function setlocale in the C++ Language Reference for *
* information about setting the locale. *
*******************************************************************************/
public:
/*--------------------------- Nested Declarations ------------------------------
| IDate defines the following related types: |
| DayOfWeek - Enumeration values Monday through Sunday for the days |
| of the week. |
| Month - Enumeration values January through December for the months |
| of the year. |
| YearFormat - Specifies the number of digits in the year for the default |
| asString format (yy or yyyy). |
------------------------------------------------------------------------------*/
typedef enum
{
Monday = 0,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
} DayOfWeek;
typedef enum
{
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
} Month;
typedef enum
{
yy,
yyyy
} YearFormat;
/*------------------------ Constructors ---------------------------------------
| You can construct an instance of this class in the following ways: |
| |
| - By using the default constructor, which returns the current day. |
| - By giving the year, month, and day for the desired day (accepted as |
| either month/day/year or day/month/year). |
| - By giving the year and day of the year for the desired day. |
| - By using the static member function today to return the current date. |
| - By copying another IDate object. |
| - By giving the Julian day number (as a long). |
| - By giving a container details CDATE structure. |
------------------------------------------------------------------------------*/
IDate ( );
IDate ( Month aMonth,
int aDay,
int aYear );
IDate ( int aDay,
Month aMonth,
int aYear );
IDate ( int aYear,
int aDay );
IDate ( const IDate &aDate );
IDate ( unsigned long julianDayNumber );
IDate ( const _CDATE &cDate );
/*-------------------------------- Accessors -----------------------------------
| These functions provide access to various portions of the |
| representation of the IDate object: |
| |
| asString - Returns the IDate as a string. The default is formatted |
| per the system (mm-dd-yy). An alternate version permits |
| the use of any strftime conversion specifiers (for |
| example, "%x" yields a string such as "Apr 10 1959" ). |
| asCDATE - Returns a container CDATE structure for the date. |
| dayOfWeek - Returns the index of the receiver's day of the week: |
| Monday through Sunday. |
| dayName - Returns the name of the receiver's day of the week. |
| dayOfMonth - Returns the day in the receiver's month as an integer |
| from 1 to 31. |
| dayOfYear - Returns the day in the receiver's year as an integer from |
| 1 to 366. |
| julianDate - Returns the Julian day number of the receiver IDate. |
| monthOfYear - Returns the index of the receiver's month of the year: |
| January through December. |
| monthName - Returns the name of the receiver's month. |
| year - Returns the receiver's year. The form returned is yyyy. |
| today - Static function that returns the current date. |
------------------------------------------------------------------------------*/
IString
asString ( YearFormat yearFmt = yy ) const,
asString ( const char *fmt ) const,
dayName ( ) const,
monthName ( ) const;
_CDATE
asCDATE ( ) const;
DayOfWeek
dayOfWeek ( ) const;
int
dayOfMonth ( ) const,
dayOfYear ( ) const;
unsigned long
julianDate ( ) const;
Month
monthOfYear ( ) const;
int
year ( ) const;
static IDate
today ( );
/*-------------------------------- Stream I/O ----------------------------------
| |
| operator << - This operator can be used to put an IDate representation to |
| an output stream. It puts the default asString |
| representation. |
------------------------------------------------------------------------------*/
friend ostream
&operator << ( ostream &aStream,
const IDate &aDate );
/*-------------------------- Comparison Operators -------------------------------
| Two IDates can be compared using the full complement of comparison |
| operators and applying the natural meaning. |
| |
| operator == - Returns true if the IDate objects represent the same date. |
| operator != - Returns true if the IDate objects represent different |
| dates. |
| operator < - Returns true if the left-hand operand represents a date |
| prior to the date represented by the right-hand operand. |
| operator <= - Returns true if the left-hand operand represents a date |
| prior to or identical to the date represented by the |
| right-hand operand. |
| operator > - Returns true if the left-hand operand represents a date |
| subsequent to the date represented by the right-hand |
| operand. |
| operator >= - Returns true if the left-hand operand represents a date |
| subsequent to or identical to the date represented by the |
| right-hand operand. |
-------------------------------------------------------------------------------*/
Boolean
operator == ( const IDate &aDate ) const,
operator != ( const IDate &aDate ) const,
operator < ( const IDate &aDate ) const,
operator <= ( const IDate &aDate ) const,
operator > ( const IDate &aDate ) const,
operator >= ( const IDate &aDate ) const;
/*------------------------- Manipulation Operators --------------------------------
| Manipulation operators: |
| |
| operator + - Adds an integral number of days to the left-hand operand, |
| yielding a new IDate. |
| operator - - Subtracts an integral number of days from the left-hand |
| operand, yeilding a new IDate. If the right-hand operand |
| is also an IDate, then the operator yields the number of |
| days between the dates. |
| operator += - Adds an integral number of days to the left-hand operand, |
| assigning the result to that operand. |
| operator -= - Subtracts an integral number of days from the right-hand |
| operand, assigning the result to that operand. |
---------------------------------------------------------------------------------*/
IDate
operator + ( int numDays ) const,
operator - ( int numDays ) const;
IDate
&operator += ( int numDays ),
&operator -= ( int numDays );
long
operator - ( const IDate &aDate ) const;
/*--------------------------------- Statics ------------------------------------
| These functions are static. |
| They provide general IDate utilities independent of specific IDates. |
| |
| dayName - Returns the name of the week day corresponding to the |
| argument DayOfWeek index. |
| monthName - Returns the name of the month with the argument |
| index. |
| daysInMonth - Returns the number of days in a given month (in a given |
| year). The form of aYear is yyyy. |
| daysInYear - Returns the number of days in a given year. The form of |
| aYear is yyyy. |
| isLeapYear - Returns true if the argument year is a leap year; |
| otherwise, it returns false. The form of aYear is yyyy. |
| isValid - Indicates whether the argument date (given as either |
| year/month/day or year/day) is valid. For example, |
| February 29, 1990 is not a valid date because February had |
| only 28 days in 1990. The form of aYear is yyyy. |
------------------------------------------------------------------------------*/
static IString
dayName ( DayOfWeek aDay ),
monthName ( Month aMonth );
static int
daysInMonth ( Month aMonth,
int aYear ),
daysInYear ( int aYear );
static Boolean
isLeapYear ( int aYear ),
isValid ( Month aMonth,
int aDay,
int aYear ),
isValid ( int aDay,
Month aMonth,
int aYear ),
isValid ( int aYear,
int aDay );
protected:
/*----------------------------- Implementation ---------------------------------
| This function is used to implement class IDate: |
| initialize - Calculates the Julian day number. The form of aYear is |
| yyyy. It returns a reference to the receiver, initialized |
| to the given date. |
------------------------------------------------------------------------------*/
IDate
&initialize ( Month aMonth,
int aDay,
int aYear );
private:
/*--------------------------------- Private ----------------------------------*/
unsigned long
julian;
}; // IDate
/*----------------------------------------------------------------------------*/
/* Resume compiler default packing. */
/*----------------------------------------------------------------------------*/
#pragma pack()
#ifndef I_NO_INLINES
#include <idate.inl>
#endif
#endif /* _IDATE_ */