UTC - operator calculates the difference in seconds
authorArjen Baart <arjen@andromeda.nl>
Thu, 25 Jun 2020 20:43:29 +0000 (22:43 +0200)
committerArjen Baart <arjen@andromeda.nl>
Thu, 25 Jun 2020 20:43:29 +0000 (22:43 +0200)
TODO
src/date.h
src/utc.cpp
test/hour_assign.cpp
test/hour_assign.exp
test/utc_arithmetic.cpp
test/utc_arithmetic.exp

diff --git a/TODO b/TODO
index 12b3b72..0909e93 100644 (file)
--- 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
index 0449441..aa1b1c4 100644 (file)
@@ -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 &);
 
index 4bc4c36..69b109b 100644 (file)
@@ -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;
index dbaf728..2824262 100644 (file)
@@ -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;
 }
 
index 202413e..223f7a9 100644 (file)
@@ -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)
index 7006bd8..650eed9 100644 (file)
@@ -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;
 }
 
index 826046f..1f32baa 100644 (file)
@@ -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)