home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IDATE.HPP < prev    next >
C/C++ Source or Header  |  1993-10-22  |  14KB  |  301 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. *******************************************************************************/
  18. #ifndef _IBASE_
  19.   #include <ibase.hpp>
  20. #endif
  21.  
  22. /*----------------------------------------------------------------------------*/
  23. /* Align classes on four byte boundary.                                       */
  24. /*----------------------------------------------------------------------------*/
  25. #pragma pack(4)
  26.  
  27. class IString;
  28.  
  29. class ostream;
  30.  
  31. struct _CDATE;
  32.  
  33. class IDate : public IBase {
  34. /*******************************************************************************
  35. * Objects of the IDate class represent specified dates.  The class also        *
  36. * provides general day and date handling functions.                            *
  37. *                                                                              *
  38. * Externally, dates appear to consist of three pieces of information: a year,  *
  39. * a month within that year, and a day within that month.  Support is also      *
  40. * provided for simply specifying the day within the year.                      *
  41. *                                                                              *
  42. * The IDate class returns language-sensitive information (names of days and    *
  43. * months) in the language defined by the current locale.  See the description  *
  44. * of the Standard C function setlocale in the C++ Language Reference for       *
  45. * information about setting the locale.                                        *
  46. *******************************************************************************/
  47. public:
  48. /*--------------------------- Nested Declarations ------------------------------
  49. | IDate defines the following related types:                                   |
  50. |   DayOfWeek  - Enumeration values Monday through Sunday for the days         |
  51. |                of the week.                                                  |
  52. |   Month      - Enumeration values January through December for the months    |
  53. |                of the year.                                                  |
  54. |   YearFormat - Specifies the number of digits in the year for the default    |
  55. |                asString format (yy or yyyy).                                 |
  56. ------------------------------------------------------------------------------*/
  57. typedef enum
  58.   {
  59.   Monday = 0,
  60.   Tuesday,
  61.   Wednesday,
  62.   Thursday,
  63.   Friday,
  64.   Saturday,
  65.   Sunday
  66.   } DayOfWeek;
  67.  
  68. typedef enum
  69.   {
  70.   January = 1,
  71.   February,
  72.   March,
  73.   April,
  74.   May,
  75.   June,
  76.   July,
  77.   August,
  78.   September,
  79.   October,
  80.   November,
  81.   December
  82.   } Month;
  83.  
  84. typedef enum
  85.   {
  86.   yy,
  87.   yyyy
  88.   } YearFormat;
  89.  
  90. /*------------------------ Constructors ---------------------------------------
  91. | You can construct an instance of this class in the following ways:          |
  92. |                                                                             |
  93. |   - By using the default constructor, which returns the current day.        |
  94. |   - By giving the year, month, and day for the desired day (accepted as     |
  95. |     either month/day/year or day/month/year).                               |
  96. |   - By giving the year and day of the year for the desired day.             |
  97. |   - By using the static member function today to return the current date.   |
  98. |   - By copying another IDate object.                                        |
  99. |   - By giving the Julian day number (as a long).                            |
  100. |   - By giving a container details CDATE structure.                          |
  101. ------------------------------------------------------------------------------*/
  102.   IDate ( );
  103.  
  104.   IDate ( Month aMonth,
  105.           int   aDay,
  106.           int   aYear );
  107.  
  108.   IDate ( int   aDay,
  109.           Month aMonth,
  110.           int   aYear );
  111.  
  112.   IDate ( int   aYear,
  113.           int   aDay );
  114.  
  115.   IDate ( const IDate &aDate );
  116.  
  117.   IDate ( unsigned long julianDayNumber );
  118.  
  119.   IDate ( const _CDATE &cDate );
  120.  
  121. /*-------------------------------- Accessors -----------------------------------
  122. | These functions provide access to various portions of the                    |
  123. | representation of the IDate object:                                          |
  124. |                                                                              |
  125. |   asString    -  Returns the IDate as a string.  The default is formatted    |
  126. |                  per the system (mm-dd-yy).  An alternate version permits    |
  127. |                  the use of any strftime conversion specifiers (for          |
  128. |                  example, "%x" yields a string such as "Apr 10 1959" ).      |
  129. |   asCDATE     -  Returns a container CDATE structure for the date.           |
  130. |   dayOfWeek   -  Returns the index of the receiver's day of the week:        |
  131. |                  Monday through Sunday.                                      |
  132. |   dayName     -  Returns the name of the receiver's day of the week.         |
  133. |   dayOfMonth  -  Returns the day in the receiver's month as an integer       |
  134. |                  from 1 to 31.                                               |
  135. |   dayOfYear   -  Returns the day in the receiver's year as an integer from   |
  136. |                  1 to 366.                                                   |
  137. |   julianDate  -  Returns the Julian day number of the receiver IDate.        |
  138. |   monthOfYear -  Returns the index of the receiver's month of the year:      |
  139. |                  January through December.                                   |
  140. |   monthName   -  Returns the name of the receiver's month.                   |
  141. |   year        -  Returns the receiver's year.  The form returned is yyyy.    |
  142. |   today       -  Static function that returns the current date.              |
  143. ------------------------------------------------------------------------------*/
  144. IString
  145.   asString  ( YearFormat  yearFmt = yy ) const,
  146.   asString  ( const char *fmt ) const,
  147.   dayName   ( ) const,
  148.   monthName ( ) const;
  149.  
  150. _CDATE
  151.   asCDATE   ( ) const;
  152.  
  153. DayOfWeek
  154.   dayOfWeek ( ) const;
  155.  
  156. int
  157.   dayOfMonth  ( ) const,
  158.   dayOfYear   ( ) const;
  159.  
  160. unsigned long
  161.   julianDate  ( ) const;
  162.  
  163. Month
  164.   monthOfYear ( ) const;
  165.  
  166. int
  167.   year        ( ) const;
  168.  
  169. static IDate
  170.   today ( );
  171.  
  172. /*-------------------------------- Stream I/O ----------------------------------
  173. |                                                                              |
  174. | operator << - This operator can be used to put an IDate representation to    |
  175. |               an output stream.  It puts the default asString                |
  176. |               representation.                                                |
  177. ------------------------------------------------------------------------------*/
  178. friend ostream
  179.  &operator << ( ostream     &aStream,
  180.                 const IDate &aDate );
  181.  
  182. /*-------------------------- Comparison Operators -------------------------------
  183. | Two IDates can be compared using the full complement of comparison            |
  184. | operators and applying the natural meaning.                                   |
  185. |                                                                               |
  186. |   operator ==  - Returns true if the IDate objects represent the same date.   |
  187. |   operator !=  - Returns true if the IDate objects represent different        |
  188. |                  dates.                                                       |
  189. |   operator <   - Returns true if the left-hand operand represents a date      |
  190. |                  prior to the date represented by the right-hand operand.     |
  191. |   operator <=  - Returns true if the left-hand operand represents a date      |
  192. |                  prior to or identical to the date represented by the         |
  193. |                  right-hand operand.                                          |
  194. |   operator >   - Returns true if the left-hand operand represents a date      |
  195. |                  subsequent to the date represented by the right-hand         |
  196. |                  operand.                                                     |
  197. |   operator >=  - Returns true if the left-hand operand represents a date      |
  198. |                  subsequent to or identical to the date represented by the    |
  199. |                  right-hand operand.                                          |
  200. -------------------------------------------------------------------------------*/
  201. Boolean
  202.   operator == ( const IDate &aDate ) const,
  203.   operator != ( const IDate &aDate ) const,
  204.   operator <  ( const IDate &aDate ) const,
  205.   operator <= ( const IDate &aDate ) const,
  206.   operator >  ( const IDate &aDate ) const,
  207.   operator >= ( const IDate &aDate ) const;
  208.  
  209. /*------------------------- Manipulation Operators --------------------------------
  210. | Manipulation operators:                                                         |
  211. |                                                                                 |
  212. |    operator +  - Adds an integral number of days to the left-hand operand,      |
  213. |                  yielding a new IDate.                                          |
  214. |    operator -  - Subtracts an integral number of days from the left-hand        |
  215. |                  operand, yeilding a new IDate.  If the right-hand operand      |
  216. |                  is also an IDate, then the operator yields the number of       |
  217. |                  days between the dates.                                        |
  218. |    operator += - Adds an integral number of days to the left-hand operand,      |
  219. |                  assigning the result to that operand.                          |
  220. |    operator -= - Subtracts an integral number of days from the right-hand       |
  221. |                  operand, assigning the result to that operand.                 |
  222. ---------------------------------------------------------------------------------*/
  223. IDate
  224.   operator +  ( int numDays ) const,
  225.   operator -  ( int numDays ) const;
  226.  
  227. IDate
  228.  &operator += ( int numDays ),
  229.  &operator -= ( int numDays );
  230.  
  231. long
  232.   operator -  ( const IDate &aDate ) const;
  233.  
  234. /*--------------------------------- Statics ------------------------------------
  235. | These functions are static.                                                  |
  236. | They provide general IDate utilities independent of specific IDates.         |
  237. |                                                                              |
  238. |   dayName     -  Returns the name of the week day corresponding to the       |
  239. |                  argument DayOfWeek index.                                   |
  240. |   monthName   -  Returns the name of the month with the argument             |
  241. |                  index.                                                      |
  242. |   daysInMonth -  Returns the number of days in a given month (in a given     |
  243. |                  year).  The form of aYear is yyyy.                          |
  244. |   daysInYear  -  Returns the number of days in a given year.  The form of    |
  245. |                  aYear is yyyy.                                              |
  246. |   isLeapYear  -  Returns true if the argument year is a leap year;           |
  247. |                  otherwise, it returns false.  The form of aYear is yyyy.    |
  248. |   isValid     -  Indicates whether the argument date (given as either        |
  249. |                  year/month/day or year/day) is valid.  For example,         |
  250. |                  February 29, 1990 is not a valid date because February had  |
  251. |                  only 28 days in 1990.  The form of aYear is yyyy.           |
  252. ------------------------------------------------------------------------------*/
  253. static IString
  254.   dayName   ( DayOfWeek aDay   ),
  255.   monthName ( Month     aMonth );
  256.  
  257. static int
  258.   daysInMonth ( Month aMonth,
  259.                 int   aYear ),
  260.   daysInYear  ( int   aYear );
  261.  
  262. static Boolean
  263.   isLeapYear ( int   aYear ),
  264.   isValid    ( Month aMonth,
  265.                int   aDay,
  266.                int   aYear ),
  267.   isValid    ( int   aDay,
  268.                Month aMonth,
  269.                int   aYear ),
  270.   isValid    ( int   aYear,
  271.                int   aDay );
  272.  
  273. protected:
  274. /*----------------------------- Implementation ---------------------------------
  275. | This function is used to implement class IDate:                              |
  276. |   initialize - Calculates the Julian day number.  The form of aYear is       |
  277. |                yyyy.  It returns a reference to the receiver, initialized    |
  278. |                to the given date.                                            |
  279. ------------------------------------------------------------------------------*/
  280. IDate
  281.  &initialize ( Month aMonth,
  282.                int   aDay,
  283.                int   aYear );
  284.  
  285. private:
  286. /*--------------------------------- Private ----------------------------------*/
  287. unsigned long
  288.   julian;
  289. }; // IDate
  290.  
  291. /*----------------------------------------------------------------------------*/
  292. /* Resume compiler default packing.                                           */
  293. /*----------------------------------------------------------------------------*/
  294. #pragma pack()
  295.  
  296. #ifndef I_NO_INLINES
  297.   #include <idate.inl>
  298. #endif
  299.  
  300. #endif /* _IDATE_ */
  301.