From a4e9d0f32fbd43a2b053f88b38c33dea9cdb000f Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Thu, 29 Aug 2019 05:16:20 +0200 Subject: [PATCH] Implemented hour addition and subtraction. --- ChangeLog | 12 +++++-- configure.ac | 2 +- src/date.h | 26 +++++++++++++++ src/datelex.c | 22 ++++++------ src/hour.cpp | 56 +++++++++++++++++++++++++++++++ test/Makefile.am | 3 +- test/hour_arithmetic.cpp | 72 ++++++++++++++++++++++++++++++++++++++++ test/hour_arithmetic.exp | 9 +++++ 8 files changed, 186 insertions(+), 16 deletions(-) create mode 100644 test/hour_arithmetic.cpp create mode 100644 test/hour_arithmetic.exp diff --git a/ChangeLog b/ChangeLog index 393ab1b..86dec94 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,16 @@ -May 30, 2019 - Release 0.1 +Aug 29, 2019 - Release 0.3 ============================ - - Extracted the classes not related to X windows from AXE - - Implemented and brushed up the String class. + - Implemented hour class Jun 22, 2019 - Release 0.2 ============================ - Added date class + +May 30, 2019 - Release 0.1 +============================ + + - Extracted the classes not related to X windows from AXE + - Implemented and brushed up the String class. + diff --git a/configure.ac b/configure.ac index 3c53ac7..5657a2b 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.69]) -AC_INIT([libACL], [0.2], [arjen@androemda.nl]) +AC_INIT([libACL], [0.3], [arjen@androemda.nl]) AM_INIT_AUTOMAKE([-Wall foreign]) AC_CONFIG_MACRO_DIRS([m4]) AC_CONFIG_FILES([Makefile src/Makefile test/Makefile doc/Makefile]) diff --git a/src/date.h b/src/date.h index 2776e12..d9d2afa 100644 --- a/src/date.h +++ b/src/date.h @@ -247,6 +247,9 @@ class hour return cm; } + hour increase(); + hour decrease(); + public: hour() @@ -316,7 +319,30 @@ public: friend hour operator+(hour &, hour &); hour operator += (hour h); + + hour operator ++ () // prefix + { + return increase(); + } + + hour operator ++ (int) // postfix + { + return increase(); + } + friend hour operator-(hour &, hour &); + hour operator -= (hour h); + + hour operator -- () // prefix + { + return decrease(); + } + + hour operator -- (int) // postfix + { + return decrease(); + } + friend std::ostream& operator<<(std::ostream &, const hour &); friend std::istream& operator>>(std::istream &, hour &); diff --git a/src/datelex.c b/src/datelex.c index bb27781..7cff26a 100644 --- a/src/datelex.c +++ b/src/datelex.c @@ -1,15 +1,15 @@ /*$Log: datelex.c,v $ -/*Revision 1.2 2002-09-28 06:58:45 arjen -/*Bugfix: conversion of an empty string to a date or hour object -/*now makes the values of such an object 0 (null) instead of giving -/*a segmentation fault. -/*The class UTC combines the date and hour classes. The most basic -/*functions of the UTC class are now implemented. -/*These include constructors and conversion to and from String objects. -/*New functions: date::proper(), hour::proper() and UTC::proper(). -/*Return true if the object holds a proper clock time and/or calendar -/*date; false if at least one value is out of range. -/* + *Revision 1.2 2002-09-28 06:58:45 arjen + *Bugfix: conversion of an empty string to a date or hour object + *now makes the values of such an object 0 (null) instead of giving + *a segmentation fault. + *The class UTC combines the date and hour classes. The most basic + *functions of the UTC class are now implemented. + *These include constructors and conversion to and from String objects. + *New functions: date::proper(), hour::proper() and UTC::proper(). + *Return true if the object holds a proper clock time and/or calendar + *date; false if at least one value is out of range. + * *Revision 1.1 2002/07/25 08:01:26 arjen *First checkin, AXE release 0.2 * diff --git a/src/hour.cpp b/src/hour.cpp index 26b7a30..347b656 100644 --- a/src/hour.cpp +++ b/src/hour.cpp @@ -135,6 +135,23 @@ hour hour::operator+=(hour t) return *this; } +hour hour::increase() +{ + seconds ++; + if (seconds >= 60) + { + minutes++; + seconds -= 60; + } + if (minutes >= 60) + { + hours++; + minutes -= 60; + } + + return *this; +} + hour operator-(hour &t1, hour &t2) { hour t = t1; @@ -158,6 +175,45 @@ hour operator-(hour &t1, hour &t2) return t; } +hour hour::operator-=(hour t) +{ + seconds -= t.seconds; + if (seconds < 0) + { + minutes--; + seconds += 60; + } + + minutes -= t.minutes; + if (minutes < 0) + { + hours--; + minutes += 60; + } + + hours -= t.hours; + + return *this; +} + +hour hour::decrease() +{ + seconds --; + if (seconds < 0) + { + minutes--; + seconds += 60; + } + + if (minutes < 0) + { + hours--; + minutes += 60; + } + + return *this; +} + std::ostream& operator<<(std::ostream &s, const hour &t) { s.width(2); diff --git a/test/Makefile.am b/test/Makefile.am index 4697b37..ef02d0f 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,7 +4,7 @@ AM_CPPFLAGS = -I../src 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_assign hour_parse hour_compare hour_arithmetic string_assign_SOURCES = string_assign.cpp string_basics_SOURCES = string_basics.cpp @@ -26,3 +26,4 @@ date_today : date_check_today hour_assign_SOURCES = hour_assign.cpp hour_parse_SOURCES = hour_parse.cpp hour_compare_SOURCES = hour_compare.cpp +hour_arithmetic_SOURCES = hour_arithmetic.cpp diff --git a/test/hour_arithmetic.cpp b/test/hour_arithmetic.cpp new file mode 100644 index 0000000..78988ba --- /dev/null +++ b/test/hour_arithmetic.cpp @@ -0,0 +1,72 @@ +/******************************************************* + * Unit test for the hour class + * + * test hour add and subtract expressions + ****************************************************** + * + */ + +#include "date.h" +#include + +int main() +{ + hour h0(14, 22, 45); + hour period0(11, 45, 30); + + hour h2; + h2 = h0 + period0; + + std::cout << h0 << " + " << period0 << " = " << h2 << "\n"; + assert(h2 == hour(26, 8, 15)); + + std::cout << h0 << " += " << period0 << " = "; + h0 += period0; + std::cout << h0 << "\n"; + assert(h0 == hour(26, 8, 15)); + + // 11:05:30 - 1:20:40 = 9:44:50 + hour h1(11, 5, 30); + hour period1(1, 20, 40); + h2 = h1 - period1; + + std::cout << h1 << " - " << period1 << " = " << h2 << "\n"; + assert(h2 == hour(9, 44, 50)); + + std::cout << h1 << " -= " << period1 << " = "; + h1 -= period1; + std::cout << h1 << "\n"; + std::cout.flush(); + assert(h1 == hour(9, 44, 50)); + + // The increment operator + + hour h3(22, 59, 59); + std::cout << h3 << "++ = "; + h3++; + std::cout << h3 << "\n"; + assert(h3 == hour(23, 0, 0)); + + hour h4(22, 59, 59); + std::cout << "++" << h4 << " = "; + ++h4; + std::cout << h4 << "\n"; + assert(h4 == hour(23, 0, 0)); + + // The decrement operator + + hour h5(22, 0, 0); + std::cout << h5 << "-- = "; + h5--; + std::cout << h5 << "\n"; + assert(h5 == hour(21, 59, 59)); + + hour h6(22, 0, 0); + std::cout << "--" << h6 << " = "; + --h6; + std::cout << h6 << "\n"; + assert(h6 == hour(21, 59, 59)); + + return 0; +} + diff --git a/test/hour_arithmetic.exp b/test/hour_arithmetic.exp new file mode 100644 index 0000000..070adfc --- /dev/null +++ b/test/hour_arithmetic.exp @@ -0,0 +1,9 @@ +14:22:45 + 11:45:30 = 26:08:15 +14:22:45 += 11:45:30 = 26:08:15 +11:05:30 - 01:20:40 = 09:44:50 +11:05:30 -= 01:20:40 = 09:44:50 +22:59:59++ = 23:00:00 +++22:59:59 = 23:00:00 +22:00:00-- = 21:59:59 +--22:00:00 = 21:59:59 +PASS hour_arithmetic (exit status: 0) -- 2.20.1