Implemented hour addition and subtraction.
[libacl.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    if (minutes == -1)
69    {
70       minutes = 0;
71    }
72    if (seconds == -1)
73    {
74       seconds = 0;
75    }
76 }
77
78 hour now()
79 {
80    long      clock;
81    struct tm   *tp;
82
83    time(&clock);
84    tp = localtime(&clock);
85    
86    return hour(tp->tm_hour, tp->tm_min, tp->tm_sec);
87 }
88
89 /*  An hour is a proper hour if it lies within the 24-hour clock. */
90
91 bool hour::proper()
92 {
93    return   hours >= 0 && hours < 24       &&
94             minutes >= 0 && minutes < 60   &&
95             seconds >= 0 && seconds < 60;
96 }
97
98 hour operator+(hour &t1, hour &t2)
99 {
100    hour t = t1;
101
102    t.seconds += t2.seconds;
103    if (t.seconds >= 60)
104    {
105       t.minutes++;
106       t.seconds -= 60;
107    }
108    t.minutes += t2.minutes;
109    if (t.minutes >= 60)
110    {
111       t.hours++;
112       t.minutes -= 60;
113    }
114    t.hours += t2.hours;
115
116    return t;
117 }
118
119 hour hour::operator+=(hour t)
120 {
121    seconds += t.seconds;
122    if (seconds >= 60)
123    {
124       minutes++;
125       seconds -= 60;
126    }
127    minutes += t.minutes;
128    if (minutes >= 60)
129    {
130       hours++;
131       minutes -= 60;
132    }
133    hours += t.hours;
134
135    return *this;
136 }
137
138 hour hour::increase()
139 {
140    seconds ++;
141    if (seconds >= 60)
142    {
143       minutes++;
144       seconds -= 60;
145    }
146    if (minutes >= 60)
147    {
148       hours++;
149       minutes -= 60;
150    }
151
152    return *this;
153 }
154
155 hour operator-(hour &t1, hour &t2)
156 {
157    hour t = t1;
158
159    t.seconds -= t2.seconds;
160    if (t.seconds < 0)
161    {
162       t.minutes--;
163       t.seconds += 60;
164    }
165
166    t.minutes -= t2.minutes;
167    if (t.minutes < 0)
168    {
169       t.hours--;
170       t.minutes += 60;
171    }
172
173    t.hours -= t2.hours;
174
175    return t;
176 }
177
178 hour hour::operator-=(hour t)
179 {
180    seconds -= t.seconds;
181    if (seconds < 0)
182    {
183       minutes--;
184       seconds += 60;
185    }
186
187    minutes -= t.minutes;
188    if (minutes < 0)
189    {
190       hours--;
191       minutes += 60;
192    }
193
194    hours -= t.hours;
195
196    return *this;
197 }
198
199 hour hour::decrease()
200 {
201    seconds --;
202    if (seconds < 0)
203    {
204       minutes--;
205       seconds += 60;
206    }
207
208    if (minutes < 0)
209    {
210       hours--;
211       minutes += 60;
212    }
213
214    return *this;
215 }
216
217 std::ostream& operator<<(std::ostream &s, const hour &t)
218 {
219    s.width(2);
220    s.fill('0');
221    s << t.hours << ":";
222    s.width(2);
223    s.fill('0');
224    s << t.minutes << ":";
225    s.width(2);
226    s.fill('0');
227    s << t.seconds;
228
229    return s;
230 }
231
232 std::istream &operator>>(std::istream &s, hour &h)
233 {
234    char c;
235    int  H, M, S;
236
237    s >> H >> c >> M >> c >> S;
238
239    h.hours   = H;
240    h.minutes = M;
241    h.seconds = S;
242
243    return s;
244 }
245
246 String hour::format(const char *fmt)
247 {
248    String  s;
249    char    buf[40];
250    struct  tm  t;
251
252    t.tm_sec = seconds;
253    t.tm_min  = minutes;
254    t.tm_hour = hours;
255
256    strftime(buf, 40, fmt, &t);
257    s = buf;
258
259    return s;
260 }
261