Implemented hour addition and subtraction.
authorArjen Baart <arjen@andromeda.nl>
Thu, 29 Aug 2019 03:16:20 +0000 (05:16 +0200)
committerArjen Baart <arjen@andromeda.nl>
Thu, 29 Aug 2019 03:16:20 +0000 (05:16 +0200)
ChangeLog
configure.ac
src/date.h
src/datelex.c
src/hour.cpp
test/Makefile.am
test/hour_arithmetic.cpp [new file with mode: 0644]
test/hour_arithmetic.exp [new file with mode: 0644]

index 393ab1b..86dec94 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,10 +1,16 @@
-May 30, 2019 - Release 0.1
+Aug 29, 2019 - Release 0.3
 ============================
 
-   - Extracted the classes not related to X windows from AXE
-   - Implemented and brushed up the String class.
+  - Implemented hour class
 
 Jun 22, 2019 - Release 0.2
 ============================
 
    - Added date class
+
+May 30, 2019 - Release 0.1
+============================
+
+   - Extracted the classes not related to X windows from AXE
+   - Implemented and brushed up the String class.
+
index 3c53ac7..5657a2b 100644 (file)
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ([2.69])
-AC_INIT([libACL], [0.2], [arjen@androemda.nl])
+AC_INIT([libACL], [0.3], [arjen@androemda.nl])
 AM_INIT_AUTOMAKE([-Wall foreign])
 AC_CONFIG_MACRO_DIRS([m4])
 AC_CONFIG_FILES([Makefile src/Makefile test/Makefile doc/Makefile])
index 2776e12..d9d2afa 100644 (file)
@@ -247,6 +247,9 @@ class hour
       return cm;
    }
 
+   hour increase();
+   hour decrease();
+
 public:
 
    hour()
@@ -316,7 +319,30 @@ public:
 
    friend hour operator+(hour &, hour &);
    hour operator += (hour h);
+
+   hour operator ++ ()        // prefix
+   {
+      return increase();
+   }
+
+   hour operator ++ (int)     // postfix
+   {
+      return increase();
+   }
+
    friend hour operator-(hour &, hour &);
+   hour operator -= (hour h);
+
+   hour operator -- ()        // prefix
+   {
+      return decrease();
+   }
+
+   hour operator -- (int)     // postfix
+   {
+      return decrease();
+   }
+
 
    friend std::ostream& operator<<(std::ostream &, const hour &);
    friend std::istream& operator>>(std::istream &, hour &);
index bb27781..7cff26a 100644 (file)
@@ -1,15 +1,15 @@
 /*$Log: datelex.c,v $
-/*Revision 1.2  2002-09-28 06:58:45  arjen
-/*Bugfix: conversion of an empty string to a date or hour object
-/*now makes the values of such an object 0 (null) instead of giving
-/*a segmentation fault.
-/*The class UTC combines the date and hour classes. The most basic
-/*functions of the UTC class are now implemented.
-/*These include constructors and conversion to and from String objects.
-/*New functions: date::proper(), hour::proper() and UTC::proper().
-/*Return true if the object holds a proper clock time and/or calendar
-/*date; false if at least one value is out of range.
-/*
+ *Revision 1.2  2002-09-28 06:58:45  arjen
+ *Bugfix: conversion of an empty string to a date or hour object
+ *now makes the values of such an object 0 (null) instead of giving
+ *a segmentation fault.
+ *The class UTC combines the date and hour classes. The most basic
+ *functions of the UTC class are now implemented.
+ *These include constructors and conversion to and from String objects.
+ *New functions: date::proper(), hour::proper() and UTC::proper().
+ *Return true if the object holds a proper clock time and/or calendar
+ *date; false if at least one value is out of range.
+ *
  *Revision 1.1  2002/07/25 08:01:26  arjen
  *First checkin, AXE release 0.2
  *
index 26b7a30..347b656 100644 (file)
@@ -135,6 +135,23 @@ hour hour::operator+=(hour t)
    return *this;
 }
 
+hour hour::increase()
+{
+   seconds ++;
+   if (seconds >= 60)
+   {
+      minutes++;
+      seconds -= 60;
+   }
+   if (minutes >= 60)
+   {
+      hours++;
+      minutes -= 60;
+   }
+
+   return *this;
+}
+
 hour operator-(hour &t1, hour &t2)
 {
    hour t = t1;
@@ -158,6 +175,45 @@ hour operator-(hour &t1, hour &t2)
    return t;
 }
 
+hour hour::operator-=(hour t)
+{
+   seconds -= t.seconds;
+   if (seconds < 0)
+   {
+      minutes--;
+      seconds += 60;
+   }
+
+   minutes -= t.minutes;
+   if (minutes < 0)
+   {
+      hours--;
+      minutes += 60;
+   }
+
+   hours -= t.hours;
+
+   return *this;
+}
+
+hour hour::decrease()
+{
+   seconds --;
+   if (seconds < 0)
+   {
+      minutes--;
+      seconds += 60;
+   }
+
+   if (minutes < 0)
+   {
+      hours--;
+      minutes += 60;
+   }
+
+   return *this;
+}
+
 std::ostream& operator<<(std::ostream &s, const hour &t)
 {
    s.width(2);
index 4697b37..ef02d0f 100644 (file)
@@ -4,7 +4,7 @@ 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_assign hour_parse hour_compare hour_arithmetic
 
 string_assign_SOURCES      = string_assign.cpp
 string_basics_SOURCES      = string_basics.cpp
@@ -26,3 +26,4 @@ date_today : date_check_today
 hour_assign_SOURCES        = hour_assign.cpp
 hour_parse_SOURCES         = hour_parse.cpp
 hour_compare_SOURCES       = hour_compare.cpp
+hour_arithmetic_SOURCES    = hour_arithmetic.cpp
diff --git a/test/hour_arithmetic.cpp b/test/hour_arithmetic.cpp
new file mode 100644 (file)
index 0000000..78988ba
--- /dev/null
@@ -0,0 +1,72 @@
+/*******************************************************
+ *  Unit test for the hour class
+ *
+ * test hour add and subtract expressions
+ ******************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+int main()
+{
+   hour   h0(14, 22, 45);
+   hour   period0(11, 45, 30);
+
+   hour h2;
+   h2 = h0 + period0;
+
+   std::cout << h0 << " + " << period0 << " = " << h2 << "\n";
+   assert(h2 == hour(26, 8, 15));
+
+   std::cout << h0 << " += " << period0 << " = ";
+   h0 += period0;
+   std::cout << h0 << "\n";
+   assert(h0 == hour(26, 8, 15));
+
+   // 11:05:30 - 1:20:40 =  9:44:50
+   hour h1(11, 5, 30);
+   hour period1(1, 20, 40);
+   h2 = h1 - period1;
+
+   std::cout << h1 << " - " << period1 << " = " << h2 << "\n";
+   assert(h2 == hour(9, 44, 50));
+
+   std::cout << h1 << " -= " << period1 << " = ";
+   h1 -= period1;
+   std::cout << h1 << "\n";
+   std::cout.flush();
+   assert(h1 == hour(9, 44, 50));
+
+   // The increment operator
+
+   hour h3(22, 59, 59);
+   std::cout << h3 << "++ = ";
+   h3++;
+   std::cout << h3 << "\n";
+   assert(h3 == hour(23, 0, 0));
+
+   hour h4(22, 59, 59);
+   std::cout << "++" << h4 << " = ";
+   ++h4;
+   std::cout << h4 << "\n";
+   assert(h4 == hour(23, 0, 0));
+
+   // The decrement operator
+
+   hour h5(22, 0, 0);
+   std::cout << h5 << "-- = ";
+   h5--;
+   std::cout << h5 << "\n";
+   assert(h5 == hour(21, 59, 59));
+
+   hour h6(22, 0, 0);
+   std::cout << "--" << h6 << " = ";
+   --h6;
+   std::cout << h6 << "\n";
+   assert(h6 == hour(21, 59, 59));
+
+   return 0;
+}
+
diff --git a/test/hour_arithmetic.exp b/test/hour_arithmetic.exp
new file mode 100644 (file)
index 0000000..070adfc
--- /dev/null
@@ -0,0 +1,9 @@
+14:22:45 + 11:45:30 = 26:08:15
+14:22:45 += 11:45:30 = 26:08:15
+11:05:30 - 01:20:40 = 09:44:50
+11:05:30 -= 01:20:40 = 09:44:50
+22:59:59++ = 23:00:00
+++22:59:59 = 23:00:00
+22:00:00-- = 21:59:59
+--22:00:00 = 21:59:59
+PASS hour_arithmetic (exit status: 0)