From 6034a2f00c2b4720e9612855b45a89edc83f0b03 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Sat, 16 Nov 2019 12:27:23 +0100 Subject: [PATCH] Add parameter localtime in today() and now() --- ChangeLog | 1 + TODO | 4 ++-- doc/date.xml | 5 +++++ doc/hour.xml | 8 ++++---- src/date.cpp | 11 +++++++++-- src/date.h | 10 +++++----- src/hour.cpp | 15 +++++++++++---- src/utc.cpp | 11 +++++++++-- test/date_check_today.cpp | 14 ++++++++++++-- test/date_today | 18 +++++++++++++++++- test/hour_check_now.cpp | 19 ++++++++++++++++--- test/hour_now | 17 ++++++++++++++++- 12 files changed, 107 insertions(+), 26 deletions(-) diff --git a/ChangeLog b/ChangeLog index afa2233..aedec61 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + - Add parameter bool localtime to now() and today() - String: Boundary checking on index and subtring operators - Ported class Integer from the Gnu libg++ library. diff --git a/TODO b/TODO index 70ec276..b29b42d 100644 --- a/TODO +++ b/TODO @@ -7,8 +7,8 @@ Things to do: - date: test parser for invalid dates and syntax errors - date: Parser for stream input -- date: Weekday and week number +- date: Yearday, Weekday and week number - UTC: Convert to and from time_t, struct tm -- Add classes: configuration, Integer, amount +- Add classes: configuration, amount diff --git a/doc/date.xml b/doc/date.xml index 6c983ff..5b6055c 100644 --- a/doc/date.xml +++ b/doc/date.xml @@ -127,6 +127,11 @@ Returns TRUE if this year is a leapyear. If this year is not a leapyear, Returns the number of days in this month, or zero if the month is not between 1 and 12. + +Calculate the day of the year, starting from January 1st. +For example, 1 means January 1st and the 22nd of February is 53. +If the date is not a proper calendar date, return 0. + Calculate the day of the week, where 1 is Monday and 7 is Sunday. If the date is not a proper calendar date, return 0. diff --git a/doc/hour.xml b/doc/hour.xml index d36c9f0..56d36a1 100644 --- a/doc/hour.xml +++ b/doc/hour.xml @@ -97,21 +97,21 @@ Converts to a textual representation of the hour in the format "hh:mm:ss". Arithmetic - + 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. diff --git a/src/date.cpp b/src/date.cpp index bf9907e..c9c59b6 100644 --- a/src/date.cpp +++ b/src/date.cpp @@ -227,13 +227,20 @@ date operator-(date d, unsigned long l) return d; } -date today() +date today(bool local) { long clock; struct tm *tp; time(&clock); - tp = localtime(&clock); + if (local) + { + tp = localtime(&clock); + } + else + { + tp = gmtime(&clock); + } return date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900); } diff --git a/src/date.h b/src/date.h index 7b6fd3b..1927b5e 100644 --- a/src/date.h +++ b/src/date.h @@ -227,7 +227,7 @@ public: String format(const char *fmt = "%F"); }; -date today(); +date today(bool localtime = true); class hour { @@ -317,7 +317,7 @@ public: return compare(h) >= 0; } - friend hour operator+(hour &, hour &); + friend hour operator+(hour, hour); hour operator += (hour h); hour operator ++ () // prefix @@ -330,7 +330,7 @@ public: return increase(); } - friend hour operator-(hour &, hour &); + friend hour operator-(hour, hour); hour operator -= (hour h); hour operator -- () // prefix @@ -350,7 +350,7 @@ public: String format(const char *fmt = "%T"); }; -hour now(); +hour now(bool localtime = true); class UTC { @@ -453,6 +453,6 @@ public: String format(const char *fmt = "%F %T"); }; -UTC Now(); +UTC Now(bool localtime = true); #endif /* AXE_DATE_H */ diff --git a/src/hour.cpp b/src/hour.cpp index 347b656..1d426de 100644 --- a/src/hour.cpp +++ b/src/hour.cpp @@ -75,13 +75,20 @@ hour::hour(String s) } } -hour now() +hour now(bool local) { long clock; struct tm *tp; time(&clock); - tp = localtime(&clock); + if (local) + { + tp = localtime(&clock); + } + else + { + tp = gmtime(&clock); + } return hour(tp->tm_hour, tp->tm_min, tp->tm_sec); } @@ -95,7 +102,7 @@ bool hour::proper() seconds >= 0 && seconds < 60; } -hour operator+(hour &t1, hour &t2) +hour operator+(hour t1, hour t2) { hour t = t1; @@ -152,7 +159,7 @@ hour hour::increase() return *this; } -hour operator-(hour &t1, hour &t2) +hour operator-(hour t1, hour t2) { hour t = t1; diff --git a/src/utc.cpp b/src/utc.cpp index ebaef07..3a5548f 100644 --- a/src/utc.cpp +++ b/src/utc.cpp @@ -68,7 +68,7 @@ UTC::UTC(String s) } } -UTC Now() +UTC Now(bool local) { long clock; struct tm *tp; @@ -76,7 +76,14 @@ UTC Now() hour t; time(&clock); - tp = localtime(&clock); + if (local) + { + tp = localtime(&clock); + } + else + { + tp = gmtime(&clock); + } t = hour(tp->tm_hour, tp->tm_min, tp->tm_sec); d = date(tp->tm_mday, tp->tm_mon+1, tp->tm_year+1900); diff --git a/test/date_check_today.cpp b/test/date_check_today.cpp index b47d397..fabbcbe 100644 --- a/test/date_check_today.cpp +++ b/test/date_check_today.cpp @@ -13,12 +13,22 @@ int main(int argc, char *argv[]) { date d0; - if (argc == 2) + if (argc >= 2) { String system_date(argv[1]); date d1(system_date); - d0 = today(); + if (argc == 3) + { + // Use UTC + d0 = today(false); + } + else + { + // Use local time + d0 = today(); + } + std::cout << "Date of today() = " << d0 << "\n"; assert(d0 == d1); } diff --git a/test/date_today b/test/date_today index 3640ea4..8ffebe9 100755 --- a/test/date_today +++ b/test/date_today @@ -3,5 +3,21 @@ STATUS=0 TODAY=`date` +echo "System time = $TODAY" ./date_check_today "$TODAY" -exit $? +RESULT=$? +if [ $STATUS == 0 ] +then + STATUS=${RESULT} +fi + +TODAY=`date -u` +echo "System time = $TODAY" +./date_check_today "$TODAY" utc +RESULT=$? +if [ $STATUS == 0 ] +then + STATUS=${RESULT} +fi + +exit $STATUS diff --git a/test/hour_check_now.cpp b/test/hour_check_now.cpp index e0cb6ba..3d55b1d 100644 --- a/test/hour_check_now.cpp +++ b/test/hour_check_now.cpp @@ -13,13 +13,26 @@ int main(int argc, char *argv[]) { hour d0; - if (argc == 2) + if (argc >= 2) { + hour one_second(0, 0, 1); String system_date(argv[1]); hour d1(system_date); + if (argc == 3) + { + // Use UTC + d0 = now(false); + } + else + { + // Use local time + d0 = now(); + } + std::cout << "Hour of now() = " << d0 << "\n"; + std::cout.flush(); - d0 = now(); - assert(d0 == d1); + // Allow for 1 second delay + assert(d0 == d1 || d0 == d1 + hour(0, 0, 1)); } return 0; diff --git a/test/hour_now b/test/hour_now index f7a7246..edabcda 100755 --- a/test/hour_now +++ b/test/hour_now @@ -3,5 +3,20 @@ STATUS=0 TODAY=`date` +echo "System time = $TODAY" ./hour_check_now "$TODAY" -exit $? +RESULT=$? +if [ $STATUS == 0 ] +then + STATUS=${RESULT} +fi + +TODAY=`date -u` +echo "System time = $TODAY" +./hour_check_now "$TODAY" utc +RESULT=$? +if [ $STATUS == 0 ] +then + STATUS=${RESULT} +fi +exit $STATUS -- 2.20.1