home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 6.ddi / MWHC.006 / Y1 < prev    next >
Encoding:
Text File  |  1992-06-07  |  6.4 KB  |  175 lines

  1. #ifndef __RWDATE_H__
  2. #define __RWDATE_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * Declarations for RWDate Class
  7.  *
  8.  * $Header:   E:/vcs/rw/rwdate.h_v   1.7   17 Mar 1992 11:59:16   KEFFER  $
  9.  *
  10.  ****************************************************************************
  11.  *
  12.  * Rogue Wave Software, Inc.
  13.  * P.O. Box 2328
  14.  * Corvallis, OR 97339
  15.  * Voice: (503) 754-3010    FAX: (503) 757-6650
  16.  *
  17.  * Copyright (C) 1989, 1990, 1991. This software is subject to copyright 
  18.  * protection under the laws of the United States and other countries.
  19.  *
  20.  ***************************************************************************
  21.  *
  22.  * An RWDate can be tested for validity using the member function isValid().
  23.  *
  24.  ***************************************************************************
  25.  *
  26.  * $Log:   E:/vcs/rw/rwdate.h_v  $
  27.  * 
  28.  *    Rev 1.7   17 Mar 1992 11:59:16   KEFFER
  29.  * Pre- and post-increment and decrement operators now have a return type.
  30.  * 
  31.  *    Rev 1.6   04 Mar 1992 10:21:20   KEFFER
  32.  * Instance manager used in multi-thread situation.
  33.  * 
  34.  *    Rev 1.3   13 Nov 1991 11:10:24   keffer
  35.  * Static variables maintained by a manager for multi-thread version
  36.  * 
  37.  *    Rev 1.2   29 Oct 1991 13:57:32   keffer
  38.  * Added postfix ++ and --.
  39.  * 
  40.  *    Rev 1.1   28 Oct 1991 09:08:20   keffer
  41.  * Changed inclusions to <rw/xxx.h>
  42.  * 
  43.  */
  44.  
  45. #include "rw/tooldefs.h"
  46. #include "rw/procinit.h"    /* Uses instance manager */
  47.  
  48. class RWExport RWTime;
  49. class RWExport RWCString;
  50.  
  51. typedef unsigned    dayTy;
  52. typedef unsigned    monthTy;
  53. typedef unsigned    yearTy;
  54. typedef unsigned long    julTy;
  55. static const julTy jul1901 = 2415386L;    // Julian day for 1/1/1901
  56.  
  57. class RWExport RWDate {
  58. public:
  59.   enum            howToPrint {normal, terse, numbers, europeanNumbers, european};
  60. private:
  61.   julTy         julnum;            // Julian Day Number (Not same as Julian date.)  
  62. #ifndef RW_MULTI_THREAD
  63.   /* If not compiling for an MT situation, then use static data--- */
  64.   static howToPrint      printOption;        // Printing with different formats
  65. #endif
  66. private:
  67.   void             parseFrom(istream&);    // Reading dates
  68.   void near        mdy(monthTy&, dayTy&, yearTy&) const;
  69.   RWDate(julTy j)    {julnum = j; }
  70. protected:
  71.   static RWBoolean     assertWeekDayNumber(dayTy d)
  72.             {return d>=1 && d<=7;}
  73.   static RWBoolean     assertIndexOfMonth(monthTy m)
  74.             {return m>=1 && m<=12;}
  75. public:
  76.  
  77.   // Construct a RWDate with the current date
  78.   RWDate();
  79.  
  80.   /* Construct a RWDate with a given day of the year and a given year.
  81.      The base date for this computation is Dec. 31 of the previous year.  
  82.      If year == 0, Construct a RWDate with Jan. 1, 1901 as the "day zero".  
  83.         i.e., RWDate(-1) = Dec. 31, 1900 and RWDate(1) = Jan. 2, 1901. */
  84.   RWDate(dayTy day, yearTy year);
  85.  
  86.   //  Construct a RWDate for the given day, month, and year.
  87.   RWDate(dayTy, const char* month, yearTy);
  88.   RWDate(dayTy, monthTy, yearTy);
  89.   RWDate(istream& s)     {parseFrom(s);}  // Read date from stream.
  90.   RWDate(const RWTime&);              // Construct an RWDate from an RWTime
  91.  
  92.   RWCString        asString() const;
  93.   RWBoolean         between(const RWDate& d1, const RWDate& d2) const
  94.             { return julnum >= d1.julnum && julnum <= d2.julnum; }
  95.   int             compareTo(const RWDate*) const;
  96.   dayTy             day() const;        // 1-365
  97.   dayTy         dayOfMonth() const;    // 1-31
  98.   dayTy             firstDayOfMonth() const {return firstDayOfMonth(month());}
  99.   dayTy                firstDayOfMonth(monthTy) const;
  100.   unsigned        hash() const;
  101.   RWBoolean        isValid() const { return julnum>0; }
  102.   RWBoolean         leap()     const { return leapYear(year());}  // leap year?
  103.   RWDate        max(const RWDate& dt) const;
  104.   RWDate        min(const RWDate& dt) const;
  105.   monthTy            month() const;
  106.   const char*         nameOfDay()   const {return dayName(weekDay());}
  107.   const char*         nameOfMonth() const {return monthName(month());}
  108.   RWDate            previous(const char* dayName) const;    // Return date of previous dayName
  109.   RWDate            previous(dayTy) const;             // Same as above, but use day of week
  110.   dayTy             weekDay() const;
  111.   yearTy             year() const;
  112.  
  113.   // Date comparisons:
  114.   RWBoolean         operator<(const RWDate& date)      const
  115.               { return julnum < date.julnum; }
  116.   RWBoolean         operator<=(const RWDate& date)      const
  117.             { return julnum <= date.julnum; }
  118.   RWBoolean         operator>(const RWDate& date)      const
  119.             { return julnum > date.julnum; }
  120.   RWBoolean         operator>=(const RWDate& date)      const
  121.             { return julnum >= date.julnum; }
  122.   RWBoolean         operator==(const RWDate& date)      const
  123.             { return julnum == date.julnum; }
  124.   RWBoolean         operator!=(const RWDate& date)      const
  125.             { return julnum != date.julnum; }
  126.  
  127.   // Arithmetic operators:
  128.   julTy         operator-(const RWDate& dt) const
  129.             { return julnum - dt.julnum; }
  130.   friend RWDate     operator+(const RWDate& dt, int dd)
  131.             { return RWDate(dt.julnum + dd); }
  132.   friend RWDate     operator+(int dd, const RWDate& dt)
  133.             { return RWDate(dt.julnum + dd); }
  134.   friend RWDate        operator-(const RWDate& dt, int dd)
  135.             { return RWDate(dt.julnum - dd); }
  136.   RWDate        operator++()            { return RWDate(++julnum); }
  137.   RWDate         operator--()            { return RWDate(--julnum); }
  138. #ifdef CPPV21
  139.   RWDate        operator++(int)            { return RWDate(julnum++); }
  140.   RWDate        operator--(int)            { return RWDate(julnum--); }
  141. #endif
  142.   RWDate         operator+=(int dd)        { return RWDate(julnum += dd); }
  143.   RWDate         operator-=(int dd)        { return RWDate(julnum -= dd); }
  144.  
  145.   // Read or write dates:
  146.   friend         ostream& operator<<(ostream&, const RWDate&);
  147.   friend         istream& operator>>(istream& s, RWDate& d)
  148.                     {d.parseFrom(s); return s;}
  149.   unsigned        binaryStoreSize() const {return sizeof(julnum);}
  150.   void            restoreFrom(RWvistream& s);
  151.   void              restoreFrom(RWFile&);
  152.   void            saveOn(RWvostream& s) const;
  153.   void            saveOn(RWFile&) const;
  154.  
  155.   // Static member functions:
  156.   static  const char*     dayName(dayTy weekDayNumber);
  157.   static  dayTy     dayOfWeek(const char* dayName);
  158.   static  RWBoolean     dayWithinMonth(monthTy, dayTy, yearTy);
  159.   static  dayTy          daysInYear(yearTy);
  160.   static  monthTy     indexOfMonth(const char* monthName);
  161.   static  julTy        jday(monthTy, dayTy, yearTy);
  162.   static  RWBoolean    leapYear(yearTy year);
  163.   static  const char*     monthName(monthTy monthNumber);
  164.   static  howToPrint    setPrintOption(howToPrint h);
  165. #ifdef RW_MULTI_THREAD
  166.   // Just declarations --- static data must be retrieved from the instance manager.
  167.   static  howToPrint    getPrintOption();
  168. #else
  169.   static  howToPrint    getPrintOption()    {return printOption;}
  170. #endif
  171. };
  172.  
  173. pragma pop_align_members();
  174. #endif     /* __RWDATE_H__ */
  175.