Implemented class hour.
authorArjen Baart <arjen@andromeda.nl>
Sun, 25 Aug 2019 18:12:39 +0000 (20:12 +0200)
committerArjen Baart <arjen@andromeda.nl>
Sun, 25 Aug 2019 18:12:39 +0000 (20:12 +0200)
doc/hour.xml
src/Makefile.am
src/date.cpp
src/date.h
src/hour.cpp
test/Makefile.am
test/hour_assign.cpp [new file with mode: 0644]
test/hour_assign.exp [new file with mode: 0644]
test/hour_parse.cpp [new file with mode: 0644]
test/hour_parse.exp [new file with mode: 0644]

index e1c4cd1..d36c9f0 100644 (file)
@@ -21,6 +21,7 @@ String objects or other hour objects.
 </para>
 <description>
 <item tag='hour()'>
+The default value is 0 hours, 0 minutes and 0 seconds.
 </item>
 <item tag='hour(long)'>
 A number of seconds.
@@ -28,16 +29,19 @@ A number of seconds.
 <item tag='hour(int hour, short minute, short second)'>
 </item>
 <item tag='hour(const String)'>
-
-Format is HH:MM:SS
-
+A time can be written in various formats, such as "1134", meaning
+11 hours and 34 minutes.
+The same time can be written as "11:34" or "11.34".
+A number of seconds can be added in the same manner.
+For example "11:34:25", "113425" or 11.34.25".
 </item>
 
 <item tag='operator=(hour &amp;)'>
 </item>
-<item tag='operator=(string &amp;)'>
+<item tag='operator=(String &amp;)'>
 </item>
 <item tag='operator=(long)'>
+Assing a number of seconds.
 </item>
 </description>
 
@@ -48,11 +52,11 @@ Format is HH:MM:SS
 
 <description>
 <item tag='long()'>
-
 Converts to a number of seconds.
 </item>
 
 <item tag='String()'>
+Converts to a textual representation of the hour in the format "hh:mm:ss".
 </item>
 </description>
 
@@ -63,7 +67,7 @@ Converts to a number of seconds.
 <description>
 <item tag='operator==(hour &amp;)'>
 </item>
-<item tag='>operator!=(hour &amp;)'>
+<item tag='operator!=(hour &amp;)'>
 </item>
 <item tag='operator&lt;(hour &amp;)'>
 </item>
@@ -80,11 +84,11 @@ Converts to a number of seconds.
 <heading>Attributes</heading>
 
 <description>
-<item tag='int hour()'>
+<item tag='int Hour()'>
 </item>
-<item tag='short minute()'>
+<item tag='short Minute()'>
 </item>
-<item tag='short second()'>
+<item tag='short Second()'>
 </item>
 </description>
 
@@ -94,16 +98,26 @@ Converts to a number of seconds.
 
 <description>
 <item tag='hour operator+(hour &amp;t1, hour &amp;t2)'>
+  Add two hour objects, for example:
+  <example>
+    11:45:30 + 1:20:40 = 13:06:10
+  </example>
 </item>
 <item tag='hour operator-(hour &amp;t1, hour &amp;t2)'>
+  Subtract two hour objects, for example:
+  <example>
+    11:05:30 - 1:20:40 =  9:44:50
+  </example>
 </item>
 <item tag='hour &amp;operator+=(hour &amp;t)'>
 </item>
 <item tag='hour &amp;operator-=(hour &amp;t)'>
 </item>
 <item tag='hour &amp;operator++()'>
+Add one second.
 </item>
 <item tag='hour &amp;operator--()'>
+Subtract one second.
 </item>
 </description>
 
index e6dcf30..837681f 100644 (file)
@@ -1,5 +1,6 @@
 lib_LTLIBRARIES = libACL.la
 
-libACL_la_SOURCES = string.cpp regex.cpp date.cpp parsedate.c dateyacc.y datelex.c
+libACL_la_SOURCES = string.cpp regex.cpp date.cpp parsedate.c dateyacc.y datelex.c \
+                    hour.cpp
 
 include_HEADERS = String.h date.h
index 9ee5d91..bf9907e 100644 (file)
@@ -259,8 +259,8 @@ date today()
 
 long date::julian(void)
 {
-   register long cent;
-   register int  yr, mn, dy;
+   long cent;
+   int  yr, mn, dy;
 
    yr = year;
    mn = month;
index 338b87b..25f023a 100644 (file)
@@ -129,7 +129,7 @@ public:
 
    int operator==(date d)
    {
-      register int cm;
+      int cm;
       
       cm = this->year - d.year;
       cm = (cm != 0) ? cm : this->month - d.month;
@@ -140,7 +140,7 @@ public:
 
    int operator!=(date d)
    {
-      register int cm;
+      int cm;
       
       cm = this->year - d.year;
       cm = (cm != 0) ? cm : this->month - d.month;
@@ -151,7 +151,7 @@ public:
    
    int operator<=(date d)
    {
-      register int cm;
+      int cm;
       
       cm = this->year - d.year;
       cm = (cm != 0) ? cm : this->month - d.month;
@@ -162,7 +162,7 @@ public:
    
    int operator>=(date d)
    {
-      register int cm;
+      int cm;
       
       cm = this->year - d.year;
       cm = (cm != 0) ? cm : this->month - d.month;
@@ -173,7 +173,7 @@ public:
    
    int operator>(date d)
    {
-      register int cm;
+      int cm;
       
       cm = this->year - d.year;
       cm = (cm != 0) ? cm : this->month - d.month;
@@ -184,7 +184,7 @@ public:
    
    int operator<(date d)
    {
-      register int cm;
+      int cm;
       
       cm = this->year - d.year;
       cm = (cm != 0) ? cm : this->month - d.month;
@@ -236,6 +236,17 @@ class hour
 
    friend class UTC;
 
+   int compare(hour h)
+   {
+      int cm;
+      
+      cm = this->hours - h.hours;
+      cm = (cm != 0) ? cm : this->minutes - h.minutes;
+      cm = (cm != 0) ? cm : this->seconds - h.seconds;
+      
+      return cm;
+   }
+
 public:
 
    hour()
@@ -256,6 +267,28 @@ public:
 
    bool proper();   // Check wether this is a proper clock time
 
+   //  Attributes.
+
+   short Second()
+   {
+      return seconds;
+   }
+
+   short Minute()
+   {
+      return minutes;
+   }
+
+   int Hour()
+   {
+      return hours;
+   }
+
+   bool operator==(hour h)
+   {
+      return compare(h) == 0;
+   }
+
    friend hour operator+(hour &, hour &);
    hour operator += (hour h);
    friend hour operator-(hour &, hour &);
index 703c126..26b7a30 100644 (file)
@@ -65,6 +65,14 @@ hour::hour(String s)
       minutes = pd->minute;
       seconds = pd->second;
    }
+   if (minutes == -1)
+   {
+      minutes = 0;
+   }
+   if (seconds == -1)
+   {
+      seconds = 0;
+   }
 }
 
 hour now()
index c3f99c7..a7cdd2f 100644 (file)
@@ -3,7 +3,8 @@ TESTS = $(check_PROGRAMS) date_today check_output
 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
+                 date_assign date_parse date_compare date_arithmetic date_attributes date_check_today \
+                 hour_assign hour_parse
 
 string_assign_SOURCES      = string_assign.cpp
 string_basics_SOURCES      = string_basics.cpp
@@ -21,3 +22,6 @@ date_attributes_SOURCES    = date_attributes.cpp
 date_check_today_SOURCES   = date_check_today.cpp
 
 date_today : date_check_today
+
+hour_assign_SOURCES        = hour_assign.cpp
+hour_parse_SOURCES         = hour_parse.cpp
diff --git a/test/hour_assign.cpp b/test/hour_assign.cpp
new file mode 100644 (file)
index 0000000..8a25691
--- /dev/null
@@ -0,0 +1,57 @@
+/*******************************************************
+ *  Unit test for the hour class
+ *
+ * test contrustor and assignment of hour objects
+ ******************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+int main()
+{
+   // Default contructor: all zero
+   hour h0;
+
+   std::cout << "The default contructor makes a zero hour: \"" << h0 << "\"\n";
+   assert(h0.Hour() == 0);
+   assert(h0.Minute() == 0);
+   assert(h0.Second() == 0);
+
+   hour h1(15, 20, 45);
+
+   std::cout << "Contructor with hour = 15 minure = 20, and second = 45: \"" << h1 << "\"\n";
+   assert(h1.Hour() == 15);
+   assert(h1.Minute() == 20);
+   assert(h1.Second() == 45);
+
+   hour h2(88, 55, 1000);
+
+   std::cout << "Contructor with hour = 88 minute = 55, and second = 1000: \"" << h2 << "\"\n";
+   assert(h2.Hour() == 88);
+   assert(h2.Minute() == 55);
+   assert(h2.Second() == 1000);
+
+   // The copy constructor is just a bitwise copy
+
+   hour h3(h1);
+
+   std::cout << "Copy contructor from " << h1 << ": \"" << h3 << "\"\n";
+   assert(h3.Hour() == 15);
+   assert(h3.Minute() == 20);
+   assert(h3.Second() == 45);
+
+   // Also assignment is just a bitwise copy
+
+   hour h4;
+
+   h4 = h1;
+   std::cout << "Assigned from " << h1 << ": \"" << h4 << "\"\n";
+   assert(h4.Hour() == 15);
+   assert(h4.Minute() == 20);
+   assert(h4.Second() == 45);
+
+   return 0;
+}
+
diff --git a/test/hour_assign.exp b/test/hour_assign.exp
new file mode 100644 (file)
index 0000000..18fd34e
--- /dev/null
@@ -0,0 +1,6 @@
+The default contructor makes a zero hour: "00:00:00"
+Contructor with hour = 15 minure = 20, and second = 45: "15:20:45"
+Contructor with hour = 88 minute = 55, and second = 1000: "88:55:1000"
+Copy contructor from 15:20:45: "15:20:45"
+Assigned from 15:20:45: "15:20:45"
+PASS hour_assign (exit status: 0)
diff --git a/test/hour_parse.cpp b/test/hour_parse.cpp
new file mode 100644 (file)
index 0000000..9648fe7
--- /dev/null
@@ -0,0 +1,53 @@
+/*******************************************************
+ *  Unit test for the hour class
+ *
+ * test the date parser that converts String to hour
+ ******************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+int main()
+{
+   hour   h0;
+   String the_time;
+
+
+   the_time = "1134";
+   h0 = hour(the_time);
+   std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n";
+   std::cout.flush();
+   assert(h0 == hour(11, 34, 0));
+
+   the_time = "11:34";
+   h0 = hour(the_time);
+   std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n";
+   assert(h0 == hour(11, 34, 0));
+
+   the_time = "11.34";
+   h0 = hour(the_time);
+   std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n";
+   assert(h0 == hour(11, 34, 0));
+
+   the_time = "113427";
+   h0 = hour(the_time);
+   std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n";
+   assert(h0 == hour(11, 34, 27));
+
+   the_time = "11:34:27";
+   h0 = hour(the_time);
+   std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n";
+   assert(h0 == hour(11, 34, 27));
+
+   the_time = "11.34.27";
+   h0 = hour(the_time);
+   std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n";
+   assert(h0 == hour(11, 34, 27));
+
+   //  TODO: test for invalid houts and syntax errors
+
+   return 0;
+}
+
diff --git a/test/hour_parse.exp b/test/hour_parse.exp
new file mode 100644 (file)
index 0000000..cc9c633
--- /dev/null
@@ -0,0 +1,7 @@
+String "1134" converts to: "11:34:00"
+String "11:34" converts to: "11:34:00"
+String "11.34" converts to: "11:34:00"
+String "113427" converts to: "11:34:27"
+String "11:34:27" converts to: "11:34:27"
+String "11.34.27" converts to: "11:34:27"
+PASS hour_parse (exit status: 0)