home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CLASSINC.PAK / TIME.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.5 KB  |  226 lines

  1. //----------------------------------------------------------------------------
  2. // Borland Class Library
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.6  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined(CLASSLIB_TIME_H)
  9. #define CLASSLIB_TIME_H
  10.  
  11. #if !defined(CLASSLIB_DEFS_H)
  12. # include <classlib/defs.h>
  13. #endif
  14. #if !defined(CLASSLIB_DATE_H)
  15. # include <classlib/date.h>
  16. #endif
  17. #if !defined(__TIME_H)
  18. # include <time.h>
  19. #endif
  20.  
  21. #if defined( BI_CLASSLIB_NO_po )
  22. # pragma option -po-
  23. #endif
  24.  
  25. class _EXPCLASS string;
  26. class _EXPCLASS istream;
  27. class _EXPCLASS ostream;
  28.  
  29. #if defined(BI_NAMESPACE)
  30. namespace ClassLib {
  31. #endif
  32.  
  33. class _BIDSCLASS ipstream;
  34. class _BIDSCLASS opstream;
  35.  
  36. typedef unsigned HourTy;
  37. typedef unsigned MinuteTy;
  38. typedef unsigned SecondTy;
  39. typedef unsigned long ClockTy;
  40.  
  41. static const unsigned long secFrom_Jan_1_1901_to_Jan_1_1970 = 2177452800L;
  42.  
  43. class _BIDSCLASS TTime
  44. {
  45.  
  46. public:
  47.  
  48.     friend TDate::TDate( const TTime _BIDSFAR & );
  49.  
  50.     TTime();                  // Current time
  51.     TTime( ClockTy s );       // Seconds since Jan 1, 1901.
  52.     TTime( HourTy h, MinuteTy m, SecondTy s = 0 );
  53.                                 // Specified time and today's date
  54.     TTime( const TDate _BIDSFAR &, HourTy h=0, MinuteTy m=0, SecondTy s=0 );
  55.                                 // Given date and time
  56.  
  57.     string AsString() const;
  58.     int CompareTo( const TTime _BIDSFAR & ) const;
  59.     unsigned Hash() const;
  60.     HourTy Hour() const;        // hour: local time
  61.     HourTy HourGMT() const;     // hour: GMT
  62.     int IsDST() const;
  63.     int IsValid() const;
  64.     TTime Max( const TTime _BIDSFAR & t ) const;
  65.     TTime Min( const TTime _BIDSFAR & t ) const;
  66.     MinuteTy Minute() const;    // minute: local time
  67.     MinuteTy MinuteGMT() const; // minute: GMT
  68.     SecondTy Second() const;    // second: local time or GMT
  69.     ClockTy Seconds() const;
  70.  
  71.     // Write times:
  72.     friend ostream _BIDSFAR & _BIDSFUNC operator << ( ostream _BIDSFAR &, const TTime _BIDSFAR & );
  73.  
  74.     // Read or write times on persistent streams
  75.     friend opstream _BIDSFAR & _BIDSFUNC operator << ( opstream _BIDSFAR & s, const TTime _BIDSFAR & d );
  76.     friend ipstream _BIDSFAR & _BIDSFUNC operator >> ( ipstream _BIDSFAR & s, TTime _BIDSFAR & d );
  77.  
  78.     // Boolean operators.
  79.     int operator <  ( const TTime _BIDSFAR & t ) const;
  80.     int operator <= ( const TTime _BIDSFAR & t ) const;
  81.     int operator >  ( const TTime _BIDSFAR & t ) const;
  82.     int operator >= ( const TTime _BIDSFAR & t ) const;
  83.     int operator == ( const TTime _BIDSFAR & t ) const;
  84.     int operator != ( const TTime _BIDSFAR & t ) const;
  85.     int Between( const TTime _BIDSFAR & a, const TTime _BIDSFAR & b ) const;
  86.  
  87.     // Add or subtract seconds.
  88.     friend TTime _BIDSFUNC operator + ( const TTime _BIDSFAR & t, long s );
  89.     friend TTime _BIDSFUNC operator + ( long s, const TTime _BIDSFAR & t );
  90.     friend TTime operator - ( const TTime _BIDSFAR & t, long s );
  91.     friend TTime operator - ( long s, const TTime _BIDSFAR & t );
  92.     void operator++();
  93.     void operator--();
  94.     void operator+=(long s);
  95.     void operator-=(long s);
  96.  
  97.     // Static member functions:
  98.     static TTime BeginDST( unsigned year ); // Start of DST for given year.
  99.     static TTime EndDST( unsigned year );   // End of DST for given year.
  100.     static int PrintDate( int );    // Whether to include date when printing time
  101.  
  102. protected:
  103.  
  104.     static int AssertDate( const TDate _BIDSFAR & );
  105.     static const TDate RefDate;
  106.     static const TDate MaxDate;
  107.  
  108. private:
  109.  
  110.     ClockTy Sec;        // Seconds since 1/1/1901.
  111.     static int PrintDateFlag;  // True to print date along with time.
  112.  
  113.     ClockTy LocalSecs() const;
  114.     static TTime BuildLocal( const TDate _BIDSFAR &, HourTy );
  115.  
  116. };
  117.  
  118. #if defined( BI_OLDNAMES )
  119. #define BI_Time TTime
  120. #endif
  121.  
  122. inline TTime::TTime( ClockTy s )
  123. {
  124.     Sec = s;
  125. }
  126.  
  127. inline int TTime::IsValid() const
  128. {
  129.     return Sec > 0;
  130. }
  131.  
  132. inline ClockTy TTime::Seconds() const
  133. {
  134.     return Sec;
  135. }
  136.  
  137. inline int TTime::operator <  ( const TTime& t ) const
  138. {
  139.     return Sec < t.Sec;
  140. }
  141.  
  142. inline int TTime::operator <= ( const TTime& t ) const
  143. {
  144.     return Sec <= t.Sec;
  145. }
  146.  
  147. inline int TTime::operator >  ( const TTime& t ) const
  148. {
  149.     return Sec > t.Sec;
  150. }
  151.  
  152. inline int TTime::operator >= ( const TTime& t ) const
  153. {
  154.     return Sec >= t.Sec;
  155. }
  156.  
  157. inline int TTime::operator == ( const TTime& t ) const
  158. {
  159.     return Sec == t.Sec;
  160. }
  161.  
  162. inline int TTime::operator != ( const TTime& t ) const
  163. {
  164.     return Sec != t.Sec;
  165. }
  166.  
  167. inline int TTime::Between( const TTime& a, const TTime& b ) const
  168. {
  169.     return *this >= a && *this <= b;
  170. }
  171.  
  172. inline TTime operator + ( const TTime& t, long s )
  173. {
  174.     return TTime(t.Sec+s);
  175. }
  176.  
  177. inline TTime operator + ( long s, const TTime& t )
  178. {
  179.     return TTime(t.Sec+s);
  180. }
  181.  
  182. inline TTime operator - ( const TTime& t, long s )
  183. {
  184.     return TTime(t.Sec-s);
  185. }
  186.  
  187. inline TTime operator - ( long s, const TTime& t )
  188. {
  189.     return TTime(t.Sec-s);
  190. }
  191.  
  192. inline void TTime::operator++()
  193. {
  194.     Sec += 1;
  195. }
  196.  
  197. inline void TTime::operator--()
  198. {
  199.     Sec -= 1;
  200. }
  201.  
  202. inline void TTime::operator+=(long s)
  203. {
  204.     Sec += s;
  205. }
  206.  
  207. inline void TTime::operator-=(long s)
  208. {
  209.     Sec -= s;
  210. }
  211.  
  212. inline HashValue( TTime _BIDSFAR & t )
  213. {
  214.     return t.Hash();
  215. }
  216.  
  217. #if defined(BI_NAMESPACE)
  218. }     // namespace ClassLib
  219. #endif
  220.  
  221. #if defined( BI_CLASSLIB_NO_po )
  222. # pragma option -po.
  223. #endif
  224.  
  225. #endif  // CLASSLIB_TIME_H
  226.