Add parameter localtime in today() and now()
[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 hour::operator+=(hour t)
127 {
128    seconds += t.seconds;
129    if (seconds >= 60)
130    {
131       minutes++;
132       seconds -= 60;
133    }
134    minutes += t.minutes;
135    if (minutes >= 60)
136    {
137       hours++;
138       minutes -= 60;
139    }
140    hours += t.hours;
141
142    return *this;
143 }
144
145 hour hour::increase()
146 {
147    seconds ++;
148    if (seconds >= 60)
149    {
150       minutes++;
151       seconds -= 60;
152    }
153    if (minutes >= 60)
154    {
155       hours++;
156       minutes -= 60;
157    }
158
159    return *this;
160 }
161
162 hour operator-(hour t1, hour t2)
163 {
164    hour t = t1;
165
166    t.seconds -= t2.seconds;
167    if (t.seconds < 0)
168    {
169       t.minutes--;
170       t.seconds += 60;
171    }
172
173    t.minutes -= t2.minutes;
174    if (t.minutes < 0)
175    {
176       t.hours--;
177       t.minutes += 60;
178    }
179
180    t.hours -= t2.hours;
181
182    return t;
183 }
184
185 hour hour::operator-=(hour t)
186 {
187    seconds -= t.seconds;
188    if (seconds < 0)
189    {
190       minutes--;
191       seconds += 60;
192    }
193
194    minutes -= t.minutes;
195    if (minutes < 0)
196    {
197       hours--;
198       minutes += 60;
199    }
200
201    hours -= t.hours;
202
203    return *this;
204 }
205
206 hour hour::decrease()
207 {
208    seconds --;
209    if (seconds < 0)
210    {
211       minutes--;
212       seconds += 60;
213    }
214
215    if (minutes < 0)
216    {
217       hours--;
218       minutes += 60;
219    }
220
221    return *this;
222 }
223
224 std::ostream& operator<<(std::ostream &s, const hour &t)
225 {
226    s.width(2);
227    s.fill('0');
228    s << t.hours << ":";
229    s.width(2);
230    s.fill('0');
231    s << t.minutes << ":";
232    s.width(2);
233    s.fill('0');
234    s << t.seconds;
235
236    return s;
237 }
238
239 std::istream &operator>>(std::istream &s, hour &h)
240 {
241    char c;
242    int  H, M, S;
243
244    s >> H >> c >> M >> c >> S;
245
246    h.hours   = H;
247    h.minutes = M;
248    h.seconds = S;
249
250    return s;
251 }
252
253 String hour::format(const char *fmt)
254 {
255    String  s;
256    char    buf[40];
257    struct  tm  t;
258
259    t.tm_sec = seconds;
260    t.tm_min  = minutes;
261    t.tm_hour = hours;
262
263    strftime(buf, 40, fmt, &t);
264    s = buf;
265
266    return s;
267 }
268