    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Dates &amp; Times</title>
        <link>https://members.accu.org/index.php/articles/1338</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + Overload Journal #1 - Apr 1993</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c220/">01</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-220/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+220/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Dates &amp; Times</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 30 April 1993 11:53:00 +01:00 or Fri, 30 April 1993 11:53:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<p>Borland supply a class library known as the container class library.
This series of articles on the Borland class libraries does not
concentrate on the mechanics and detail functions that are available,
but on how to make use of them. In this first article I will look at
the uses of the Date class, the Time class, the Timer class and how to
make a Timestamp class.</p>
<p>The Date class can be used only in a limited way to store and print
dates. It is of little or no use to UK programmers as the
constructor that takes arguments is set up in the US format of
Months/Days/Years, and it has no validation on the month/no of days per
month (31st February is valid). The header file for the Date container
class is Idate.h. So this class is obviously unacceptable for the
professional programmer.</p>
<p>What we will now do is to derive a class from Date that overcomes
these limitations and adds a number of desirable features, which
include the ability to add or subtract a number of days from a date, to
subtract two dates from one another in order to obtain the
difference, to examine on what day of the week that the date
falls, to extract the Julian date from the date class, and to extract
the day number from within the year. It is also a requirement that the
output format is controllable.</p>
<p>The header file is as follows:</p>
<pre class="programlisting">  class CPPDate : public Date
  {
  protected:
    int validate(unsigned char, unsigned char, unsigned);
    void to_julian();
    void from_julian();
    long julian_value;
    unsigned char print_format; 
  public:
    CPPDate() : Date() {} // Default constructor
    CPPDate(unsigned char, unsigned char, unsigned);
    CPPDate(unsigned char, unsigned char, unsigned char);
    CPPDate(CPPDate&amp;); // Copy constructor
    void SetDD(unsigned char);
    void SetMM(unsigned char);
    void SetYY(unsigned);
    void SetYY(unsigned char);
    CPPDate&amp; operator = (CPPDate&amp;);
    int operator !(); // true only if valid date
    unsigned char GetDD(void);
    unsigned char GetMM(void);
    unsigned&nbsp;&nbsp;&nbsp;&nbsp; GetYY(void);
    // print function
    void printOn(ostream&amp;) const;
    void set_format(unsigned char);
    // computation functions
    CPPDate&amp; operator + (long); // add days
    CPPDate&amp; operator - (long); // subtract days
    long&amp; operator - (CPPDate&amp;); // date difference
    // misc functions
    long julian(void);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Julian date
    unsigned char dow(void); // day of week
    unsigned doy(void);&nbsp;&nbsp;&nbsp;&nbsp; // day of year
    int operator &lt; (CPPDate&amp;);
    // other comparators left out for space reasons
  };</pre>
<p>The complete source code for the member functions is on the provided
companion disk.</p>
<p>The formats that the date is output is dependent on calls to the
member function set_format and the following values produce the
results as shown for 6th March 1961:</p>
<p>
<table border="0">
    <tr>
      <td>0</td>
      <td> 6/3/61 (default)</td>
    </tr>
    <tr>
      <td>1</td>
      <td>06/03/61</td>
    </tr>
    <tr>
      <td>2</td>
      <td> 6/3/1961</td>
    </tr>
    <tr>
      <td>3</td>
      <td>06/03/1961</td>
    </tr>
    <tr>
      <td>4</td>
      <td>6-3-61</td>
    </tr>
    <tr>
      <td>5</td>
      <td>06-03-61</td>
    </tr>
    <tr>
      <td>6</td>
      <td>6-3-1961</td>
    </tr>
    <tr>
      <td>7</td>
      <td>06-03-1961</td>
    </tr>
    <tr>
      <td>8</td>
      <td>March 6th 1961</td>
    </tr>
    <tr>
      <td>9</td>
      <td>6th March 1961</td>
    </tr>
    <tr>
      <td>10</td>
      <td>6-Mar-1961</td>
    </tr>
    <tr>
      <td>11</td>
      <td>Wednesday March 6th 1961</td>
    </tr>
    <tr>
      <td>12</td>
      <td>Wednesday 6th March 1961</td>
    </tr>
    <tr>
      <td>13</td>
      <td>Wed 6-Mar-1961</td>
    </tr>
</table>
</p>
<p>If you want any other formats, they are easy enough to add. Thus a
simple program to display the date on which I was 10000 days old and
how long ago that was would be as follows.</p>
<pre class="programlisting">  CPPDate today, birthday(6,3,61);
  birthday.set_format(11);
  cout &lt;&lt;&nbsp;&nbsp; &quot;I was born on&nbsp; &quot;&nbsp; &lt;&lt;&nbsp; birthday&nbsp; &lt;&lt; endl
       &lt;&lt;&nbsp;&nbsp; &quot;My&nbsp; 10,000 day occured on&nbsp; &quot;
       &lt;&lt;&nbsp; birthday+10000&nbsp; &lt;&lt;&nbsp; endl
       &lt;&lt;&nbsp;&nbsp; &quot;which was&nbsp; &quot;&nbsp;&nbsp; &lt;&lt;&nbsp; today - (birthday+10000)
       &lt;&lt;&nbsp;&nbsp; &quot;&nbsp; days ago.&quot;&nbsp;&nbsp; &lt;&lt;&nbsp; endl;</pre>
<p>Easy isn't it?</p>
<p>The Time class needs similar enhancements to the Date class, the
ability to add and subtract times, to control the output format, and
the ability to reduce the time to the range 0:00 through 23:59:59.99
and returns the number of day adjustments made. The header
file for the Time is as follows:</p>
<pre class="programlisting">  class CPPTime : public Time
  {
  protected:
    unsigned char print_format;
    long double to_unit(void);
    void&nbsp; set from_unit(long double);
  public:
    CPPTime(void) : Time() {} // Default constructor 
    CPPTime(unsigned char = 0, unsigned char = 0,
            unsigned char = 0, unsigned char = 0); 
    CPPTime(CPPTime&amp;); // copy constructor 
    void SetHH(unsigned char); 
    void SetMM(unsigned char); 
    void SetSS(unsigned char); 
    void SetCC(unsigned char); 
    unsigned char GetHH(void); 
    unsigned char GetMM(void); 
    unsigned char GetSS(void); 
    unsigned char GetCC(void); 
    CPPTime&amp; operator = (CPPTime&amp;); 
    int operator !(void); // true only if time valid 
    int reduce(void); // returns days adjustmemt 
    CPPTime&amp; operator + (CPPTime&amp;); 
    CPPTime&amp; operator - (CPPTime&amp;); 
    void printOn(ostream&amp;) const; 
    int operator &lt; (CPPTime&amp;); 
    // other comparator operators left out
  };</pre>
<p>Again for this class and member functions the source is on the disk
provided as are several examples of how to use them. The formats in
which the time is output is dependent on the call to a member function
set_format and the following values produce the results as shown
for 9hrs 7min 3.2 secs:</p>
<pre class="programlisting">  9:07:03.20
  9:07:03
  9:07</pre>
<p>This class is not suitable for all applications, because, as it is
based on the Borland Time class, it is not able to hold negative times.
The final Borland class we will look at is the timer class. This class
is not based on the Object class. The timer class can be used as a
stopwatch for timing anything within the program. The timer class
is sufficiently useful to be used without modification.</p>
<p>Before the timer is used, it must be calibrated; fortunately this is
done before the call to main() automatically. Beware of using it
in any global constructors or any function or member functions that are
executed before main(). Timer cannot be used in windows
programs or DLLs (what happens when we get DOS DLLs I don't know
what will happen). The primary member functions used are start(),
stop() and reset() (uses are obvious), status() returns true if the
timer is running. The call to time() returns the number of seconds (as
a double).</p>
<p>In order to read the timer, it must first be stopped. Once stopped
the timer cannot be restarted.</p>
<pre class="programlisting">  #include &lt;iostream.h&gt;
  #include &lt;timer.h&gt;
  {
    Timer TO, Tl, T2;
    int i, j =0;
    float k = 0.0;
    T0.start();
    for (i=0;i&lt;30000;i++) ; // do nothing
    T0.stop();
    Tl.start();
    for (i=0;i&lt;30000;i++) j++;
    Tl.stop();
    T2.start();
    for (i=0;i&lt;30000;i++) k++;
    T2.stop();
    cout &lt;&lt; &quot;The int++ operation took &quot;
         &lt;&lt; Tl.timer()&nbsp; - T0.timer()
         &lt;&lt; &quot; seconds&quot; &lt;&lt; endl
         &lt;&lt; &quot;The float++ operation took &quot;
         &lt;&lt; T2.timer() - T0.timer()
         &lt;&lt; &quot; seconds&quot;&nbsp; &lt;&lt;&nbsp; endl;
  }</pre>
<p>The timestamp class is derived from both CPPDate and CPPTime. The
only function (apart from the constructors) is the now() member
function, which resets a timestamp to the current time. A timestamp is
normally fixed at the time of instanciation (default
constructor) or can be set to the value of another timestamp using
either the copy constructor or the equals (=) operator. There are an
adequate number of comparator operators associated with timestamp. The
only calculation allowed on timestamps is to subtract one from another,
the result being a long integer representing the number of
hundredths of seconds between them. The header file is as follows:</p>
<pre class="programlisting">  class Timestamp
  {
  protected:
    CPPDate Tdate;
    CPPTime TTime; 
  public:
    Timestamp(void);
    ~Timestamp(Timestamp&amp;);
    Timestamp&amp; operator = (Timestamp&amp;);
    friend long operator - (Timestamp&amp;, Timestamp&amp;);
    friend operator &lt;&lt; (ostream&amp;, Timestamp&amp;);
    int operator == (Timestamp&amp;);
    int operator &lt;= (Timestamp&amp;);
    int operator &lt; (Timestamp&amp;);
    int operator &gt;= (Timestamp&amp;);
    int operator &gt; (Timestamp&amp;);
    int operator != (Timestamp&amp;);
    Timestamp&amp; now(void);
  };
<br></pre></p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
