home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmdate.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  6.1 KB  |  133 lines

  1. // CmDate.h
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Date class definition.
  7. // -----------------------------------------------------------------
  8.  
  9. #ifndef _CMDATE_H
  10. #define _CMDATE_H
  11.  
  12. #include <cm/include/cmobject.h>
  13.  
  14. class CmDate : public CmObject {                  // Date class definition.
  15. public:
  16.   CmDate();                                       // Default date constructor.
  17.   CmDate(unsigned, unsigned);                     // Construct from d, y.
  18.   CmDate(unsigned, const char*, unsigned);        // Construct from d, m, y.
  19.   CmDate(unsigned, unsigned, unsigned);           // Construct from d, m, y.
  20.   CmDate(unsigned long j) : _julnum(j) {}         // Construct from jul num.
  21.  
  22.   unsigned    day            () const;            // Get day of year number.
  23.   unsigned    dayOfMonth     () const;            // Get day of month number.
  24.   unsigned    firstDayOfMonth() const;            // Get day num first of month.
  25.   unsigned    firstDayOfMonth(unsigned) const;    // Get day num first of month.
  26.   unsigned    month          () const;            // Get date's month number.
  27.   const char* nameOfDay      () const;            // Get date's day name.
  28.   const char* nameOfMonth    () const;            // Get date's month name.
  29.   unsigned    weekDay        () const;            // Get date's weekday number.
  30.   unsigned    year           () const;            // Get date's year number.
  31.   Bool        leap           () const;            // Check if leap year.
  32.   CmDate      previous       (const char*) const; // Get previous named day.
  33.   CmDate      previous       (unsigned) const;    // Get previous numbered day.
  34.   void        makeCurrent    ();                  // Make this date current.
  35.  
  36.   Bool     isEqual(CmObject*) const;              // Compare dates.
  37.   int      compare(CmObject*) const;              // Compare dates.
  38.   unsigned hash   (unsigned)  const;              // Hash date.
  39.   void     printOn(ostream&)  const;              // Print date to stream.
  40.   Bool     write  (CmReserveFile&) const;         // Write date to file.
  41.   Bool     read   (CmReserveFile&);               // Read date from file.
  42.  
  43.   CMOBJECT_DEFINE(CmDate, CmObject)               // Define object funcs.
  44.  
  45.   unsigned long julNum() const;                   // Return julian number.
  46.  
  47.   Bool operator< (const CmDate&) const;           // Check if date <  this.
  48.   Bool operator<=(const CmDate&) const;           // Check if date <= this.
  49.   Bool operator> (const CmDate&) const;           // Check if date >  this.
  50.   Bool operator>=(const CmDate&) const;           // Check if date >= this.
  51.   Bool operator==(const CmDate&) const;           // Check if date == this.
  52.   Bool operator!=(const CmDate&) const;           // Check if date != this.
  53.  
  54.   CmDate operator+(const CmDate&) const;          // Add dates.
  55.   CmDate operator+(int)           const;          // Add julnum to date.
  56.   CmDate operator-(const CmDate&) const;          // Subtract dates.
  57.   CmDate operator-(int)           const;          // Sub julnum from date.
  58.  
  59.   static unsigned long julDay   (unsigned, unsigned, unsigned); // Get julnum.
  60.   static unsigned dayOfWeek     (const char*);    // Return day of week num.
  61.   static Bool     dayWithinMonth(unsigned, unsigned, unsigned); // Get day num.
  62.   static Bool     leapYear      (unsigned);       // Check if leap year.
  63.   static unsigned indexOfMonth  (const char*);    // Get month number.
  64.  
  65.   static const char* dayName  (unsigned);         // Get day name from number.
  66.   static const char* monthName(unsigned);         // Get month name from num.
  67.  
  68.   static void  displayStyle(int);                 // Set display style for all.
  69.   static int   displayStyle();                    // Get display style.
  70.  
  71.   enum display_style { NORMAL=0, NUMBERS=1 };     // Display styles.
  72.  
  73. private:
  74.   void mdy(unsigned&, unsigned&, unsigned&) const; // Get date from julnum.
  75.  
  76.   unsigned long _julnum;                          // Julian day number.
  77.   static   int  _displayStyle;                    // Date display style.
  78. };
  79.  
  80. // "julNum" returns the julian day number.
  81. inline unsigned long CmDate::julNum() const
  82. { return _julnum; }
  83.  
  84. // "<" checks if the specified date is less than this.
  85. inline Bool CmDate::operator<(const CmDate& D) const
  86. { return (_julnum < D._julnum); }
  87.  
  88. // "<=" checks if the specified date is less than or equal to this.
  89. inline Bool CmDate::operator<=(const CmDate& D) const
  90. { return (_julnum <= D._julnum); }
  91.  
  92. // ">" checks if the specified date is greater than this.
  93. inline Bool CmDate::operator>(const CmDate& D) const
  94. { return (_julnum > D._julnum); }
  95.  
  96. // ">=" checks if the specified date is greater than or equal to this.
  97. inline Bool CmDate::operator>=(const CmDate& D) const
  98. { return (_julnum >= D._julnum); }
  99.  
  100. // "==" checks if the specified date is equal to this.
  101. inline Bool CmDate::operator==(const CmDate& D) const
  102. { return (_julnum == D._julnum); }
  103.  
  104. // "!=" checks if the specified date is not equal to this.
  105. inline Bool CmDate::operator!=(const CmDate& D) const
  106. { return (_julnum != D._julnum); }
  107.  
  108. // "+" adds two dates returning the resulting date.
  109. inline CmDate CmDate::operator+(const CmDate& D) const
  110. { return CmDate(_julnum + D._julnum); }
  111.  
  112. // "+" adds a julian day number to this date returning the new date.
  113. inline CmDate CmDate::operator+(int d) const
  114. { return CmDate(_julnum + d); }
  115.  
  116. // "-" subtracts the input date from this returning the resulting date.
  117. inline CmDate CmDate::operator-(const CmDate& D) const
  118. { return CmDate(_julnum - D._julnum); }
  119.  
  120. // "-" subtracts a julian day number from this returning the new date.
  121. inline CmDate CmDate::operator-(int d) const
  122. { return CmDate(_julnum - d); }
  123.  
  124. // "displayStyle" sets a new display style for all dates.
  125. inline void CmDate::displayStyle(int ds)
  126. { CmDate::_displayStyle = ds; }
  127.  
  128. // "displayStyle" returns the current display style.
  129. inline int CmDate::displayStyle()
  130. { return CmDate::_displayStyle; }
  131.  
  132. #endif
  133.