Started a test setup
[AXE.git] / src / hour.cpp
1 /**************************************************************************
2 **  (c) Copyright 1999, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : hour.cpp
7 **      SYSTEM NAME    : AXE - Andromeda X-windows Encapsulation
8 **      VERSION NUMBER : $Revision: 1.3 $
9 **
10 **  DESCRIPTION      :  
11 **
12 **  EXPORTED OBJECTS : 
13 **  LOCAL    OBJECTS : 
14 **  MODULES  USED    :
15 ***************************************************************************
16 **  ADMINISTRATIVE INFORMATION *
17 ********************************
18 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
19 **      CREATION DATE   : Feb 06, 1998
20 **      LAST UPDATE     : Oct 16, 1999
21 **************************************************************************/
22
23 /*****************************
24    $Log: hour.cpp,v $
25    Revision 1.3  2002-09-28 06:58:45  arjen
26    Bugfix: conversion of an empty string to a date or hour object
27    now makes the values of such an object 0 (null) instead of giving
28    a segmentation fault.
29    The class UTC combines the date and hour classes. The most basic
30    functions of the UTC class are now implemented.
31    These include constructors and conversion to and from String objects.
32    New functions: date::proper(), hour::proper() and UTC::proper().
33    Return true if the object holds a proper clock time and/or calendar
34    date; false if at least one value is out of range.
35
36    Revision 1.2  2002/09/02 06:18:20  arjen
37    Fixed some date and time conversion functions
38
39    Revision 1.1  2002/07/25 08:01:27  arjen
40    First checkin, AXE release 0.2
41
42 *****************************/
43
44 static const char *RCSID = "$Id: hour.cpp,v 1.3 2002-09-28 06:58:45 arjen Exp $";
45
46 #include <time.h>
47
48 #include "date.h"
49 #include "parsedate.h"
50
51 hour::hour(String s)
52 {
53    struct parseddate *pd;
54
55    if (~s == 0)
56    {
57       hours   = 0;
58       minutes = 0;
59       seconds = 0;
60    }
61    else
62    {
63       pd = parsedate(s);
64       hours = pd->hour;
65       minutes = pd->minute;
66       seconds = pd->second;
67    }
68 }
69
70 hour now()
71 {
72    long      clock;
73    struct tm   *tp;
74
75    time(&clock);
76    tp = localtime(&clock);
77    
78    return hour(tp->tm_hour, tp->tm_min, tp->tm_sec);
79 }
80
81 /*  An hour is a proper hour if it lies within the 24-hour clock. */
82
83 bool hour::proper()
84 {
85    return   hours >= 0 && hours < 24       &&
86             minutes >= 0 && minutes < 60   &&
87             seconds >= 0 && seconds < 60;
88 }
89
90 hour operator+(hour &t1, hour &t2)
91 {
92    hour t = t1;
93
94    t.seconds += t2.seconds;
95    if (t.seconds >= 60)
96    {
97       t.minutes++;
98       t.seconds -= 60;
99    }
100    t.minutes += t2.minutes;
101    if (t.minutes >= 60)
102    {
103       t.hours++;
104       t.minutes -= 60;
105    }
106    t.hours += t2.hours;
107
108    return t;
109 }
110
111 hour hour::operator+=(hour t)
112 {
113    seconds += t.seconds;
114    if (seconds >= 60)
115    {
116       minutes++;
117       seconds -= 60;
118    }
119    minutes += t.minutes;
120    if (minutes >= 60)
121    {
122       hours++;
123       minutes -= 60;
124    }
125    hours += t.hours;
126
127    return *this;
128 }
129
130 hour operator-(hour &t1, hour &t2)
131 {
132    hour t = t1;
133
134    t.seconds -= t2.seconds;
135    if (t.seconds < 0)
136    {
137       t.minutes--;
138       t.seconds += 60;
139    }
140
141    t.minutes -= t2.minutes;
142    if (t.minutes < 0)
143    {
144       t.hours--;
145       t.minutes += 60;
146    }
147
148    t.hours -= t2.hours;
149
150    return t;
151 }
152
153 std::ostream& operator<<(std::ostream &s, const hour &t)
154 {
155    s.width(2);
156    s.fill('0');
157    s << t.hours << ":";
158    s.width(2);
159    s.fill('0');
160    s << t.minutes << ":";
161    s.width(2);
162    s.fill('0');
163    s << t.seconds;
164
165    return s;
166 }
167
168 std::istream &operator>>(std::istream &s, hour &h)
169 {
170    char c;
171    int  H, M, S;
172
173    s >> H >> c >> M >> c >> S;
174
175    h.hours   = H;
176    h.minutes = M;
177    h.seconds = S;
178
179    return s;
180 }
181
182 String hour::format(const char *fmt)
183 {
184    String  s;
185    char    buf[40];
186    struct  tm  t;
187
188    t.tm_sec = seconds;
189    t.tm_min  = minutes;
190    t.tm_hour = hours;
191
192    strftime(buf, 40, fmt, &t);
193    s = buf;
194
195    return s;
196 }
197