home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / ibmcli / idate.hp_ / IDATE.HPP
Encoding:
C/C++ Source or Header  |  1992-10-26  |  10.8 KB  |  251 lines

  1. #ifndef _IDATE_
  2. #define _IDATE_
  3. /*******************************************************************************
  4. * FILE NAME: idate.hpp                                                         *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     IDate - Class to represent dates                                         *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   Licensed Materials - Property of IBM                                       *
  12. *   (C) Copyright IBM Corporation 1992, 1993                                   *
  13. *   All Rights Reserved                                                        *
  14. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  15. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  16. *                                                                              *
  17. *$Log:   R:/IBMCLASS/IBASE/VCS/IDATE.HPV  $                                                                         *
  18. //
  19. //   Rev 1.2   25 Oct 1992 16:46:26   nunn
  20. //changed library name to ICLUI
  21.    
  22.       Rev 1.1   13 Oct 1992 09:58:00   law
  23.    Removed virtual inheritance from IBase.
  24.    
  25.       Rev 1.0   07 Oct 1992 10:10:16   law
  26.    Initial revision.
  27. *******************************************************************************/
  28. #ifndef _IBASE_
  29.   #include <ibase.hpp>
  30. #endif
  31.  
  32. #ifndef _ISTRING_
  33.   #include <istring.hpp>
  34. #endif
  35.  
  36. class ostream;
  37.  
  38. class IDate : public IBase {
  39. /*******************************************************************************
  40. * Objects of this class represent given days and the class also provides       *
  41. * general day/date handling functions.                                         *
  42. *                                                                              *
  43. * Externally, dates appear to consist of three pieces of information: a year,  *
  44. * a month within that year, and a day within that month (alternatively,        *
  45. * support is provided for simply specifying the day within the year).          *
  46. *******************************************************************************/
  47. public:
  48. /*--------------------------- NESTED DECLARATIONS ------------------------------
  49. | IDate defines the following related types:                                   |
  50. |   DayOfWeek - enumeration values Monday-Sunday for the days of the week.     |
  51. |   Month     - enumeration values January-December for the months of the year.|
  52. ------------------------------------------------------------------------------*/
  53. typedef enum 
  54.   {
  55.   Monday = 0,
  56.   Tuesday,
  57.   Wednesday,
  58.   Thursday,
  59.   Friday,
  60.   Saturday,
  61.   Sunday 
  62.   } DayOfWeek;
  63.  
  64. typedef enum
  65.   {
  66.   January = 1,
  67.   February,
  68.   March,
  69.   April,
  70.   May,
  71.   June,
  72.   July,
  73.   August,
  74.   September,
  75.   October,
  76.   November,
  77.   December 
  78.   } Month;
  79.  
  80.  
  81. /*------------------------ CONSTRUCTORS/DESTRUCTORS ----------------------------
  82. | There are six ways to construct IDate objects:                               |
  83. |                                                                              |
  84. | 1. Using the default constructor, which returns the current                  |
  85. |    day                                                                       |
  86. |                                                                              |
  87. | 2. By giving the year, month, and day for the desired day                    |
  88. |    (accepted as either month/day/year or day/month/year)                     |
  89. |                                                                              |
  90. | 3. By giving the year and day of the year for the desired day                |
  91. |                                                                              |
  92. | 4. By using the static member function today() to return the                 |
  93. |    current date                                                              |
  94. |                                                                              |
  95. | 5. By copying another IDate object.                                          |
  96. |                                                                              |
  97. | 6. By giving the Julian day number (as a long).                              |
  98. ------------------------------------------------------------------------------*/
  99.   IDate ( );
  100.  
  101.   IDate ( Month aMonth,
  102.           int   aDay,
  103.           int   aYear );
  104.  
  105.   IDate ( int   aDay,
  106.           Month aMonth,
  107.           int   aYear );
  108.  
  109.   IDate ( int   aYear,
  110.           int   aDay );
  111.  
  112.   IDate ( const IDate &aDate );
  113.  
  114.   IDate ( unsigned long julianDayNumber );
  115.  
  116.   static IDate
  117.      today ( );
  118.  
  119. /*-------------------------------- ACCESSORS -----------------------------------
  120. | These functions provide access to various portions of the                    |
  121. | representation of the IDate object:                                          |
  122. |                                                                              |
  123. | asString    -  returns the IDate as a string like "April 10, 1959"           |
  124. | dayOfWeek   -  returns the index of the receiver's day of the                |
  125. |                week: Monday ... Sunday                                       |
  126. | dayName     -  returns the name of the receiver's day of the week            |
  127. | dayOfMonth  -  returns the day in the receiver's month as an                 |
  128. |                integer 1-31                                                  |
  129. | dayOfYear   -  returns the day in the receiver's year as an                  |
  130. |                integer 1-366                                                 |
  131. | julianDate  -  returns the Julian day number of the receiver IDate           |
  132. | monthOfYear -  returns the index of the receiver's month of the              |
  133. |                year: January ... December                                    |
  134. | monthName   -  returns the name of the receiver's month                      |
  135. | year        -  returns the receiver's year                                   |
  136. ------------------------------------------------------------------------------*/
  137. IString
  138.   asString  ( ) const,
  139.   dayName   ( ) const,
  140.   monthName ( ) const;
  141.  
  142. DayOfWeek
  143.   dayOfWeek ( ) const;
  144.  
  145. int
  146.   dayOfMonth  ( ) const,
  147.   dayOfYear   ( ) const;
  148.  
  149. unsigned long 
  150.   julianDate  ( ) const;
  151.  
  152. Month
  153.   monthOfYear ( ) const;
  154.  
  155. int
  156.   year        ( ) const;
  157.  
  158. /*--------------------------------- DISPLAY ------------------------------------
  159. | This function permits a Date to be output on an ostream using the            |
  160. | conventional left-shift (<<) operator.                                       |
  161. ------------------------------------------------------------------------------*/
  162. friend ostream
  163.  &operator << ( ostream     &aStream,
  164.                 const IDate &aDate );
  165.  
  166. /*-------------------------- COMPARISON OPERATORS ------------------------------
  167. | Two IDates can be compared using the full complement of comparison           |
  168. | operators and applying the natural meaning.                                  |
  169. ------------------------------------------------------------------------------*/
  170. Boolean
  171.   operator == ( const IDate &aDate ) const,
  172.   operator != ( const IDate &aDate ) const,
  173.   operator <  ( const IDate &aDate ) const,
  174.   operator <= ( const IDate &aDate ) const,
  175.   operator >  ( const IDate &aDate ) const,
  176.   operator >= ( const IDate &aDate ) const;
  177.  
  178. /*------------------------- MANIPULATION OPERATORS -----------------------------
  179. | These operators permit an integral number of days to be added or             |
  180. | subtracted from a given IDate via operator+ and operator-, respectively.     |
  181. |                                                                              |
  182. | Two IDates can be subtracted to produce the number of days between them.     |
  183. |                                                                              |
  184. | The result can be placed back into the receiver via operator+= and           |
  185. | operator-=, respectively.                                                    |
  186. ------------------------------------------------------------------------------*/
  187. IDate
  188.   operator +  ( int numDays ) const,
  189.   operator -  ( int numDays ) const;
  190.  
  191. IDate
  192.  &operator += ( int numDays ),
  193.  &operator -= ( int numDays );
  194.  
  195. long
  196.   operator -  ( const IDate &aDate ) const;
  197.  
  198. /*--------------------------------- STATICS ------------------------------------
  199. | These functions are static; they provide general IDate utilities             |
  200. | independent of specific IDates.                                              |
  201. |                                                                              |
  202. | dayName     -  returns the name of the week day corresponding to the         |
  203. |                argument DayOfWeek index                                      |
  204. | monthName   -  returns the name of the month with the argument               |
  205. |                index                                                         |
  206. | daysInMonth -  returns the number of days in a given month (in a given year) |
  207. | daysInYear  -  returns the number of days in a given year                    |
  208. | isLeapYear  -  returns true if the argument year is a leap year,             |
  209. |                false otherwise                                               |
  210. | isValid     -  indicates whether the argument date (given as                 |
  211. |                either year/month/day or year/day) is valid (e.g.,            |
  212. |                February 29, 1990 is *not* valid)                             |
  213. ------------------------------------------------------------------------------*/
  214. static IString
  215.   dayName   ( DayOfWeek aDay   ),
  216.   monthName ( Month     aMonth );
  217.  
  218. static int
  219.   daysInMonth ( Month aMonth,
  220.                 int   aYear ),
  221.   daysInYear  ( int   aYear );
  222.  
  223. static Boolean
  224.   isLeapYear ( int   aYear ),
  225.   isValid    ( Month aMonth,
  226.                int   aDay,
  227.                int   aYear ),
  228.   isValid    ( int   aDay,
  229.                Month aMonth,
  230.                int   aYear ),
  231.   isValid    ( int   aYear,
  232.                int   aDay );
  233.  
  234. protected:
  235. /*----------------------------- IMPLEMENTATION ---------------------------------
  236. | These functions are utilized to implement class IDate:                       |
  237. |   initialize - calculate julian day number                                   |
  238. ------------------------------------------------------------------------------*/
  239. IDate
  240.  &initialize ( Month aMonth,
  241.                int   aDay,
  242.                int   aYear );
  243.  
  244. private:
  245. /*--------------------------------- PRIVATE ----------------------------------*/
  246. unsigned long
  247.   julian;
  248. }; // IDate
  249.  
  250. #endif /* _IDATE_ */
  251.