From: Arjen Baart Date: Tue, 8 Oct 2019 20:16:45 +0000 (+0200) Subject: Implemented class UTC. X-Git-Tag: v0.2~3 X-Git-Url: http://www.andromeda.nl/gitweb/?a=commitdiff_plain;h=272c6dd5fe365c995538c2d3d8fca2e4f28c284e;p=libacl.git Implemented class UTC. --- diff --git a/doc/Makefile.am b/doc/Makefile.am index 200bf0c..94b843e 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -7,7 +7,7 @@ SUFFIXES = .obj .eps .png .obj.eps: tgif -print -eps -color $< -XMLS = manual.xml string.xml date.xml hour.xml +XMLS = manual.xml string.xml date.xml hour.xml utc.xml IMAGES= PICTURES= diff --git a/doc/manual.xml b/doc/manual.xml index d1ce759..bbb9745 100644 --- a/doc/manual.xml +++ b/doc/manual.xml @@ -11,5 +11,7 @@ xmlns:xi="http://www.w3.org/2001/XInclude"/> + diff --git a/doc/utc.xml b/doc/utc.xml new file mode 100644 index 0000000..11b102d --- /dev/null +++ b/doc/utc.xml @@ -0,0 +1,95 @@ + +UTC - Universal Time Coordinated class + +
+#include <date.h>
+
+UTC clock;
+
+ +
+Construction and assignment + +A UTC object can be constructed from its parts or by parsing from a String. + + +The default constructor creates a default of its parts. + + + + +Parse the date and time from String s. + + + + + + +Parse the date and time from String s. + + + +
+ +
+Conversion + + +String() + + +
+ +
+Relational operations + + + + + + + + + + +
+ +
+Attributes + +
date day()
+
hour hour()
+
+ +
+Arithmetic + + + + + + + +Add one second. + + +Subtract one second. + + + +
+ +
+Stream I/O + + + +Write the date and time to the output stream in the default format. + + +Scan the input stream for a date and time. + + +
+ +
diff --git a/src/Makefile.am b/src/Makefile.am index 837681f..7e1d3cc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ lib_LTLIBRARIES = libACL.la libACL_la_SOURCES = string.cpp regex.cpp date.cpp parsedate.c dateyacc.y datelex.c \ - hour.cpp + hour.cpp utc.cpp include_HEADERS = String.h date.h diff --git a/src/date.h b/src/date.h index d9d2afa..7b6fd3b 100644 --- a/src/date.h +++ b/src/date.h @@ -386,6 +386,67 @@ public: return d.proper() && t.proper(); } + bool operator==(UTC u) + { + return d == u.d && t == u.t; + } + + bool operator!=(UTC u) + { + return d != u.d || t != u.t; + } + + bool operator<=(UTC u) + { + if (d == u.d) + { + return t <= u.t; + } + else + { + return d <= u.d; + } + } + + bool operator<(UTC u) + { + if (d == u.d) + { + return t < u.t; + } + else + { + return d < u.d; + } + } + + bool operator>=(UTC u) + { + if (d == u.d) + { + return t >= u.t; + } + else + { + return d >= u.d; + } + } + + bool operator>(UTC u) + { + if (d == u.d) + { + return t > u.t; + } + else + { + return d > u.d; + } + } + + friend UTC operator+(UTC &, UTC &); + UTC operator += (UTC h); + friend std::ostream& operator<<(std::ostream &, const UTC &); friend std::istream& operator>>(std::istream &, UTC &); diff --git a/test/Makefile.am b/test/Makefile.am index aeeb56e..b4691f5 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,7 +4,8 @@ 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_arithmetic hour_check_now + hour_assign hour_parse hour_compare hour_arithmetic hour_check_now \ + utc_assign utc_compare string_assign_SOURCES = string_assign.cpp string_basics_SOURCES = string_basics.cpp @@ -30,3 +31,5 @@ hour_arithmetic_SOURCES = hour_arithmetic.cpp 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 diff --git a/test/date_attributes.exp b/test/date_attributes.exp new file mode 100644 index 0000000..6a95164 --- /dev/null +++ b/test/date_attributes.exp @@ -0,0 +1,12 @@ +09-06-2019 Day = 9 +09-06-2019 Month = 6 +09-06-2019 Monthname = Jun +09-06-2019 Year = 2019 +09-06-2019 Leap = 0 +09-06-2020 Leap = 1 +09-06-2000 Leap = 1 +09-06-2100 Leap = 0 +Jan 2019 has 31 days. +Feb 2019 has 28 days. +Feb 2020 has 29 days. +PASS date_attributes (exit status: 0) diff --git a/test/utc_assign.cpp b/test/utc_assign.cpp new file mode 100644 index 0000000..844748f --- /dev/null +++ b/test/utc_assign.cpp @@ -0,0 +1,78 @@ +/******************************************************* + * Unit test for the UTC class + * + * test contrustor and assignment of UTC objects + ****************************************************** + * + */ + +#include "date.h" +#include + +int main() +{ + // Default contructor: all zero + UTC u0; + date d0; + hour h0; + + d0 = date(u0); + h0 = hour(u0); + + std::cout << "The default contructor makes a zero date:and time " << u0 << "\n"; + assert(d0.Day() == 0); + assert(d0.Month() == 0); + assert(d0.Year() == 0); + assert(h0.Hour() == 0); + assert(h0.Minute() == 0); + assert(h0.Second() == 0); + + date d1(15, 9, 2002); + hour h1(15, 23, 46); + + UTC u1(d1, h1); + + std::cout << "Contructor with date = 15-9-2002, hour = 15:23:46: " << u1 << "\n"; + date d2(u1); + hour h2(u1); + + assert(d2.Day() == 15); + assert(d2.Month() == 9); + assert(d2.Year() == 2002); + assert(h2.Hour() == 15); + assert(h2.Minute() == 23); + assert(h2.Second() == 46); + + String s3("23-10-2019 23:45:16"); + UTC u3(s3); + + std::cout << "Contructor from String " << s3 << " : " << u3 << "\n"; + + date d3(u3); + hour h3(u3); + + assert(d3.Day() == 23); + assert(d3.Month() == 10); + assert(d3.Year() == 2019); + assert(h3.Hour() == 23); + assert(h3.Minute() == 45); + assert(h3.Second() == 16); + + String s4("Thu Oct 3 22:38:38 CEST 2019"); + UTC u4(s4); + + std::cout << "Contructor from String " << s4 << " : " << u4 << "\n"; + + date d4(u4); + hour h4(u4); + + assert(d4.Day() == 3); + assert(d4.Month() == 10); + assert(d4.Year() == 2019); + assert(h4.Hour() == 22); + assert(h4.Minute() == 38); + assert(h4.Second() == 38); + + return 0; +} + diff --git a/test/utc_assign.exp b/test/utc_assign.exp new file mode 100644 index 0000000..54b36ea --- /dev/null +++ b/test/utc_assign.exp @@ -0,0 +1,5 @@ +The default contructor makes a zero date:and time 00-00-0 00:00:00 +Contructor with date = 15-9-2002, hour = 15:23:46: 15-09-2002 15:23:46 +Contructor from String 23-10-2019 23:45:16 : 23-10-2019 23:45:16 +Contructor from String Thu Oct 3 22:38:38 CEST 2019 : 03-10-2019 22:38:38 +PASS utc_assign (exit status: 0) diff --git a/test/utc_compare.cpp b/test/utc_compare.cpp new file mode 100644 index 0000000..b46552f --- /dev/null +++ b/test/utc_compare.cpp @@ -0,0 +1,79 @@ +/******************************************************* + * Unit test for the UTC class + * + * test UTC relational operators + ****************************************************** + * + */ + +#include "date.h" +#include + +int main() +{ + UTC u0("9-6-2019 11:22:00"); + UTC u1("9-6-2019 11:22:00"); + UTC u2("9-6-2019 11:22:01"); + UTC u3("7-8-2019 11:22:00"); + + // equal dates + + std::cout << u0 << " == " << u1 << " = " << (u0 == u1) << "\n"; + assert((u0 == u1) == true); + + std::cout << u0 << " != " << u1 << " = " << (u0 != u1) << "\n"; + assert((u0 != u1) == false); + + std::cout << u0 << " < " << u1 << " = " << (u0 < u1) << "\n"; + assert((u0 < u1) == false); + + std::cout << u0 << " <= " << u1 << " = " << (u0 <= u1) << "\n"; + assert((u0 <= u1) == true); + + std::cout << u0 << " > " << u1 << " = " << (u0 > u1) << "\n"; + assert((u0 > u1) == false); + + std::cout << u0 << " >= " << u1 << " = " << (u0 >= u1) << "\n"; + assert((u0 >= u1) == true); + + // unequal dates + + std::cout << u0 << " == " << u2 << " = " << (u0 == u2) << "\n"; + assert((u0 == u2) == false); + + std::cout << u0 << " != " << u2 << " = " << (u0 != u2) << "\n"; + assert((u0 != u2) == true); + + std::cout << u0 << " < " << u2 << " = " << (u0 < u2) << "\n"; + assert((u0 < u2) == true); + + std::cout << u0 << " <= " << u2 << " = " << (u0 <= u2) << "\n"; + assert((u0 <= u2) == true); + + std::cout << u0 << " > " << u2 << " = " << (u0 > u2) << "\n"; + assert((u0 > u2) == false); + + std::cout << u0 << " >= " << u2 << " = " << (u0 >= u2) << "\n"; + assert((u0 >= u2) == false); + + std::cout << u0 << " == " << u3 << " = " << (u0 == u3) << "\n"; + assert((u0 == u3) == false); + + std::cout << u0 << " != " << u3 << " = " << (u0 != u3) << "\n"; + assert((u0 != u3) == true); + + std::cout << u0 << " < " << u3 << " = " << (u0 < u3) << "\n"; + assert((u0 < u3) == true); + + std::cout << u0 << " <= " << u3 << " = " << (u0 <= u3) << "\n"; + assert((u0 <= u3) == true); + + std::cout << u0 << " > " << u3 << " = " << (u0 > u3) << "\n"; + assert((u0 > u3) == false); + + std::cout << u0 << " >= " << u3 << " = " << (u0 >= u3) << "\n"; + assert((u0 >= u3) == false); + + return 0; +} + diff --git a/test/utc_compare.exp b/test/utc_compare.exp new file mode 100644 index 0000000..7ca9fa2 --- /dev/null +++ b/test/utc_compare.exp @@ -0,0 +1,19 @@ +09-06-2019 11:22:00 == 09-06-2019 11:22:00 = 1 +09-06-2019 11:22:00 != 09-06-2019 11:22:00 = 0 +09-06-2019 11:22:00 < 09-06-2019 11:22:00 = 0 +09-06-2019 11:22:00 <= 09-06-2019 11:22:00 = 1 +09-06-2019 11:22:00 > 09-06-2019 11:22:00 = 0 +09-06-2019 11:22:00 >= 09-06-2019 11:22:00 = 1 +09-06-2019 11:22:00 == 09-06-2019 11:22:01 = 0 +09-06-2019 11:22:00 != 09-06-2019 11:22:01 = 1 +09-06-2019 11:22:00 < 09-06-2019 11:22:01 = 1 +09-06-2019 11:22:00 <= 09-06-2019 11:22:01 = 1 +09-06-2019 11:22:00 > 09-06-2019 11:22:01 = 0 +09-06-2019 11:22:00 >= 09-06-2019 11:22:01 = 0 +09-06-2019 11:22:00 == 07-08-2019 11:22:00 = 0 +09-06-2019 11:22:00 != 07-08-2019 11:22:00 = 1 +09-06-2019 11:22:00 < 07-08-2019 11:22:00 = 1 +09-06-2019 11:22:00 <= 07-08-2019 11:22:00 = 1 +09-06-2019 11:22:00 > 07-08-2019 11:22:00 = 0 +09-06-2019 11:22:00 >= 07-08-2019 11:22:00 = 0 +PASS utc_compare (exit status: 0)