46210c2e0b4fba1405de4ae38cba4bcaad832efb
[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.2 $
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.2  2002-09-02 06:18:20  arjen
26    Fixed some date and time conversion functions
27
28    Revision 1.1  2002/07/25 08:01:27  arjen
29    First checkin, AXE release 0.2
30
31 *****************************/
32
33 static const char *RCSID = "$Id: hour.cpp,v 1.2 2002-09-02 06:18:20 arjen Exp $";
34
35 #include <time.h>
36
37 #include "date.h"
38 #include "parsedate.h"
39
40 hour::hour(String s)
41 {
42    struct parseddate *pd;
43
44    pd = parsedate(s);
45    hours = pd->hour;
46    minutes = pd->minute;
47    seconds = pd->second;
48 }
49
50 hour now()
51 {
52    long      clock;
53    struct tm   *tp;
54
55    time(&clock);
56    tp = localtime(&clock);
57    
58    return hour(tp->tm_hour, tp->tm_min, tp->tm_sec);
59 }
60
61 hour operator+(hour &t1, hour &t2)
62 {
63    hour t = t1;
64
65    t.seconds += t2.seconds;
66    if (t.seconds >= 60)
67    {
68       t.minutes++;
69       t.seconds -= 60;
70    }
71    t.minutes += t2.minutes;
72    if (t.minutes >= 60)
73    {
74       t.hours++;
75       t.minutes -= 60;
76    }
77    t.hours += t2.hours;
78
79    return t;
80 }
81
82 hour hour::operator+=(hour t)
83 {
84    seconds += t.seconds;
85    if (seconds >= 60)
86    {
87       minutes++;
88       seconds -= 60;
89    }
90    minutes += t.minutes;
91    if (minutes >= 60)
92    {
93       hours++;
94       minutes -= 60;
95    }
96    hours += t.hours;
97
98    return *this;
99 }
100
101 hour operator-(hour &t1, hour &t2)
102 {
103    hour t = t1;
104
105    t.seconds -= t2.seconds;
106    if (t.seconds < 0)
107    {
108       t.minutes--;
109       t.seconds += 60;
110    }
111
112    t.minutes -= t2.minutes;
113    if (t.minutes < 0)
114    {
115       t.hours--;
116       t.minutes += 60;
117    }
118
119    t.hours -= t2.hours;
120
121    return t;
122 }
123
124 ostream& operator<<(ostream &s, const hour &t)
125 {
126    s.width(2);
127    s.fill('0');
128    s << t.hours << ":";
129    s.width(2);
130    s.fill('0');
131    s << t.minutes << ":";
132    s.width(2);
133    s.fill('0');
134    s << t.seconds;
135
136    return s;
137 }
138
139 istream &operator>>(istream &s, hour &h)
140 {
141    char c;
142    int  H, M, S;
143
144    s >> H >> c >> M >> c >> S;
145
146    h.hours   = H;
147    h.minutes = M;
148    h.seconds = S;
149
150    return s;
151 }
152
153 String hour::format(const char *fmt)
154 {
155    String  s;
156    char    buf[40];
157    struct  tm  t;
158
159    t.tm_sec = seconds;
160    t.tm_min  = minutes;
161    t.tm_hour = hours;
162
163    strftime(buf, 40, fmt, &t);
164    s = buf;
165
166    return s;
167 }
168