hour: Add and subtract a number of seconds
authorArjen Baart <arjen@andromeda.nl>
Sat, 23 Nov 2019 21:41:57 +0000 (22:41 +0100)
committerArjen Baart <arjen@andromeda.nl>
Sat, 23 Nov 2019 21:41:57 +0000 (22:41 +0100)
ChangeLog
src/date.h
src/hour.cpp
test/hour_arithmetic.cpp
test/hour_arithmetic.exp
test/hour_assign.cpp
test/hour_assign.exp

index aedec61..169770f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+  - hour: Add and subtract a number of seconds.
   - Add parameter bool localtime to now() and today()
   - String: Boundary checking on index and subtring operators
   - Ported class Integer from the Gnu libg++ library.
index 2a7f3e1..7fa59c8 100644 (file)
@@ -268,6 +268,13 @@ public:
       seconds = sec;
    }
 
+   hour(long sec)
+   {
+      hours = sec / 3600;
+      minutes = sec / 60 % 60;
+      seconds = sec % 60;
+   }
+
    hour(String s);
 
    bool proper();   // Check wether this is a proper clock time
@@ -320,6 +327,7 @@ public:
    }
 
    friend hour operator+(hour, hour);
+   friend hour operator+(hour, long);
    hour operator += (hour h);
 
    hour operator ++ ()        // prefix
@@ -333,6 +341,7 @@ public:
    }
 
    friend hour operator-(hour, hour);
+   friend hour operator-(hour, long);
    hour operator -= (hour h);
 
    hour operator -- ()        // prefix
index 1d426de..7c762b5 100644 (file)
@@ -123,6 +123,18 @@ hour operator+(hour t1, hour t2)
    return t;
 }
 
+hour operator+(hour t1, long sec)
+{
+   long s;   // Seconds
+
+   s = t1.hours * 3600 + t1.minutes * 60 + t1.seconds;
+   s += sec;
+
+   hour t(s);
+
+   return t;
+}
+
 hour hour::operator+=(hour t)
 {
    seconds += t.seconds;
@@ -182,6 +194,18 @@ hour operator-(hour t1, hour t2)
    return t;
 }
 
+hour operator-(hour t1, long sec)
+{
+   long s;   // Seconds
+
+   s = t1.hours * 3600 + t1.minutes * 60 + t1.seconds;
+   s -= sec;
+
+   hour t(s);
+
+   return t;
+}
+
 hour hour::operator-=(hour t)
 {
    seconds -= t.seconds;
index 78988ba..0dee4ea 100644 (file)
@@ -67,6 +67,36 @@ int main()
    std::cout << h6 << "\n";
    assert(h6 == hour(21, 59, 59));
 
+   // Adding an integer number of seconds
+
+   hour   h7(14, 22, 45);
+   long   s7 = 7000;
+   hour   h8;
+
+   h8 = h7 + s7;
+   std::cout << h7 << " + " << s7 << " seconds = " << h8 << "\n";
+   assert(h8 == hour(16, 19, 25));
+
+   std::cout << h7 << " += " << s7 << " seconds = ";
+   h7 += s7;
+   std::cout << h7 << "\n";
+   assert(h7 == hour(16, 19, 25));
+
+   // Subtracting an integer number of seconds
+
+   hour   h11(14, 22, 45);
+   long   s11 = 7000;
+   hour   h12;
+
+   h12 = h11 - s11;
+   std::cout << h11 << " - " << s11 << " seconds = " << h12 << "\n";
+   assert(h12 == hour(12, 26, 05));
+
+   std::cout << h11 << " -= " << s11 << " seconds = ";
+   h11 -= s11;
+   std::cout << h11 << "\n";
+   assert(h11 == hour(12, 26, 05));
+
    return 0;
 }
 
index 070adfc..e811412 100644 (file)
@@ -6,4 +6,8 @@
 ++22:59:59 = 23:00:00
 22:00:00-- = 21:59:59
 --22:00:00 = 21:59:59
+14:22:45 + 7000 seconds = 16:19:25
+14:22:45 += 7000 seconds = 16:19:25
+14:22:45 - 7000 seconds = 12:26:05
+14:22:45 -= 7000 seconds = 12:26:05
 PASS hour_arithmetic (exit status: 0)
index 8a25691..dbaf728 100644 (file)
@@ -52,6 +52,14 @@ int main()
    assert(h4.Minute() == 20);
    assert(h4.Second() == 45);
 
+   int seconds = 5000;
+   hour h5(seconds);
+
+   std::cout << seconds << " seconds = " << h5 << "\n";
+   assert(h5.Hour() == 1);
+   assert(h5.Minute() == 23);
+   assert(h5.Second() == 20);
+
    return 0;
 }
 
index 18fd34e..202413e 100644 (file)
@@ -3,4 +3,5 @@ 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"
+5000 seconds = 01:23:20
 PASS hour_assign (exit status: 0)