From dcb4ff319e2a3521940f9e9f532c30714385ddee Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Thu, 25 Jun 2020 22:43:29 +0200 Subject: [PATCH] UTC - operator calculates the difference in seconds --- TODO | 3 +-- src/date.h | 7 +++++++ src/utc.cpp | 11 +++++++++++ test/hour_assign.cpp | 6 ++++++ test/hour_assign.exp | 1 + test/utc_arithmetic.cpp | 13 +++++++++++++ test/utc_arithmetic.exp | 1 + 7 files changed, 40 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index 12b3b72..0909e93 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ Things to do: - String: Implement String != regex operator - String: index and rindex methods with String argument - String: format operator % like in python -- String: split and join - String: assign fstream objects to read and write a file - String: convert to and from std::sstream and std::string - regex: throw an exception when contructing a regex returns an error @@ -13,7 +12,7 @@ Things to do: - date: Parser for stream input - date: Yearday, Weekday and week number -- UTC: Convert to and from time_t, struct tm +- UTC: Convert to and from struct tm - xml: Add access to attributes - xml: Xinclude processing diff --git a/src/date.h b/src/date.h index 0449441..aa1b1c4 100644 --- a/src/date.h +++ b/src/date.h @@ -281,6 +281,11 @@ public: hour(String s); + operator long() + { + return hours * 3600 + minutes * 60 + seconds; + } + bool proper(); // Check wether this is a proper clock time // Attributes. @@ -465,6 +470,8 @@ public: friend UTC operator+(UTC &, UTC &); UTC operator += (UTC h); + friend long operator-(UTC &, UTC &); + friend std::ostream& operator<<(std::ostream &, const UTC &); friend std::istream& operator>>(std::istream &, UTC &); diff --git a/src/utc.cpp b/src/utc.cpp index 4bc4c36..69b109b 100644 --- a/src/utc.cpp +++ b/src/utc.cpp @@ -141,6 +141,17 @@ UTC UTC::operator+=(UTC u) return *this; } +long operator-(UTC &u1, UTC &u2) +{ + long days; + long seconds; + + days = u1.d - u2.d; + seconds = long(u1.t) - long(u2.t); + + return days * 24 * 60 * 60 + seconds; +} + std::ostream& operator<<(std::ostream &s, const UTC &t) { s << t.d << " " << t.t; diff --git a/test/hour_assign.cpp b/test/hour_assign.cpp index dbaf728..2824262 100644 --- a/test/hour_assign.cpp +++ b/test/hour_assign.cpp @@ -60,6 +60,12 @@ int main() assert(h5.Minute() == 23); assert(h5.Second() == 20); + long l5; + + l5 = (long)h5; + std::cout << h5 << " = " << l5 << " seconds.\n"; + assert(l5 == 5000); + return 0; } diff --git a/test/hour_assign.exp b/test/hour_assign.exp index 202413e..223f7a9 100644 --- a/test/hour_assign.exp +++ b/test/hour_assign.exp @@ -4,4 +4,5 @@ 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 +01:23:20 = 5000 seconds. PASS hour_assign (exit status: 0) diff --git a/test/utc_arithmetic.cpp b/test/utc_arithmetic.cpp index 7006bd8..650eed9 100644 --- a/test/utc_arithmetic.cpp +++ b/test/utc_arithmetic.cpp @@ -35,6 +35,19 @@ int main() std::cout.flush(); assert(d1 == UTC(date(22, 7, 2023), t0)); + // Calculate the number of seconds between 2 UTC objects. + + date d3(2, 2, 2020); + hour h3(10, 0, 0); + UTC u3(d3, h3); + date d4(4, 2, 2020); + hour h4(20, 0, 0); + UTC u4(d4, h4); + + long l4 = u4 - u3; + std::cout << "From " << u3 << " to " << u4 << " takes " << l4 << " seconds\n"; + assert(l4 == 208800); + return 0; } diff --git a/test/utc_arithmetic.exp b/test/utc_arithmetic.exp index 826046f..1f32baa 100644 --- a/test/utc_arithmetic.exp +++ b/test/utc_arithmetic.exp @@ -1,4 +1,5 @@ 09-06-2019 00:00:00 + 05-03-6 00:00:00 = 14-09-2025 00:00:00 09-06-2019 00:00:00 + 30-11-3 00:00:00 = 08-06-2023 00:00:00 22-07-2019 00:00:00 += 30-11-3 00:00:00 = 22-07-2023 00:00:00 +From 02-02-2020 10:00:00 to 04-02-2020 20:00:00 takes 208800 seconds PASS utc_arithmetic (exit status: 0) -- 2.20.1