From: Arjen Baart Date: Sun, 25 Aug 2019 18:12:39 +0000 (+0200) Subject: Implemented class hour. X-Git-Tag: v0.2~7 X-Git-Url: http://www.andromeda.nl/gitweb/?a=commitdiff_plain;h=57637dc00d8831294d2d4395a38413744ec46472;p=libacl.git Implemented class hour. --- diff --git a/doc/hour.xml b/doc/hour.xml index e1c4cd1..d36c9f0 100644 --- a/doc/hour.xml +++ b/doc/hour.xml @@ -21,6 +21,7 @@ String objects or other hour objects. +The default value is 0 hours, 0 minutes and 0 seconds. A number of seconds. @@ -28,16 +29,19 @@ A number of seconds. - -Format is HH:MM:SS - +A time can be written in various formats, such as "1134", meaning +11 hours and 34 minutes. +The same time can be written as "11:34" or "11.34". +A number of seconds can be added in the same manner. +For example "11:34:25", "113425" or 11.34.25". - + +Assing a number of seconds. @@ -48,11 +52,11 @@ Format is HH:MM:SS - Converts to a number of seconds. +Converts to a textual representation of the hour in the format "hh:mm:ss". @@ -63,7 +67,7 @@ Converts to a number of seconds. - + @@ -80,11 +84,11 @@ Converts to a number of seconds. Attributes - + - + - + @@ -94,16 +98,26 @@ Converts to a number of seconds. + Add two hour objects, for example: + + 11:45:30 + 1:20:40 = 13:06:10 + + Subtract two hour objects, for example: + + 11:05:30 - 1:20:40 = 9:44:50 + +Add one second. +Subtract one second. diff --git a/src/Makefile.am b/src/Makefile.am index e6dcf30..837681f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,6 @@ lib_LTLIBRARIES = libACL.la -libACL_la_SOURCES = string.cpp regex.cpp date.cpp parsedate.c dateyacc.y datelex.c +libACL_la_SOURCES = string.cpp regex.cpp date.cpp parsedate.c dateyacc.y datelex.c \ + hour.cpp include_HEADERS = String.h date.h diff --git a/src/date.cpp b/src/date.cpp index 9ee5d91..bf9907e 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -259,8 +259,8 @@ date today() long date::julian(void) { - register long cent; - register int yr, mn, dy; + long cent; + int yr, mn, dy; yr = year; mn = month; diff --git a/src/date.h b/src/date.h index 338b87b..25f023a 100644 --- a/src/date.h +++ b/src/date.h @@ -129,7 +129,7 @@ public: int operator==(date d) { - register int cm; + int cm; cm = this->year - d.year; cm = (cm != 0) ? cm : this->month - d.month; @@ -140,7 +140,7 @@ public: int operator!=(date d) { - register int cm; + int cm; cm = this->year - d.year; cm = (cm != 0) ? cm : this->month - d.month; @@ -151,7 +151,7 @@ public: int operator<=(date d) { - register int cm; + int cm; cm = this->year - d.year; cm = (cm != 0) ? cm : this->month - d.month; @@ -162,7 +162,7 @@ public: int operator>=(date d) { - register int cm; + int cm; cm = this->year - d.year; cm = (cm != 0) ? cm : this->month - d.month; @@ -173,7 +173,7 @@ public: int operator>(date d) { - register int cm; + int cm; cm = this->year - d.year; cm = (cm != 0) ? cm : this->month - d.month; @@ -184,7 +184,7 @@ public: int operator<(date d) { - register int cm; + int cm; cm = this->year - d.year; cm = (cm != 0) ? cm : this->month - d.month; @@ -236,6 +236,17 @@ class hour friend class UTC; + int compare(hour h) + { + int cm; + + cm = this->hours - h.hours; + cm = (cm != 0) ? cm : this->minutes - h.minutes; + cm = (cm != 0) ? cm : this->seconds - h.seconds; + + return cm; + } + public: hour() @@ -256,6 +267,28 @@ public: bool proper(); // Check wether this is a proper clock time + // Attributes. + + short Second() + { + return seconds; + } + + short Minute() + { + return minutes; + } + + int Hour() + { + return hours; + } + + bool operator==(hour h) + { + return compare(h) == 0; + } + friend hour operator+(hour &, hour &); hour operator += (hour h); friend hour operator-(hour &, hour &); diff --git a/src/hour.cpp b/src/hour.cpp index 703c126..26b7a30 100644 --- a/src/hour.cpp +++ b/src/hour.cpp @@ -65,6 +65,14 @@ hour::hour(String s) minutes = pd->minute; seconds = pd->second; } + if (minutes == -1) + { + minutes = 0; + } + if (seconds == -1) + { + seconds = 0; + } } hour now() diff --git a/test/Makefile.am b/test/Makefile.am index c3f99c7..a7cdd2f 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -3,7 +3,8 @@ TESTS = $(check_PROGRAMS) date_today check_output 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 + date_assign date_parse date_compare date_arithmetic date_attributes date_check_today \ + hour_assign hour_parse string_assign_SOURCES = string_assign.cpp string_basics_SOURCES = string_basics.cpp @@ -21,3 +22,6 @@ date_attributes_SOURCES = date_attributes.cpp date_check_today_SOURCES = date_check_today.cpp date_today : date_check_today + +hour_assign_SOURCES = hour_assign.cpp +hour_parse_SOURCES = hour_parse.cpp diff --git a/test/hour_assign.cpp b/test/hour_assign.cpp new file mode 100644 index 0000000..8a25691 --- /dev/null +++ b/test/hour_assign.cpp @@ -0,0 +1,57 @@ +/******************************************************* + * Unit test for the hour class + * + * test contrustor and assignment of hour objects + ****************************************************** + * + */ + +#include "date.h" +#include + +int main() +{ + // Default contructor: all zero + hour h0; + + std::cout << "The default contructor makes a zero hour: \"" << h0 << "\"\n"; + assert(h0.Hour() == 0); + assert(h0.Minute() == 0); + assert(h0.Second() == 0); + + hour h1(15, 20, 45); + + std::cout << "Contructor with hour = 15 minure = 20, and second = 45: \"" << h1 << "\"\n"; + assert(h1.Hour() == 15); + assert(h1.Minute() == 20); + assert(h1.Second() == 45); + + hour h2(88, 55, 1000); + + std::cout << "Contructor with hour = 88 minute = 55, and second = 1000: \"" << h2 << "\"\n"; + assert(h2.Hour() == 88); + assert(h2.Minute() == 55); + assert(h2.Second() == 1000); + + // The copy constructor is just a bitwise copy + + hour h3(h1); + + std::cout << "Copy contructor from " << h1 << ": \"" << h3 << "\"\n"; + assert(h3.Hour() == 15); + assert(h3.Minute() == 20); + assert(h3.Second() == 45); + + // Also assignment is just a bitwise copy + + hour h4; + + h4 = h1; + std::cout << "Assigned from " << h1 << ": \"" << h4 << "\"\n"; + assert(h4.Hour() == 15); + assert(h4.Minute() == 20); + assert(h4.Second() == 45); + + return 0; +} + diff --git a/test/hour_assign.exp b/test/hour_assign.exp new file mode 100644 index 0000000..18fd34e --- /dev/null +++ b/test/hour_assign.exp @@ -0,0 +1,6 @@ +The default contructor makes a zero hour: "00:00:00" +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" +PASS hour_assign (exit status: 0) diff --git a/test/hour_parse.cpp b/test/hour_parse.cpp new file mode 100644 index 0000000..9648fe7 --- /dev/null +++ b/test/hour_parse.cpp @@ -0,0 +1,53 @@ +/******************************************************* + * Unit test for the hour class + * + * test the date parser that converts String to hour + ****************************************************** + * + */ + +#include "date.h" +#include + +int main() +{ + hour h0; + String the_time; + + + the_time = "1134"; + h0 = hour(the_time); + std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n"; + std::cout.flush(); + assert(h0 == hour(11, 34, 0)); + + the_time = "11:34"; + h0 = hour(the_time); + std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n"; + assert(h0 == hour(11, 34, 0)); + + the_time = "11.34"; + h0 = hour(the_time); + std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n"; + assert(h0 == hour(11, 34, 0)); + + the_time = "113427"; + h0 = hour(the_time); + std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n"; + assert(h0 == hour(11, 34, 27)); + + the_time = "11:34:27"; + h0 = hour(the_time); + std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n"; + assert(h0 == hour(11, 34, 27)); + + the_time = "11.34.27"; + h0 = hour(the_time); + std::cout << "String \"" << the_time << "\" converts to: \"" << h0 << "\"\n"; + assert(h0 == hour(11, 34, 27)); + + // TODO: test for invalid houts and syntax errors + + return 0; +} + diff --git a/test/hour_parse.exp b/test/hour_parse.exp new file mode 100644 index 0000000..cc9c633 --- /dev/null +++ b/test/hour_parse.exp @@ -0,0 +1,7 @@ +String "1134" converts to: "11:34:00" +String "11:34" converts to: "11:34:00" +String "11.34" converts to: "11:34:00" +String "113427" converts to: "11:34:27" +String "11:34:27" converts to: "11:34:27" +String "11.34.27" converts to: "11:34:27" +PASS hour_parse (exit status: 0)