Implemented class UTC.
authorArjen Baart <arjen@andromeda.nl>
Tue, 8 Oct 2019 20:16:45 +0000 (22:16 +0200)
committerArjen Baart <arjen@andromeda.nl>
Tue, 8 Oct 2019 20:16:45 +0000 (22:16 +0200)
doc/Makefile.am
doc/manual.xml
doc/utc.xml [new file with mode: 0644]
src/Makefile.am
src/date.h
test/Makefile.am
test/date_attributes.exp [new file with mode: 0644]
test/utc_assign.cpp [new file with mode: 0644]
test/utc_assign.exp [new file with mode: 0644]
test/utc_compare.cpp [new file with mode: 0644]
test/utc_compare.exp [new file with mode: 0644]

index 200bf0c..94b843e 100644 (file)
@@ -7,7 +7,7 @@ SUFFIXES = .obj .eps .png
 .obj.eps:
        tgif -print -eps -color $<
 
-XMLS = manual.xml string.xml date.xml hour.xml
+XMLS = manual.xml string.xml date.xml hour.xml utc.xml
 IMAGES=
 
 PICTURES=
index d1ce759..bbb9745 100644 (file)
@@ -11,5 +11,7 @@
          xmlns:xi="http://www.w3.org/2001/XInclude"/>
     <xi:include href="hour.xml"
          xmlns:xi="http://www.w3.org/2001/XInclude"/>
+    <xi:include href="utc.xml"
+         xmlns:xi="http://www.w3.org/2001/XInclude"/>
   </book>
 </doc>
diff --git a/doc/utc.xml b/doc/utc.xml
new file mode 100644 (file)
index 0000000..11b102d
--- /dev/null
@@ -0,0 +1,95 @@
+<chapter>
+<heading>UTC - Universal Time Coordinated class</heading>
+
+<pre>
+#include &lt;date.h&gt;
+
+UTC clock;
+</pre>
+
+<section>
+<heading>Construction and assignment</heading>
+<para>
+A UTC object can be constructed from its parts or by parsing from a String.
+<description>
+<item tag="UTC()">
+The default constructor creates a default of its parts.
+</item>
+<item tag="UTC(date d, hour t)">
+</item>
+<item tag="UTC(String s)">
+Parse the date and time from String s.
+</item>
+</description>
+</para>
+
+<description>
+<item tag="operator=(String &amp;s)">
+Parse the date and time from String s.
+</item>
+</description>
+
+</section>
+
+<section>
+<heading>Conversion</heading>
+
+<para>
+String()
+</para>
+
+</section>
+
+<section>
+<heading>Relational operations</heading>
+
+<description>
+<item tag="operator==(UTC &amp;)"></item>
+<item tag="operator!=(UTC &amp;)"></item>
+<item tag="operator&lt;(UTC &amp;)"></item>
+<item tag="operator&lt;=(UTC &amp;)"></item>
+<item tag="operator&gt;(UTC &amp;)"></item>
+<item tag="operator&gt;=(UTC &amp;)"></item>
+</description>
+
+</section>
+
+<section>
+<heading>Attributes</heading>
+
+<h5>date day()</h5>
+<h5>hour hour()</h5>
+</section>
+
+<section>
+<heading>Arithmetic</heading>
+
+<description>
+<item tag="UTC operator+(UTC &amp;t1, UTC &amp;t2)"></item>
+<item tag="UTC operator-(UTC &amp;t1, UTC &amp;t2)"></item>
+<item tag="UTC &amp;operator+=(UTC &amp;t)"></item>
+<item tag="UTC &amp;operator-=(UTC &amp;t)"></item>
+<item tag="UTC &amp;operator++()">
+Add one second.
+</item>
+<item tag="UTC &amp;operator--()">
+Subtract one second.
+</item>
+</description>
+
+</section>
+
+<section>
+<heading>Stream I/O</heading>
+
+<description>
+<item tag="ostream &amp;operator&lt;&lt;(ostream &amp;str, UTC &amp;t)">
+Write the date and time to the output stream in the default format.
+</item>
+<item tag="istream &amp;operator&gt;&gt;(istream &amp;str, UTC &amp;t)">
+Scan the input stream for a date and time.
+</item>
+</description>
+</section>
+
+</chapter>
index 837681f..7e1d3cc 100644 (file)
@@ -1,6 +1,6 @@
 lib_LTLIBRARIES = libACL.la
 
 libACL_la_SOURCES = string.cpp regex.cpp date.cpp parsedate.c dateyacc.y datelex.c \
-                    hour.cpp
+                    hour.cpp utc.cpp
 
 include_HEADERS = String.h date.h
index d9d2afa..7b6fd3b 100644 (file)
@@ -386,6 +386,67 @@ public:
       return d.proper() && t.proper();
    }
 
+   bool operator==(UTC u)
+   {
+      return d == u.d && t == u.t;
+   }
+
+   bool operator!=(UTC u)
+   {
+      return d != u.d || t != u.t;
+   }
+
+   bool operator<=(UTC u)
+   {
+      if (d == u.d)
+      {
+         return t <= u.t;
+      }
+      else
+      {
+         return d <= u.d;
+      }
+   }
+
+   bool operator<(UTC u)
+   {
+      if (d == u.d)
+      {
+         return t < u.t;
+      }
+      else
+      {
+         return d < u.d;
+      }
+   }
+
+   bool operator>=(UTC u)
+   {
+      if (d == u.d)
+      {
+         return t >= u.t;
+      }
+      else
+      {
+         return d >= u.d;
+      }
+   }
+
+   bool operator>(UTC u)
+   {
+      if (d == u.d)
+      {
+         return t > u.t;
+      }
+      else
+      {
+         return d > u.d;
+      }
+   }
+
+   friend UTC operator+(UTC &, UTC &);
+   UTC operator += (UTC h);
+
    friend std::ostream& operator<<(std::ostream &, const UTC &);
    friend std::istream& operator>>(std::istream &, UTC &);
 
index aeeb56e..b4691f5 100644 (file)
@@ -4,7 +4,8 @@ AM_CPPFLAGS = -I../src
 LDADD = ../src/.libs/libACL.la
 check_PROGRAMS = string_assign string_basics string_compare string_cat string_substring string_regex \
                  date_assign date_parse date_compare date_arithmetic date_attributes date_check_today \
-                 hour_assign hour_parse hour_compare hour_arithmetic hour_check_now
+                 hour_assign hour_parse hour_compare hour_arithmetic hour_check_now \
+                 utc_assign utc_compare
 
 string_assign_SOURCES      = string_assign.cpp
 string_basics_SOURCES      = string_basics.cpp
@@ -30,3 +31,5 @@ hour_arithmetic_SOURCES    = hour_arithmetic.cpp
 hour_check_now_SOURCES     = hour_check_now.cpp
 
 hour_now : hour_check_now
+utc_assign_SOURCES         = utc_assign.cpp
+utc_compare_SOURCES        = utc_compare.cpp
diff --git a/test/date_attributes.exp b/test/date_attributes.exp
new file mode 100644 (file)
index 0000000..6a95164
--- /dev/null
@@ -0,0 +1,12 @@
+09-06-2019 Day = 9
+09-06-2019 Month = 6
+09-06-2019 Monthname = Jun
+09-06-2019 Year = 2019
+09-06-2019 Leap = 0
+09-06-2020 Leap = 1
+09-06-2000 Leap = 1
+09-06-2100 Leap = 0
+Jan 2019 has 31 days.
+Feb 2019 has 28 days.
+Feb 2020 has 29 days.
+PASS date_attributes (exit status: 0)
diff --git a/test/utc_assign.cpp b/test/utc_assign.cpp
new file mode 100644 (file)
index 0000000..844748f
--- /dev/null
@@ -0,0 +1,78 @@
+/*******************************************************
+ *  Unit test for the UTC class
+ *
+ * test contrustor and assignment of UTC objects
+ ******************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+int main()
+{
+   // Default contructor: all zero
+   UTC  u0;
+   date d0;
+   hour h0;
+
+   d0 = date(u0);
+   h0 = hour(u0);
+
+   std::cout << "The default contructor makes a zero date:and time " << u0 << "\n";
+   assert(d0.Day() == 0);
+   assert(d0.Month() == 0);
+   assert(d0.Year() == 0);
+   assert(h0.Hour() == 0);
+   assert(h0.Minute() == 0);
+   assert(h0.Second() == 0);
+
+   date d1(15, 9, 2002);
+   hour h1(15, 23, 46);
+
+   UTC  u1(d1, h1);
+
+   std::cout << "Contructor with date = 15-9-2002, hour = 15:23:46: " << u1 << "\n";
+   date d2(u1);
+   hour h2(u1);
+
+   assert(d2.Day() == 15);
+   assert(d2.Month() == 9);
+   assert(d2.Year() == 2002);
+   assert(h2.Hour() == 15);
+   assert(h2.Minute() == 23);
+   assert(h2.Second() == 46);
+
+   String s3("23-10-2019 23:45:16");
+   UTC    u3(s3);
+
+   std::cout << "Contructor from String " << s3 << " : " << u3 << "\n";
+
+   date d3(u3);
+   hour h3(u3);
+
+   assert(d3.Day() == 23);
+   assert(d3.Month() == 10);
+   assert(d3.Year() == 2019);
+   assert(h3.Hour() == 23);
+   assert(h3.Minute() == 45);
+   assert(h3.Second() == 16);
+
+   String s4("Thu Oct  3 22:38:38 CEST 2019");
+   UTC    u4(s4);
+
+   std::cout << "Contructor from String " << s4 << " : " << u4 << "\n";
+
+   date d4(u4);
+   hour h4(u4);
+
+   assert(d4.Day() == 3);
+   assert(d4.Month() == 10);
+   assert(d4.Year() == 2019);
+   assert(h4.Hour() == 22);
+   assert(h4.Minute() == 38);
+   assert(h4.Second() == 38);
+
+   return 0;
+}
+
diff --git a/test/utc_assign.exp b/test/utc_assign.exp
new file mode 100644 (file)
index 0000000..54b36ea
--- /dev/null
@@ -0,0 +1,5 @@
+The default contructor makes a zero date:and time 00-00-0 00:00:00
+Contructor with date = 15-9-2002, hour = 15:23:46: 15-09-2002 15:23:46
+Contructor from String 23-10-2019 23:45:16 : 23-10-2019 23:45:16
+Contructor from String Thu Oct  3 22:38:38 CEST 2019 : 03-10-2019 22:38:38
+PASS utc_assign (exit status: 0)
diff --git a/test/utc_compare.cpp b/test/utc_compare.cpp
new file mode 100644 (file)
index 0000000..b46552f
--- /dev/null
@@ -0,0 +1,79 @@
+/*******************************************************
+ *  Unit test for the UTC class
+ *
+ * test UTC relational operators
+ ******************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+int main()
+{
+   UTC   u0("9-6-2019 11:22:00");
+   UTC   u1("9-6-2019 11:22:00");
+   UTC   u2("9-6-2019 11:22:01");
+   UTC   u3("7-8-2019 11:22:00");
+
+   //  equal dates
+
+   std::cout << u0 << " == " << u1 << " = " << (u0 == u1) << "\n";
+   assert((u0 == u1) == true);
+
+   std::cout << u0 << " != " << u1 << " = " << (u0 != u1) << "\n";
+   assert((u0 != u1) == false);
+
+   std::cout << u0 << " < " << u1 << " = " << (u0 < u1) << "\n";
+   assert((u0 < u1) == false);
+
+   std::cout << u0 << " <= " << u1 << " = " << (u0 <= u1) << "\n";
+   assert((u0 <= u1) == true);
+
+   std::cout << u0 << " > " << u1 << " = " << (u0 > u1) << "\n";
+   assert((u0 > u1) == false);
+
+   std::cout << u0 << " >= " << u1 << " = " << (u0 >= u1) << "\n";
+   assert((u0 >= u1) == true);
+
+   //  unequal dates
+
+   std::cout << u0 << " == " << u2 << " = " << (u0 == u2) << "\n";
+   assert((u0 == u2) == false);
+
+   std::cout << u0 << " != " << u2 << " = " << (u0 != u2) << "\n";
+   assert((u0 != u2) == true);
+
+   std::cout << u0 << " < " << u2 << " = " << (u0 < u2) << "\n";
+   assert((u0 < u2) == true);
+
+   std::cout << u0 << " <= " << u2 << " = " << (u0 <= u2) << "\n";
+   assert((u0 <= u2) == true);
+
+   std::cout << u0 << " > " << u2 << " = " << (u0 > u2) << "\n";
+   assert((u0 > u2) == false);
+
+   std::cout << u0 << " >= " << u2 << " = " << (u0 >= u2) << "\n";
+   assert((u0 >= u2) == false);
+
+   std::cout << u0 << " == " << u3 << " = " << (u0 == u3) << "\n";
+   assert((u0 == u3) == false);
+
+   std::cout << u0 << " != " << u3 << " = " << (u0 != u3) << "\n";
+   assert((u0 != u3) == true);
+
+   std::cout << u0 << " < " << u3 << " = " << (u0 < u3) << "\n";
+   assert((u0 < u3) == true);
+
+   std::cout << u0 << " <= " << u3 << " = " << (u0 <= u3) << "\n";
+   assert((u0 <= u3) == true);
+
+   std::cout << u0 << " > " << u3 << " = " << (u0 > u3) << "\n";
+   assert((u0 > u3) == false);
+
+   std::cout << u0 << " >= " << u3 << " = " << (u0 >= u3) << "\n";
+   assert((u0 >= u3) == false);
+
+   return 0;
+}
+
diff --git a/test/utc_compare.exp b/test/utc_compare.exp
new file mode 100644 (file)
index 0000000..7ca9fa2
--- /dev/null
@@ -0,0 +1,19 @@
+09-06-2019 11:22:00 == 09-06-2019 11:22:00 = 1
+09-06-2019 11:22:00 != 09-06-2019 11:22:00 = 0
+09-06-2019 11:22:00 < 09-06-2019 11:22:00 = 0
+09-06-2019 11:22:00 <= 09-06-2019 11:22:00 = 1
+09-06-2019 11:22:00 > 09-06-2019 11:22:00 = 0
+09-06-2019 11:22:00 >= 09-06-2019 11:22:00 = 1
+09-06-2019 11:22:00 == 09-06-2019 11:22:01 = 0
+09-06-2019 11:22:00 != 09-06-2019 11:22:01 = 1
+09-06-2019 11:22:00 < 09-06-2019 11:22:01 = 1
+09-06-2019 11:22:00 <= 09-06-2019 11:22:01 = 1
+09-06-2019 11:22:00 > 09-06-2019 11:22:01 = 0
+09-06-2019 11:22:00 >= 09-06-2019 11:22:01 = 0
+09-06-2019 11:22:00 == 07-08-2019 11:22:00 = 0
+09-06-2019 11:22:00 != 07-08-2019 11:22:00 = 1
+09-06-2019 11:22:00 < 07-08-2019 11:22:00 = 1
+09-06-2019 11:22:00 <= 07-08-2019 11:22:00 = 1
+09-06-2019 11:22:00 > 07-08-2019 11:22:00 = 0
+09-06-2019 11:22:00 >= 07-08-2019 11:22:00 = 0
+PASS utc_compare (exit status: 0)