From 58e65b24d08de5f37540fabefa92e8cec144069e Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Thu, 24 Oct 2019 23:10:54 +0200 Subject: [PATCH] Added UTC add operations. --- .gitignore | 1 + depends | 1 + src/utc.cpp | 18 ++++++++++++++++++ test/Makefile.am | 3 ++- test/string_substring.exp | 5 +++++ test/utc_arithmetic.cpp | 40 +++++++++++++++++++++++++++++++++++++++ test/utc_arithmetic.exp | 4 ++++ 7 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 depends create mode 100644 test/string_substring.exp create mode 100644 test/utc_arithmetic.cpp create mode 100644 test/utc_arithmetic.exp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..70845e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Makefile.in diff --git a/depends b/depends new file mode 100644 index 0000000..8ec845b --- /dev/null +++ b/depends @@ -0,0 +1 @@ +xmldoc diff --git a/src/utc.cpp b/src/utc.cpp index e106c87..ebaef07 100644 --- a/src/utc.cpp +++ b/src/utc.cpp @@ -84,6 +84,24 @@ UTC Now() return UTC(d, t); } +UTC operator+(UTC &t1, UTC &t2) +{ + UTC t = t1; + + t.d += t2.d; + t.t += t2.t; + + return t; +} + +UTC UTC::operator+=(UTC u) +{ + d += u.d; + t += u.t; + + return *this; +} + std::ostream& operator<<(std::ostream &s, const UTC &t) { s << t.d << " " << t.t; diff --git a/test/Makefile.am b/test/Makefile.am index b4691f5..0d8369c 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -5,7 +5,7 @@ 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_arithmetic hour_check_now \ - utc_assign utc_compare + utc_assign utc_compare utc_arithmetic string_assign_SOURCES = string_assign.cpp string_basics_SOURCES = string_basics.cpp @@ -33,3 +33,4 @@ hour_check_now_SOURCES = hour_check_now.cpp hour_now : hour_check_now utc_assign_SOURCES = utc_assign.cpp utc_compare_SOURCES = utc_compare.cpp +utc_arithmetic_SOURCES = utc_arithmetic.cpp diff --git a/test/string_substring.exp b/test/string_substring.exp new file mode 100644 index 0000000..734353e --- /dev/null +++ b/test/string_substring.exp @@ -0,0 +1,5 @@ +The substring (3,5) of abcdefghijkl is defgh +Replace substring (3,5) of abcdefghijkl with 12345678 : abc12345678ijkl +Insert "12345678" into "abcdefghijkl" : abc12345678defghijkl +Remove part of "abc12345678defghijkl" : abc12345678ijkl +PASS string_substring (exit status: 0) diff --git a/test/utc_arithmetic.cpp b/test/utc_arithmetic.cpp new file mode 100644 index 0000000..7006bd8 --- /dev/null +++ b/test/utc_arithmetic.cpp @@ -0,0 +1,40 @@ +/******************************************************* + * Unit test for the UTC class + * + * test UTC add and subtract expressions + ****************************************************** + * + */ + +#include "date.h" +#include + +int main() +{ + date d0(9, 6, 2019); + hour t0; + UTC period0(date(5, 3, 6), t0); + + UTC u0(d0, t0); + UTC d1; + UTC d2; + d2 = u0 + period0; + + std::cout << u0 << " + " << period0 << " = " << d2 << "\n"; + assert(d2 == UTC(date(14, 9, 2025), t0)); + + UTC period1(date(30, 11, 3), t0); // 3 years, 11 months and 30 days + d2 = u0 + period1; + std::cout << u0 << " + " << period1 << " = " << d2 << "\n"; + assert(d2 == UTC(date(8, 6, 2023), t0)); + + d1 = UTC(date(22, 7, 2019), t0); + std::cout << d1 << " += " << period1 << " = "; + d1 += period1; + std::cout << d1 << "\n"; + std::cout.flush(); + assert(d1 == UTC(date(22, 7, 2023), t0)); + + return 0; +} + diff --git a/test/utc_arithmetic.exp b/test/utc_arithmetic.exp new file mode 100644 index 0000000..826046f --- /dev/null +++ b/test/utc_arithmetic.exp @@ -0,0 +1,4 @@ +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 +PASS utc_arithmetic (exit status: 0) -- 2.20.1