Bugfix: conversion of an empty string to a date or hour object
[AXE.git] / src / hour.cpp
index 46210c2..703c126 100644 (file)
@@ -5,7 +5,7 @@
 ***********************
 **      FILE NAME      : hour.cpp
 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
-**      VERSION NUMBER : $Revision: 1.2 $
+**      VERSION NUMBER : $Revision: 1.3 $
 **
 **  DESCRIPTION      :  
 **
 
 /*****************************
    $Log: hour.cpp,v $
-   Revision 1.2  2002-09-02 06:18:20  arjen
+   Revision 1.3  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/02 06:18:20  arjen
    Fixed some date and time conversion functions
 
    Revision 1.1  2002/07/25 08:01:27  arjen
@@ -30,7 +41,7 @@
 
 *****************************/
 
-static const char *RCSID = "$Id: hour.cpp,v 1.2 2002-09-02 06:18:20 arjen Exp $";
+static const char *RCSID = "$Id: hour.cpp,v 1.3 2002-09-28 06:58:45 arjen Exp $";
 
 #include <time.h>
 
@@ -41,10 +52,19 @@ hour::hour(String s)
 {
    struct parseddate *pd;
 
-   pd = parsedate(s);
-   hours = pd->hour;
-   minutes = pd->minute;
-   seconds = pd->second;
+   if (~s == 0)
+   {
+      hours   = 0;
+      minutes = 0;
+      seconds = 0;
+   }
+   else
+   {
+      pd = parsedate(s);
+      hours = pd->hour;
+      minutes = pd->minute;
+      seconds = pd->second;
+   }
 }
 
 hour now()
@@ -58,6 +78,15 @@ hour now()
    return hour(tp->tm_hour, tp->tm_min, tp->tm_sec);
 }
 
+/*  An hour is a proper hour if it lies within the 24-hour clock. */
+
+bool hour::proper()
+{
+   return   hours >= 0 && hours < 24       &&
+            minutes >= 0 && minutes < 60   &&
+            seconds >= 0 && seconds < 60;
+}
+
 hour operator+(hour &t1, hour &t2)
 {
    hour t = t1;
@@ -121,7 +150,7 @@ hour operator-(hour &t1, hour &t2)
    return t;
 }
 
-ostream& operator<<(ostream &s, const hour &t)
+std::ostream& operator<<(std::ostream &s, const hour &t)
 {
    s.width(2);
    s.fill('0');
@@ -136,7 +165,7 @@ ostream& operator<<(ostream &s, const hour &t)
    return s;
 }
 
-istream &operator>>(istream &s, hour &h)
+std::istream &operator>>(std::istream &s, hour &h)
 {
    char c;
    int  H, M, S;