home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classinc.pak / DATE.H < prev    next >
C/C++ Source or Header  |  1997-07-23  |  9KB  |  295 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  DATE.H                                                                */
  4. /*                                                                        */
  5. /*  Copyright (c) 1993, 1994 Borland International                        */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( CLASSLIB__DATE_H )
  11. #define CLASSLIB__DATE_H
  12.  
  13. #if !defined(___DEFS_H)
  14. #include <_defs.h>
  15. #endif
  16.  
  17. #if !defined( CLASSLIB_DEFS_H )
  18. #include <classlib/defs.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. class _BIDSCLASS TTime;
  29. class _BIDSCLASS ipstream;
  30. class _BIDSCLASS opstream;
  31.  
  32. typedef unsigned DayTy;
  33. typedef unsigned MonthTy;
  34. typedef unsigned YearTy;
  35. typedef unsigned long JulTy;
  36.  
  37. static const JulTy jul1901 = 2415386L;  // Julian day for 1/1/1901
  38.  
  39. /*------------------------------------------------------------------------*/
  40. /*                                                                        */
  41. /*  class TDate                                                           */
  42. /*                                                                        */
  43. /*------------------------------------------------------------------------*/
  44.  
  45. class _BIDSCLASS TDate
  46. {
  47.  
  48. public:
  49.  
  50.     enum HowToPrint
  51.         {
  52.         Normal,
  53.         Terse,
  54.         Numbers,
  55.         EuropeanNumbers,
  56.         European
  57.         };
  58.  
  59.     // Construct a TDate with the current date
  60.  
  61.     TDate();
  62.  
  63.     // Construct a TDate with a given day of the year and a given year.
  64.     // The base date for this computation is Dec. 31 of the previous year.
  65.     // If year == 0, Construct a TDate with Jan. 1, 1901 as the "day zero".
  66.     // i.e., TDate(-1,0) = Dec. 31, 1900 and TDate(1,0) = Jan. 2, 1901.
  67.  
  68.     TDate( DayTy day, YearTy year );
  69.  
  70.     //  Construct a TDate for the given day, month, and year.
  71.     TDate( DayTy, const char _BIDSFAR * month, YearTy );
  72.     TDate( DayTy, MonthTy, YearTy );
  73.     TDate( istream _BIDSFAR & s );      // Read date from stream.
  74.     TDate( const TTime _BIDSFAR & );    // Construct a TDate from a TTime
  75.  
  76.     string AsString() const;
  77.     int Between( const TDate _BIDSFAR & d1, const TDate _BIDSFAR & d2 ) const;
  78.     int CompareTo( const TDate _BIDSFAR & ) const;
  79.     DayTy Day() const;            // 1-365
  80.     DayTy DayOfMonth() const;     // 1-31
  81.     DayTy FirstDayOfMonth() const;
  82.     DayTy FirstDayOfMonth( MonthTy ) const;
  83.     unsigned Hash() const;
  84.     int IsValid() const;
  85.     int Leap()  const;                             // leap year?
  86.     TDate Max( const TDate _BIDSFAR & dt ) const;
  87.     TDate Min( const TDate _BIDSFAR & dt ) const;
  88.     MonthTy Month() const;
  89.     const char _BIDSFAR *NameOfDay() const;
  90.     const char _BIDSFAR *NameOfMonth() const;
  91.     TDate Previous( const char _BIDSFAR *dayName ) const;  // Return date of previous dayName
  92.     TDate Previous( DayTy ) const;                // Same as above, but use day of week
  93.     DayTy WeekDay() const;
  94.     YearTy Year() const;
  95.  
  96.     // Date comparisons:
  97.     int operator <  ( const TDate _BIDSFAR & date ) const;
  98.     int operator <= ( const TDate _BIDSFAR & date ) const;
  99.     int operator >  ( const TDate _BIDSFAR & date ) const;
  100.     int operator >= ( const TDate _BIDSFAR & date ) const;
  101.     int operator == ( const TDate _BIDSFAR & date ) const;
  102.     int operator != ( const TDate _BIDSFAR & date ) const;
  103.  
  104.     // Arithmetic operators:
  105.     JulTy operator - ( const TDate _BIDSFAR & dt ) const;
  106.     friend TDate _BIDSFUNC operator + ( const TDate _BIDSFAR & dt, int dd );
  107.     friend TDate _BIDSFUNC operator + ( int dd, const TDate _BIDSFAR & dt );
  108.     friend TDate _BIDSFUNC operator - ( const TDate _BIDSFAR & dt, int dd );
  109.     void operator ++ ();
  110.     void operator -- ();
  111.     void operator += ( int dd );
  112.     void operator -= ( int dd );
  113.  
  114.     // Read or write dates:
  115.     friend ostream _BIDSFAR & _BIDSFUNC operator << ( ostream _BIDSFAR & s, const TDate _BIDSFAR & d );
  116.     friend istream _BIDSFAR & _BIDSFUNC operator >> ( istream _BIDSFAR & s, TDate _BIDSFAR & d );
  117.  
  118.     // Read or write dates on persistent streams
  119.     friend opstream _BIDSFAR & _BIDSFUNC operator << ( opstream _BIDSFAR & s, const TDate _BIDSFAR & d );
  120.     friend ipstream _BIDSFAR & _BIDSFUNC operator >> ( ipstream _BIDSFAR & s, TDate _BIDSFAR & d );
  121.  
  122.     // Static member functions:
  123.     static const char _BIDSFAR *DayName( DayTy weekDayNumber );
  124.     static DayTy DayOfWeek( const char _BIDSFAR * dayName );
  125.     static int DayWithinMonth( MonthTy, DayTy, YearTy );
  126.     static DayTy DaysInYear( YearTy );
  127.     static MonthTy IndexOfMonth( const char * _BIDSFAR monthName );
  128.     static JulTy Jday( MonthTy, DayTy, YearTy );
  129.     static int LeapYear( YearTy year );
  130.     static const char _BIDSFAR *MonthName( MonthTy monthNumber );
  131.     static HowToPrint SetPrintOption( HowToPrint h );
  132.  
  133. protected:
  134.  
  135.     static int AssertWeekDayNumber( DayTy d );
  136.     static int AssertIndexOfMonth( MonthTy m );
  137.  
  138. private:
  139.  
  140.     JulTy Julnum;                   // Julian Day Number (Not same as Julian date.)
  141.     static HowToPrint PrintOption;  // Printing with different formats
  142.  
  143.     void ParseFrom( istream _BIDSFAR & );     // Reading dates
  144.     void _BIDSNEARFUNC Mdy( MonthTy _BIDSFAR &, DayTy _BIDSFAR &, YearTy _BIDSFAR & ) const;
  145.     TDate( JulTy j );
  146.  
  147. };
  148.  
  149. #if defined( BI_OLDNAMES )
  150. #define BI_Date TDate
  151. #endif
  152.  
  153. /*------------------------------------------------------------------------*/
  154. /*                                                                        */
  155. /*  TDate inline functions                                                */
  156. /*                                                                        */
  157. /*------------------------------------------------------------------------*/
  158.  
  159. inline TDate::TDate( istream& s )
  160. {
  161.     ParseFrom(s);
  162. }
  163.  
  164. inline int TDate::Between( const TDate& d1, const TDate& d2 ) const
  165. {
  166.     return Julnum >= d1.Julnum && Julnum <= d2.Julnum;
  167. }
  168.  
  169. inline DayTy TDate::FirstDayOfMonth() const
  170. {
  171.     return FirstDayOfMonth(Month());
  172. }
  173.  
  174. inline int TDate::IsValid() const
  175. {
  176.     return Julnum>0;
  177. }
  178.  
  179. inline int TDate::Leap() const
  180. {
  181.     return LeapYear(Year());
  182. }
  183.  
  184. inline const char *TDate::NameOfDay() const
  185. {
  186.     return DayName(WeekDay());
  187. }
  188.  
  189. inline const char *TDate::NameOfMonth() const
  190. {
  191.     return MonthName(Month());
  192. }
  193.  
  194. inline int TDate::operator < ( const TDate& date ) const
  195. {
  196.     return Julnum < date.Julnum;
  197. }
  198.  
  199. inline int TDate::operator <= ( const TDate& date ) const
  200. {
  201.     return Julnum <= date.Julnum;
  202. }
  203.  
  204. inline int TDate::operator > ( const TDate& date ) const
  205. {
  206.     return Julnum > date.Julnum;
  207. }
  208.  
  209. inline int TDate::operator >= ( const TDate& date ) const
  210. {
  211.     return Julnum >= date.Julnum;
  212. }
  213.  
  214. inline int TDate::operator == ( const TDate& date ) const
  215. {
  216.     return Julnum == date.Julnum;
  217. }
  218.  
  219. inline int TDate::operator != ( const TDate& date ) const
  220. {
  221.     return Julnum != date.Julnum;
  222. }
  223.  
  224. inline JulTy TDate::operator - ( const TDate& dt ) const
  225. {
  226.     return Julnum - dt.Julnum;
  227. }
  228.  
  229. inline TDate _BIDSFUNC operator + ( const TDate& dt, int dd )
  230. {
  231.     return TDate(dt.Julnum + dd);
  232. }
  233.  
  234. inline TDate _BIDSFUNC operator + ( int dd, const TDate& dt )
  235. {
  236.     return TDate(dt.Julnum + dd);
  237. }
  238.  
  239. inline TDate _BIDSFUNC operator - ( const TDate& dt, int dd )
  240. {
  241.     return TDate(dt.Julnum - dd);
  242. }
  243.  
  244. inline void TDate::operator ++ ()
  245. {
  246.     Julnum += 1;
  247. }
  248.  
  249. inline void TDate::operator -- ()
  250. {
  251.     Julnum -= 1;
  252. }
  253.  
  254. inline void TDate::operator += ( int dd )
  255. {
  256.     Julnum += dd;
  257. }
  258.  
  259. inline void TDate::operator -= ( int dd )
  260. {
  261.     Julnum -= dd;
  262. }
  263.  
  264. inline istream& _BIDSFUNC operator >> ( istream& s, TDate& d )
  265. {
  266.     d.ParseFrom(s);
  267.     return s;
  268. }
  269.  
  270. inline int TDate::AssertWeekDayNumber( DayTy d )
  271. {
  272.     return d>=1 && d<=7;
  273. }
  274.  
  275. inline int TDate::AssertIndexOfMonth( MonthTy m )
  276. {
  277.     return m>=1 && m<=12;
  278. }
  279.  
  280. inline TDate::TDate( JulTy j )
  281. {
  282.     Julnum = j;
  283. }
  284.  
  285. inline HashValue( TDate _BIDSFAR & d )
  286. {
  287.     return d.Hash();
  288. }
  289.  
  290. #if defined( BI_CLASSLIB_NO_po )
  291. #pragma option -po.
  292. #endif
  293.  
  294. #endif  // CLASSLIB_DATE_H
  295.