Add parameter localtime in today() and now()
authorArjen Baart <arjen@andromeda.nl>
Sat, 16 Nov 2019 11:27:23 +0000 (12:27 +0100)
committerArjen Baart <arjen@andromeda.nl>
Sat, 16 Nov 2019 11:27:23 +0000 (12:27 +0100)
12 files changed:
ChangeLog
TODO
doc/date.xml
doc/hour.xml
src/date.cpp
src/date.h
src/hour.cpp
src/utc.cpp
test/date_check_today.cpp
test/date_today
test/hour_check_now.cpp
test/hour_now

index afa2233..aedec61 100644 (file)
--- 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 (file)
--- 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
index 6c983ff..5b6055c 100644 (file)
@@ -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.
 </item>
+<item tag="unsigned YearDay()">
+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.
+</item>
 <item tag="unsigned WeekDay()">
 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.
index d36c9f0..56d36a1 100644 (file)
@@ -97,21 +97,21 @@ Converts to a textual representation of the hour in the format "hh:mm:ss".
 <heading>Arithmetic</heading>
 
 <description>
-<item tag='hour operator+(hour &amp;t1, hour &amp;t2)'>
+<item tag='hour operator+(hour t1, hour t2)'>
   Add two hour objects, for example:
   <example>
     11:45:30 + 1:20:40 = 13:06:10
   </example>
 </item>
-<item tag='hour operator-(hour &amp;t1, hour &amp;t2)'>
+<item tag='hour operator-(hour t1, hour t2)'>
   Subtract two hour objects, for example:
   <example>
     11:05:30 - 1:20:40 =  9:44:50
   </example>
 </item>
-<item tag='hour &amp;operator+=(hour &amp;t)'>
+<item tag='hour &amp;operator+=(hour t)'>
 </item>
-<item tag='hour &amp;operator-=(hour &amp;t)'>
+<item tag='hour &amp;operator-=(hour t)'>
 </item>
 <item tag='hour &amp;operator++()'>
 Add one second.
index bf9907e..c9c59b6 100644 (file)
@@ -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);
 }
index 7b6fd3b..1927b5e 100644 (file)
@@ -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 */
index 347b656..1d426de 100644 (file)
@@ -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;
 
index ebaef07..3a5548f 100644 (file)
@@ -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);
index b47d397..fabbcbe 100644 (file)
@@ -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);
    }
 
index 3640ea4..8ffebe9 100755 (executable)
@@ -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
index e0cb6ba..3d55b1d 100644 (file)
@@ -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;
index f7a7246..edabcda 100755 (executable)
@@ -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