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

  1. #ifndef __RWTIME_H__
  2. #define __RWTIME_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * Declarations for class RWTime.
  7.  *
  8.  * $Header:   E:/vcs/rw/rwtime.h_v   1.6   04 Mar 1992 10:21:36   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.  * $Log:   E:/vcs/rw/rwtime.h_v  $
  23.  * 
  24.  *    Rev 1.6   04 Mar 1992 10:21:36   KEFFER
  25.  * Instance manager used in multi-thread situation.
  26.  * 
  27.  *    Rev 1.4   13 Nov 1991 11:10:26   keffer
  28.  * Static variables maintained by a manager for multi-thread version
  29.  * 
  30.  *    Rev 1.3   29 Oct 1991 13:57:04   keffer
  31.  * Added 24hour clock print option.
  32.  * 
  33.  *    Rev 1.2   28 Oct 1991 09:08:22   keffer
  34.  * Changed inclusions to <rw/xxx.h>
  35.  * 
  36.  *    Rev 1.1   22 Aug 1991 10:20:16   keffer
  37.  * Simplified construction of times by defining m.f. buildFrom()
  38.  * 
  39.  *    Rev 1.0   28 Jul 1991 08:16:44   keffer
  40.  * Tools.h++ V4.0.5 PVCS baseline version
  41.  *
  42.  */
  43.  
  44. #include "rw/tooldefs.h"
  45. #include "rw/rwdate.h"
  46. #include "rw/procinit.h"    /* Uses instance data */
  47. STARTWRAP
  48. #include <time.h>         /* System time management. */
  49. ENDWRAP
  50.  
  51. class RWExport RWCString;
  52.  
  53. typedef     unsigned         hourTy;
  54. typedef     unsigned         minuteTy;
  55. typedef     unsigned         secondTy;
  56. typedef     unsigned long          clockTy;
  57. static const     unsigned long         secFrom_Jan_1_1901_to_Jan_1_1970 = 2177452800L;
  58.  
  59. class RWExport RWTime {
  60. friend RWDate::RWDate(const RWTime&);
  61. private:
  62.   clockTy         sec;        // Seconds since 1/1/1901.
  63. #ifndef RW_MULTI_THREAD
  64.   /* If not compiling for an MT situation, then use static data--- */
  65.   static RWBoolean    printDateFlag;    // True to print date along with time.
  66.   static RWBoolean    milTimeFlag;    // True to use 24-hour clock
  67. #endif
  68. protected:
  69.   static RWBoolean     assertDate(const RWDate&);
  70.   void            buildFrom(const RWDate&, hourTy, minuteTy, secondTy);
  71.   static const RWDate    refDate;
  72.   static const RWDate    maxDate;
  73. private:  
  74.   clockTy        localSecs() const;
  75.   static RWTime        buildLocal(const RWDate&, hourTy);
  76. public:
  77.  
  78.   RWTime();                    // Current time
  79.   RWTime(clockTy s) {sec = s;}            // Seconds since Jan 1, 1901.
  80.   RWTime(hourTy h, minuteTy m, secondTy s = 0);    // Specified time and today's date
  81.   RWTime(const RWDate&, hourTy h=0, minuteTy m=0, secondTy s=0);    // Given date and time
  82.  
  83.   RWCString        asString() const;
  84.   unsigned        binaryStoreSize() const {return sizeof(clockTy);} 
  85.   int             compareTo(const RWTime*) const;
  86.   unsigned         hash()      const;
  87.   hourTy           hour()      const;    // hour: local time
  88.   hourTy           hourGMT() const;    // hour: GMT 
  89.   RWBoolean         isDST() const;
  90.   RWBoolean        isValid() const {return sec>0;}
  91.   RWTime         max(const RWTime& t) const;
  92.   RWTime         min(const RWTime& t) const;
  93.   minuteTy         minute()     const;    // minute: local time
  94.   minuteTy         minuteGMT()     const;    // minute: GMT 
  95.   void            restoreFrom(RWFile&);
  96.   void            restoreFrom(RWvistream&);
  97.   void            saveOn(RWFile&) const;
  98.   void            saveOn(RWvostream&) const;
  99.   secondTy         second()     const;    // second: local time or GMT 
  100.   clockTy          seconds()     const {return sec;}
  101.   friend ostream&    operator<<(ostream&, const RWTime&);
  102.  
  103.   // Boolean operators.
  104.   RWBoolean         operator<(const RWTime& t)  const    { return sec < t.sec; }
  105.   RWBoolean         operator<=(const RWTime& t) const    { return sec <= t.sec; }
  106.   RWBoolean         operator>(const RWTime& t)  const    { return sec > t.sec; }
  107.   RWBoolean         operator>=(const RWTime& t) const    { return sec >= t.sec; }
  108.   RWBoolean         operator==(const RWTime& t) const    { return sec == t.sec; }
  109.   RWBoolean         operator!=(const RWTime& t) const    { return sec != t.sec; }
  110.   RWBoolean         between(const RWTime& a, const RWTime& b) const
  111.                   { return *this >= a && *this <= b;}
  112.   
  113.   // Add or subtract seconds.
  114. friend RWTime         operator+(const RWTime& t, long s)
  115.               { return RWTime(t.sec+s); }
  116. friend RWTime         operator+(long s, const RWTime& t)
  117.               { return RWTime(t.sec+s); }
  118. friend RWTime         operator-(const RWTime& t, long s)
  119.               { return RWTime(t.sec-s); }
  120. friend RWTime         operator-(long s, const RWTime& t)
  121.               { return RWTime(t.sec-s); }
  122.   void             operator++()        { ++sec; }
  123.   void             operator--()        { --sec; }
  124. #ifdef CPPV21
  125.   void            operator++(int)        { sec++; }
  126.   void            operator--(int)        { sec--; }
  127. #endif
  128.   void             operator+=(long s)    { sec += s; }
  129.   void             operator-=(long s)    { sec -= s; }
  130.  
  131.   // Static member functions:
  132.   static RWTime     beginDST(unsigned year); // Start of DST for given year.
  133.   static RWTime     endDST(unsigned year);     // End of DST for given year.
  134.   static RWBoolean    printDate(RWBoolean);    // Whether to include date when printing time
  135.   static RWBoolean    milTime(RWBoolean);     // Whether to use 24-hour clk
  136. #ifdef RW_MULTI_THREAD
  137.   // Just declarations --- static data must be retrieved from the instance manager.
  138.   static RWBoolean    getPrintDateFlag();
  139.   static RWBoolean    getMilTimeFlag();
  140. #else
  141.   static RWBoolean    getPrintDateFlag()    {return printDateFlag;}
  142.   static RWBoolean    getMilTimeFlag()    {return milTimeFlag;}
  143. #endif
  144. };
  145.  
  146. inline RWTime::RWTime(const RWDate& d, hourTy h, minuteTy m, secondTy s)
  147. { buildFrom(d,h,m,s); }
  148.  
  149. pragma pop_align_members();
  150. #endif  /* __RWTIME_H__ */
  151.