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