home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / base / date.h < prev    next >
C/C++ Source or Header  |  1995-04-08  |  11KB  |  347 lines

  1.  
  2. #ifndef _date_h_
  3. #define _date_h_
  4.  
  5.  
  6.  
  7.  
  8.  
  9. /*
  10.  *
  11.  *          Copyright (C) 1994, M. A. Sridhar
  12.  *  
  13.  *
  14.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  15.  *     to copy, modify or distribute this software  as you see fit,
  16.  *     and to use  it  for  any  purpose, provided   this copyright
  17.  *     notice and the following   disclaimer are included  with all
  18.  *     copies.
  19.  *
  20.  *                        DISCLAIMER
  21.  *
  22.  *     The author makes no warranties, either expressed or implied,
  23.  *     with respect  to  this  software, its  quality, performance,
  24.  *     merchantability, or fitness for any particular purpose. This
  25.  *     software is distributed  AS IS.  The  user of this  software
  26.  *     assumes all risks  as to its quality  and performance. In no
  27.  *     event shall the author be liable for any direct, indirect or
  28.  *     consequential damages, even if the  author has been  advised
  29.  *     as to the possibility of such damages.
  30.  *
  31.  */
  32.  
  33.  
  34.  
  35. // This class represents a particular date. Only dates on or after
  36. // Sep 14, 1752 are permitted. The class provides for conversion to and from
  37. // a string representation in one of the following forms, encoded by
  38. // the {\tt DatePrintForm} enumeration, as illustrated below:
  39. // \par\medskip{\tt \begin{tabular}{ll}
  40. //            Date_American   &   October 17, 1992\\
  41. //            Date_Terse      &   17-Oct-1992\\
  42. //            Date_Numbers    &   10/17/92 \\
  43. //            Date_European   &   17 October 1992\\
  44. //            Date_EuroNumbers&   17/10/92\\
  45. // \end{tabular}
  46. // }\par
  47.  
  48.  
  49. #ifdef __GNUC__
  50. #pragma interface
  51. #endif
  52.  
  53.  
  54. #include "base/defs.h"
  55. #include "base/string.h"
  56.  
  57.  
  58. enum DatePrintForm { Date_American, Date_Terse, Date_Numbers,
  59.                      Date_European, Date_EuroNumbers };
  60.  
  61. class CL_EXPORT  CL_Date: public CL_Object {
  62.  
  63. public:
  64.  
  65.     enum MonthEnum { InvalidMonth = 0, January, February, March, April,
  66.                      May, June, July, August, September, October,
  67.                      November, December};
  68.  
  69.     enum WeekdayEnum {InvalidDay = 0, Sunday, Monday, Tuesday, Wednesday,
  70.                       Thursday, Friday, Saturday};
  71.     
  72.     //
  73.     // ------------- Constructors and destructors ----------------------
  74.     //
  75.  
  76.     CL_Date ();
  77.     // Construct an "empty" date object: one that represents Jan 1, 1901.
  78.  
  79.     CL_Date (short year, short month, short day);
  80.     // Construct the given date, assuming that the year is given as 1992,
  81.     // not 92, and the month is indexed from 1 (e.g. January is month 1).
  82.  
  83.     CL_Date (short year, const char* month, short day);
  84.     // Construct the given date, assuming the month specified as a
  85.     // string, e.g., "March". Assume case-sensitive comparison and
  86.     // completely-specified month names (e.g. "Oct" is not allowed).
  87.  
  88.     CL_Date (const char* s);
  89.     // Construct a date from the given string containing the
  90.     // representation, in one of the forms Date_American, Date_Terse,
  91.     // or Date_Numbers.
  92.     
  93.  
  94.     CL_Date (const CL_Date&);
  95.     // Copy constructor
  96.  
  97.     ~CL_Date ();
  98.     // Destructor
  99.  
  100.     //
  101.     // ---------------------- Access ----------------------------------
  102.     //
  103.  
  104.     bool IsLegal () const;
  105.     // Is this a legal date?
  106.     
  107.     short Year() const;
  108.     // Return our year.
  109.  
  110.     MonthEnum Month () const;
  111.     // Return our month.
  112.  
  113.     short Day () const;
  114.     // Return our day of month.
  115.  
  116.     WeekdayEnum DayOfWeek () const;
  117.     // Return our day of week. Assume that 1 is for Sunday and 7 for
  118.     // Saturday.
  119.  
  120.     short DaysInMonth () const;
  121.     // Return the number of days in our month.
  122.     
  123.     CL_String PrintString (DatePrintForm form = Date_American) const;
  124.     // Return a printable form of this object, according to the
  125.     // parameter.
  126.  
  127.  
  128.  
  129.     
  130.     bool IsLeapYear () const;
  131.     // Tell if ours is a leap year.
  132.  
  133.  
  134.     CL_Date NextWeekday (const char* weekday_name) const;
  135.     // Return the date of the next weekday given.
  136.  
  137.     CL_Date PreviousWeekday (const char* weekday_name) const;
  138.     // Return the date of the previous weekday given.
  139.     
  140.     CL_Date NextWeekday (WeekdayEnum weekday_num) const;
  141.     // Return the date of the next weekday given.
  142.  
  143.     CL_Date PreviousWeekday (WeekdayEnum weekday_num) const;
  144.     // Return the date of the previous weekday given.
  145.     
  146.  
  147.     //
  148.     // --------------------- Comparison ------------------------------
  149.     //
  150.  
  151.     bool operator<  (const CL_Date&) const;
  152.     
  153.     bool operator<= (const CL_Date&) const;
  154.     
  155.     bool operator>  (const CL_Date&) const;
  156.  
  157.     bool operator>= (const CL_Date&) const;
  158.  
  159.     bool operator== (const CL_Date&) const;
  160.  
  161.     bool operator!= (const CL_Date&) const;
  162.  
  163.     bool operator<  (const CL_Object& obj) const;
  164.  
  165.     bool operator<= (const CL_Object& obj) const;
  166.  
  167.     bool operator>  (const CL_Object& obj) const;
  168.  
  169.     bool operator>= (const CL_Object& obj) const;
  170.  
  171.     bool operator== (const CL_Object& obj) const;
  172.  
  173.     bool operator!= (const CL_Object& obj) const;
  174.  
  175.     short Compare (const CL_Date&) const;
  176.  
  177.     short Compare (const CL_Object&) const;
  178.  
  179.     bool IsBetween (const CL_Date& d1, const CL_Date& d2) const;
  180.     // Return TRUE if this date is between the two given dates.
  181.     
  182.     //
  183.     // -------------------------- Modification ----------------------
  184.     //
  185.  
  186.     // Assignment
  187.     
  188.     virtual void operator= (const CL_Date&);
  189.     // Assign the given date to this object.
  190.  
  191.     virtual void operator= (const CL_String&);
  192.     // Assign our value from a string. The string must be in one of
  193.     // the forms of Date_American, Date_Terse, or Date_Numbers, or
  194.     // else must be six digits (characters) of the form yymmdd or
  195.     // mmddyy. In the former three cases, the year can be either of
  196.     // the form 92 or 1992.
  197.  
  198.     //
  199.     // -------------------------- Date arithmetic -------------------
  200.     //
  201.  
  202.     
  203.     
  204.     virtual CL_Date  operator+  (short num_days) const;
  205.     // Add the given number of days and return the result.
  206.  
  207.     virtual CL_Date& operator+= (short num_days);
  208.     // Add the given number of days.
  209.  
  210.     virtual CL_Date  operator-  (short num_days) const;
  211.     // Subtract the given number of days and return the result.
  212.  
  213.     virtual CL_Date& operator-= (short num_days);
  214.     // Subtract the given number of days.
  215.  
  216.     virtual long operator-   (const CL_Date& date) const;
  217.     // Return the number of days between us and the given date (which
  218.     // may be positive or negative).
  219.     
  220.     CL_Date AddMonths (short num_months) const;
  221.     // Return the Date obtained by adding {\tt num_months} to this Date's
  222.     // month. The parameter can be positive or negative.
  223.  
  224.     CL_Date AddYears (short num_years) const;
  225.     // Return the Date obtained by adding {\tt num_years} to this Date's
  226.     // year. The parameter can be positive or negative.
  227.  
  228.     //
  229.     // --------------------- Static methods ---------------------------
  230.     //
  231.  
  232.     static CL_Date Today ();
  233.     // Return a date object with today's date in it.
  234.  
  235.     static CL_String DayName (WeekdayEnum weekday_num);
  236.     // Return name of given weekday.
  237.     // Return the empty string if the parameter is invalid.
  238.  
  239.     static WeekdayEnum  DayNumber (const CL_String& weekday_name);
  240.     // Do the opposite of the {\tt DayName}. Return -1 for an invalid name.
  241.  
  242.     static CL_String MonthName (short month_num);
  243.     // Return month name of given month (1 = "January", etc.). Return the
  244.     // empty string for an invalid month number.
  245.  
  246.     static CL_String ShortMonthName (short month_num);
  247.     // Return a 3-character month name of given month.
  248.     // Return the empty string for an invalid month number. 
  249.  
  250.     static MonthEnum MonthNumber (const CL_String& month_name);
  251.     // And the opposite of the above. Return -1 for an invalid month name.
  252.  
  253.     static bool IsLeapYear (short year);
  254.     // Is the given year a leap year?
  255.  
  256.     static short DaysInMonth  (short month, short year);
  257.     // Return the number of days in the given month and year.
  258.     
  259.     static long ParseAndConvert (const CL_String& date);
  260.     // Validate the given string as representing a date, and return
  261.     // either -1 if it's not a valid date, or else the number of days
  262.     // it represents since Jan 1, 1900. The string must be in one of
  263.     // the forms of Date_American, Date_Terse, or Date_Numbers, or
  264.     // else must be six digits (characters) of the form yymmdd or
  265.     // mmddyy. In the former three cases, the year can be either of
  266.     // the form 92 or 1992. The month names can be mixed case.
  267.  
  268.  
  269.     // ----------------------- Storage and restoration ----------------
  270.     
  271.     CL_String AsString () const
  272.         { return PrintString (Date_Numbers);};
  273.     // Overrides the method inherited from {\small\tt CL_Object}.
  274.  
  275.  
  276.     void FromStream (istream& strm);
  277.     // Overrides the method inherited from {\small\tt CL_Object}. The
  278.     // implementation skips fill characters, and then collects slashes,
  279.     // dashes, and alphabetic and numeric 
  280.     // characters and attempts to parse the result as a date. If successful,
  281.     // this date is modified; otherwise, the collected characters are put
  282.     // back and this date becomes invalid.
  283.     
  284.     long StorableFormWidth () const;
  285.     // Overrides the method inherited from {\small\tt CL_Object}.
  286.  
  287.     bool ReadFrom (const CL_Stream&);
  288.     // Overrides the method inherited from {\small\tt CL_Object}.
  289.  
  290.     bool WriteTo  (CL_Stream&) const;
  291.     // Overrides the method inherited from {\small\tt CL_Object}.
  292.  
  293.  
  294.     // -------------------------- Assignment ------------------------
  295.     
  296.  
  297.     virtual void operator= (const CL_Object&);
  298.     // Overrides the method inherited from {\small\tt CL_Object}.
  299.  
  300.     // ------------------------ Basic methods ----------------------
  301.     
  302.     const char* ClassName() const { return "CL_Date";};
  303.     // Overrides the method inherited from {\small\tt CL_Object}.
  304.     
  305.     CL_ClassId ClassId () const {return _CL_Date_CLASSID;};
  306.     // Overrides the method inherited from {\small\tt CL_Object}.
  307.  
  308.     CL_Object* Clone() const {return new CL_Date (*this);};
  309.     // Overrides the method inherited from {\small\tt CL_Object}.
  310.  
  311.  
  312.     
  313. protected:
  314.  
  315.     // The representation used is the number of days since Jan 1,
  316.     // 1901:
  317.     long _days;
  318.     
  319.     CL_Date (long num_days);
  320.     
  321. };
  322.  
  323.  
  324.  
  325.  
  326. inline void CL_Date::operator= (const CL_Object& obj)
  327. {
  328.     if (CheckClassType (obj, "CL_Date::op= (CL_Object&)"))
  329.         *this = (const CL_Date&) obj;
  330. }
  331.  
  332.  
  333.  
  334. inline short CL_Date::Compare (const CL_Object& obj) const
  335. {
  336.     return !IsA(obj) ? (this < (CL_Date*) &obj ? -1 :  1) 
  337.         : Compare ((const CL_Date&) obj);
  338. }
  339.  
  340.  
  341. inline bool CL_Date::IsLegal () const
  342. {
  343.     return _days > 0;
  344. }
  345.  
  346. #endif
  347.