Started a test setup
[AXE.git] / src / date.h
1 /**************************************************************************
2 **  (c) Copyright 1999, Andromeda Technology & Automation
3 ***************************************************************************
4 ** MODULE INFORMATION *
5 ***********************
6 **      FILE NAME      : date.h
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 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: date.h,v $
26    Revision 1.3  2002-09-28 06:58:45  arjen
27    Bugfix: conversion of an empty string to a date or hour object
28    now makes the values of such an object 0 (null) instead of giving
29    a segmentation fault.
30    The class UTC combines the date and hour classes. The most basic
31    functions of the UTC class are now implemented.
32    These include constructors and conversion to and from String objects.
33    New functions: date::proper(), hour::proper() and UTC::proper().
34    Return true if the object holds a proper clock time and/or calendar
35    date; false if at least one value is out of range.
36
37    Revision 1.2  2002/09/02 06:18:20  arjen
38    Fixed some date and time conversion functions
39
40    Revision 1.1  2002/07/25 08:01:26  arjen
41    First checkin, AXE release 0.2
42
43 *****************************/
44
45 /* static const char *RCSID = "$Id: date.h,v 1.3 2002-09-28 06:58:45 arjen Exp $"; */
46
47 #ifndef AXE_DATE_H
48 #define AXE_DATE_H
49
50 #include "String.h"
51
52 class date
53 {
54    unsigned char  month, day;
55    short year;
56
57    long julian();
58    date add(date D);
59    date add(unsigned long days);
60
61    date subtract(unsigned long days);
62
63    friend class UTC;
64
65 public:
66
67    date()
68    {
69       month = 0;
70       day   = 0;
71       year  = 0;
72    }       
73    
74    date(unsigned  d, unsigned  m=0, short y=0)
75    {
76       day   = d;
77       month = m;
78       year  = y;
79    }
80
81    date(String s);
82
83    bool proper();   // Check wether this is a proper calendar date
84
85    friend date operator+(date, date);
86    friend date operator+(unsigned long,  date);
87    friend date operator+(date,  unsigned long);
88
89    date operator += (date D)
90    {
91       return add(D);
92    }
93
94    date operator += (unsigned long l)
95    {
96       return add(l);
97    }
98
99    friend date operator-(unsigned long,  date);
100    friend date operator-(date,  unsigned long);
101    friend long operator-(date&, date&);
102
103    date operator -= (unsigned long l)
104    {
105       return subtract(l);
106    }
107
108 //   long(date);        // Calculate nr. of days since...
109
110    int operator==(date d)
111    {
112       register int cm;
113       
114       cm = this->year - d.year;
115       cm = (cm != 0) ? cm : this->month - d.month;
116       cm = (cm != 0) ? cm : this->day   - d.day;
117       
118       return cm == 0;
119    }
120
121    int operator!=(date d)
122    {
123       register int cm;
124       
125       cm = this->year - d.year;
126       cm = (cm != 0) ? cm : this->month - d.month;
127       cm = (cm != 0) ? cm : this->day   - d.day;
128       
129       return cm != 0;
130    }             
131    
132    int operator<=(date d)
133    {
134       register int cm;
135       
136       cm = this->year - d.year;
137       cm = (cm != 0) ? cm : this->month - d.month;
138       cm = (cm != 0) ? cm : this->day   - d.day;
139       
140       return cm <= 0;
141    }             
142    
143    int operator>=(date d)
144    {
145       register int cm;
146       
147       cm = this->year - d.year;
148       cm = (cm != 0) ? cm : this->month - d.month;
149       cm = (cm != 0) ? cm : this->day   - d.day;
150       
151       return cm >= 0;
152    }             
153    
154    int operator>(date d)
155    {
156       register int cm;
157       
158       cm = this->year - d.year;
159       cm = (cm != 0) ? cm : this->month - d.month;
160       cm = (cm != 0) ? cm : this->day   - d.day;
161       
162       return cm > 0;
163    }             
164    
165    int operator<(date d)
166    {
167       register int cm;
168       
169       cm = this->year - d.year;
170       cm = (cm != 0) ? cm : this->month - d.month;
171       cm = (cm != 0) ? cm : this->day   - d.day;
172       
173       return cm < 0;
174    }
175
176    //  Attributes.
177
178    unsigned Day()
179    {
180       return day;
181    }
182
183    unsigned Month()
184    {
185       return month;
186    }
187
188    String MonthName(void);
189
190    int Year()
191    {
192       return year;
193    }
194    //  Operations.
195
196    int Leap(void);
197    unsigned DaysInMonth();
198
199 //   date operator*(double)
200 //   date& operator*=(double)
201 //   date operator/(double)
202 //   date& operator/=(double)
203
204    friend std::ostream& operator<<(std::ostream&, const date&);
205    friend std::istream& operator>>(std::istream&, date&);
206
207    String format(const char *fmt = "%F");
208 };
209
210 date today(); 
211
212 class hour
213 {
214    int   hours;
215    short minutes, seconds;
216
217    friend class UTC;
218
219 public:
220
221    hour()
222    {
223       hours = 0;
224       minutes = 0;
225       seconds = 0;
226    }
227
228    hour(int hr, short minute, short sec)
229    {
230       hours = hr;
231       minutes = minute;
232       seconds = sec;
233    }
234
235    hour(String s);
236
237    bool proper();   // Check wether this is a proper clock time
238
239    friend hour operator+(hour &, hour &);
240    hour operator += (hour h);
241    friend hour operator-(hour &, hour &);
242
243    friend std::ostream& operator<<(std::ostream &, const hour &);
244    friend std::istream& operator>>(std::istream &, hour &);
245
246    String format(const char *fmt = "%T");
247 };
248
249 hour now();
250
251 class UTC
252 {
253    hour  t;
254    date  d;
255
256 public:
257
258    UTC()
259    {
260       /* Leave everything to the default hour and date constructors */
261    }
262
263    UTC(const String s);
264    UTC(date dt, hour tm)
265    {
266       d = dt;
267       t = tm;
268    }
269
270    operator date()
271    {
272       return d;
273    }
274
275    operator hour()
276    {
277       return t;
278    }
279
280    bool proper()
281    {
282       return d.proper() && t.proper();
283    }
284
285    friend std::ostream& operator<<(std::ostream &, const UTC &);
286    friend std::istream& operator>>(std::istream &, UTC &);
287
288    String format(const char *fmt = "%F %T");
289 };
290
291 UTC Now();
292
293 #endif /* AXE_DATE_H */