From 4454afc0337167420f968ecf05a05d9062308fa3 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Sat, 23 Nov 2019 22:41:57 +0100 Subject: [PATCH] hour: Add and subtract a number of seconds --- ChangeLog | 1 + src/date.h | 9 +++++++++ src/hour.cpp | 24 ++++++++++++++++++++++++ test/hour_arithmetic.cpp | 30 ++++++++++++++++++++++++++++++ test/hour_arithmetic.exp | 4 ++++ test/hour_assign.cpp | 8 ++++++++ test/hour_assign.exp | 1 + 7 files changed, 77 insertions(+) diff --git a/ChangeLog b/ChangeLog index aedec61..169770f 100644 --- 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. diff --git a/src/date.h b/src/date.h index 2a7f3e1..7fa59c8 100644 --- a/src/date.h +++ b/src/date.h @@ -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 diff --git a/src/hour.cpp b/src/hour.cpp index 1d426de..7c762b5 100644 --- a/src/hour.cpp +++ b/src/hour.cpp @@ -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; diff --git a/test/hour_arithmetic.cpp b/test/hour_arithmetic.cpp index 78988ba..0dee4ea 100644 --- a/test/hour_arithmetic.cpp +++ b/test/hour_arithmetic.cpp @@ -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; } diff --git a/test/hour_arithmetic.exp b/test/hour_arithmetic.exp index 070adfc..e811412 100644 --- a/test/hour_arithmetic.exp +++ b/test/hour_arithmetic.exp @@ -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) diff --git a/test/hour_assign.cpp b/test/hour_assign.cpp index 8a25691..dbaf728 100644 --- a/test/hour_assign.cpp +++ b/test/hour_assign.cpp @@ -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; } diff --git a/test/hour_assign.exp b/test/hour_assign.exp index 18fd34e..202413e 100644 --- a/test/hour_assign.exp +++ b/test/hour_assign.exp @@ -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) -- 2.11.0