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

  1.  
  2.  
  3. #ifndef _timeofday_h_
  4. #define _timeofday_h_
  5.  
  6.  
  7.  
  8.  
  9.  
  10. /*
  11.  *
  12.  *          Copyright (C) 1994, M. A. Sridhar
  13.  *  
  14.  *
  15.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  16.  *     to copy, modify or distribute this software  as you see fit,
  17.  *     and to use  it  for  any  purpose, provided   this copyright
  18.  *     notice and the following   disclaimer are included  with all
  19.  *     copies.
  20.  *
  21.  *                        DISCLAIMER
  22.  *
  23.  *     The author makes no warranties, either expressed or implied,
  24.  *     with respect  to  this  software, its  quality, performance,
  25.  *     merchantability, or fitness for any particular purpose. This
  26.  *     software is distributed  AS IS.  The  user of this  software
  27.  *     assumes all risks  as to its quality  and performance. In no
  28.  *     event shall the author be liable for any direct, indirect or
  29.  *     consequential damages, even if the  author has been  advised
  30.  *     as to the possibility of such damages.
  31.  *
  32.  */
  33.  
  34.  
  35.  
  36.  
  37. // This is an encapsulation of a particular time of day. The printable
  38. // form of a TimeOfDay is one of the following:
  39. // \par{\tt\begin{tabular}{ll}
  40. //            Time_Normal    &    1:30 pm   \\
  41. //            Time_Military  &    13:30:23  \\
  42. // \end{tabular}}
  43.  
  44.  
  45.  
  46.  
  47. #ifdef __GNUC__
  48. #pragma interface
  49. #endif
  50.  
  51. #include "base/string.h"
  52.  
  53. enum TimePrintForm { Time_Normal, Time_Military};
  54.  
  55. class CL_EXPORT  CL_TimeOfDay: public CL_Object {
  56.  
  57. public:
  58.  
  59.     //
  60.     // ------------- Constructors and destructors ----------------------
  61.     //
  62.  
  63.     CL_TimeOfDay ();
  64.     // Construct an "empty" time object, representing midnight
  65.  
  66.     CL_TimeOfDay (short hour, short minute, short second);
  67.     // Construct the given time, assuming that the hour is in the
  68.     // range 0 to 23.
  69.  
  70.     CL_TimeOfDay (const CL_String& s);
  71.     // Construct a time of day from the given string, assuming that it
  72.     // contains a representation in one of Time_Normal or
  73.     // Time_Military forms, or else either four digits (hhmm) or six
  74.     // digits (hhmmdd).
  75.     
  76.     CL_TimeOfDay (long nsecs);
  77.     // Construct a TimeOfDay with the given number of seconds since
  78.     // midnight.
  79.     
  80.     CL_TimeOfDay (const CL_TimeOfDay&);
  81.     // Copy constructor
  82.  
  83.     ~CL_TimeOfDay ();
  84.     // Destructor
  85.  
  86.     //
  87.     // ---------------------- Access ----------------------------------
  88.     //
  89.  
  90.     short Hour() const;
  91.     // Return our hour.
  92.  
  93.     short Minute () const;
  94.     // Return our minute.
  95.  
  96.     short Second () const;
  97.     // Return our second.
  98.  
  99.     
  100.  
  101.     CL_String PrintString (TimePrintForm form = Time_Normal) const;
  102.     // Return a printable form of ourselves, according to the
  103.     // parameter.
  104.  
  105.  
  106.     //
  107.     // --------------------- Comparison ------------------------------
  108.     //
  109.  
  110.     bool operator<  (const CL_TimeOfDay&) const;
  111.  
  112.     bool operator<= (const CL_TimeOfDay&) const;
  113.  
  114.     bool operator>  (const CL_TimeOfDay&) const;
  115.  
  116.     bool operator>= (const CL_TimeOfDay&) const;
  117.  
  118.     bool operator== (const CL_TimeOfDay&) const;
  119.  
  120.     bool operator!= (const CL_TimeOfDay&) const;
  121.  
  122.     bool operator<  (const CL_Object& obj) const;
  123.  
  124.     bool operator<= (const CL_Object& obj) const;
  125.  
  126.     bool operator>  (const CL_Object& obj) const;
  127.  
  128.     bool operator>= (const CL_Object& obj) const;
  129.  
  130.     bool operator== (const CL_Object& obj) const;
  131.  
  132.     bool operator!= (const CL_Object& obj) const;
  133.  
  134.     short Compare (const CL_TimeOfDay&) const;
  135.  
  136.     short Compare (const CL_Object&) const;
  137.  
  138.     bool IsBetween (const CL_TimeOfDay& t1, const CL_TimeOfDay& t2) const;
  139.     // Return TRUE if this time is between the two given times.
  140.     
  141.  
  142.     //
  143.     // -------------------------- Modification ----------------------
  144.     //
  145.  
  146.     // Assignment
  147.     
  148.     CL_TimeOfDay& operator= (const CL_TimeOfDay&);
  149.  
  150.     virtual void operator= (const CL_String&);
  151.     // Convert the given string into a TimeOfDay via ParseAndConvert() (see
  152.     // below), and assign the result to ourselves. If the strins is not
  153.     // syntactically correct, the time is set to midnight.
  154.  
  155.     virtual void operator= (const CL_Object&);
  156.     // Check that the given object is a TimeOfDay (via ClassId()), downcast
  157.     // it and assign it to ourselves.
  158.  
  159.     //
  160.     // -------------------------- TimeOfDay arithmetic -------------------
  161.     //
  162.  
  163.     
  164.     // add or subtract the given number of seconds
  165.     
  166.     CL_TimeOfDay  operator+  (long num_secs) const;
  167.  
  168.     CL_TimeOfDay& operator+= (long num_secs);
  169.  
  170.     CL_TimeOfDay  operator-  (long num_secs) const;
  171.  
  172.     CL_TimeOfDay& operator-= (long num_secs);
  173.  
  174.     long operator-   (const CL_TimeOfDay&) const;
  175.     // Return the number of seconds between us and the given time.
  176.  
  177.  
  178.     //
  179.     // ------------------------ Static  methods --------------------------
  180.     //
  181.  
  182.     static CL_TimeOfDay Now ();
  183.     // Return a time object containing the current time
  184.  
  185.     static long ParseAndConvert (const CL_String& s);
  186.     // Validate the given string as representing a given time of day,
  187.     // and return either -1 if it's not a valid time, or else the
  188.     // number of seconds since midnight. The given string must
  189.     // contain a time representation in one of Time_Normal or
  190.     // Time_Military forms, or else either four digits (hhmm) or six
  191.     // digits (hhmmdd).
  192.  
  193.     // ----------------- Saving and restoration --------------------
  194.  
  195.     void FromStream (istream&);
  196.     // Override the method inherited from {\small\tt CL_Object}. The
  197.     // implementation skips fill characters, and them collects numerics and
  198.     // ':' characters; the result is then assumed to be in Time_Military
  199.     // form and parsed.
  200.     
  201.     long StorableFormWidth () const;
  202.     // Override the method inherited from {\small\tt CL_Object}.
  203.  
  204.     bool ReadFrom (const CL_Stream&);
  205.     // Override the method inherited from {\small\tt CL_Object}.
  206.  
  207.     bool WriteTo  (CL_Stream&) const;
  208.     // Override the method inherited from {\small\tt CL_Object}.
  209.  
  210.     CL_String AsString () const;
  211.     // Override the method inherited from {\small\tt CL_Object}.
  212.  
  213.  
  214.     // ----------------------- Basic methods --------------
  215.  
  216.     const char* ClassName() const { return "CL_TimeOfDay";};
  217.     // Return the class name of this object
  218.     
  219.     CL_ClassId ClassId () const {return _CL_TimeOfDay_CLASSID;};
  220.     // Return the class id of this object
  221.  
  222.     CL_Object* Clone () const {return new CL_TimeOfDay (*this);};
  223.     
  224.     //
  225.     // -------------------- End public protocol ---------------------
  226.     //
  227.     
  228.  
  229. protected:
  230.  
  231.     // The representation used is the number of seconds since midnight:
  232.     long _numSecs;
  233. };
  234.  
  235.  
  236.  
  237.  
  238. //-------------------------Comparison operations-------------------------
  239.  
  240. // return 1 if we are less than the TimeOfDay object passed to us; 
  241. // return 0 otherwise;
  242.  
  243. inline bool CL_TimeOfDay::operator< (const CL_TimeOfDay& a_time) const
  244. {
  245.     return (_numSecs < a_time._numSecs);
  246. }
  247.  
  248. // return 1 if we are less or equal to the TimeOfDay object passed to us; 
  249. // return 0 otherwise;
  250.  
  251. inline bool CL_TimeOfDay::operator<= (const CL_TimeOfDay& a_time) const
  252. {
  253.     return (_numSecs <= a_time._numSecs);
  254. }
  255.  
  256.  
  257. // return 1 if we are greater than the TimeOfDay object passed to us; 
  258. // return 0 otherwise;
  259.     
  260. inline bool CL_TimeOfDay::operator> (const CL_TimeOfDay& a_time) const
  261. {
  262.     return (_numSecs > a_time._numSecs);
  263. }
  264.  
  265. // return 1 if we are greater than or equal to the TimeOfDay object passed; 
  266. // return 0 otherwise;
  267.  
  268. inline bool CL_TimeOfDay::operator>= (const CL_TimeOfDay& a_time) const
  269. {
  270.     return (_numSecs >= a_time._numSecs);
  271. }
  272.  
  273. // return 1 if we are equal to the TimeOfDay object passed to us; 
  274. // return 0 otherwise;
  275.  
  276.  
  277. inline bool CL_TimeOfDay::operator== (const CL_TimeOfDay& a_time) const
  278. {
  279.     return (_numSecs == a_time._numSecs);
  280. }
  281.  
  282. // return 1 if we are not equal to the TimeOfDay object passed to us; 
  283. // return 0 otherwise;
  284.  
  285. inline bool CL_TimeOfDay::operator!= (const CL_TimeOfDay& a_time) const
  286. {
  287.     return (_numSecs  != a_time._numSecs);
  288. }
  289.  
  290. inline CL_String CL_TimeOfDay::AsString() const
  291. {
  292.     return PrintString (Time_Military);
  293. }
  294.  
  295.  
  296. inline void CL_TimeOfDay::operator= (const CL_Object& obj)
  297. {
  298.     if (CheckClassType (obj, "CL_TimeOfDay::op= (CL_Object&)"))
  299.         *this = (const CL_TimeOfDay&) obj;
  300. }
  301.  
  302.  
  303.  
  304. inline short CL_TimeOfDay::Compare (const CL_TimeOfDay& d) const
  305. {
  306.     return _numSecs < d._numSecs ? -1 : (_numSecs == d._numSecs ? 0 : 1);
  307. }
  308.  
  309.  
  310. inline short CL_TimeOfDay::Compare (const CL_Object& obj) const
  311. {
  312.     return (!IsA (obj)) ? (this < (CL_TimeOfDay*) &obj ? -1 :  1)
  313.         : Compare ((const CL_TimeOfDay&) obj);
  314. }
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321. #endif
  322.