date: add WeekDay()
[libacl.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    date operator ++ ()        // prefix
100    {
101       return add(1);
102    }
103
104    date operator ++ (int)     // postfix
105    {
106       return add(1);
107    }
108
109    friend date operator-(unsigned long,  date);
110    friend date operator-(date,  unsigned long);
111    friend long operator-(date&, date&);
112
113    date operator -= (unsigned long l)
114    {
115       return subtract(l);
116    }
117
118    date operator -- ()        // prefix
119    {
120       return subtract(1);
121    }
122
123    date operator -- (int)     // postfix
124    {
125       return subtract(1);
126    }
127
128 //   long(date);        // Calculate nr. of days since...
129
130    int operator==(date d)
131    {
132       int cm;
133       
134       cm = this->year - d.year;
135       cm = (cm != 0) ? cm : this->month - d.month;
136       cm = (cm != 0) ? cm : this->day   - d.day;
137       
138       return cm == 0;
139    }
140
141    int operator!=(date d)
142    {
143       int cm;
144       
145       cm = this->year - d.year;
146       cm = (cm != 0) ? cm : this->month - d.month;
147       cm = (cm != 0) ? cm : this->day   - d.day;
148       
149       return cm != 0;
150    }             
151    
152    int operator<=(date d)
153    {
154       int cm;
155       
156       cm = this->year - d.year;
157       cm = (cm != 0) ? cm : this->month - d.month;
158       cm = (cm != 0) ? cm : this->day   - d.day;
159       
160       return cm <= 0;
161    }             
162    
163    int operator>=(date d)
164    {
165       int cm;
166       
167       cm = this->year - d.year;
168       cm = (cm != 0) ? cm : this->month - d.month;
169       cm = (cm != 0) ? cm : this->day   - d.day;
170       
171       return cm >= 0;
172    }             
173    
174    int operator>(date d)
175    {
176       int cm;
177       
178       cm = this->year - d.year;
179       cm = (cm != 0) ? cm : this->month - d.month;
180       cm = (cm != 0) ? cm : this->day   - d.day;
181       
182       return cm > 0;
183    }             
184    
185    int operator<(date d)
186    {
187       int cm;
188       
189       cm = this->year - d.year;
190       cm = (cm != 0) ? cm : this->month - d.month;
191       cm = (cm != 0) ? cm : this->day   - d.day;
192       
193       return cm < 0;
194    }
195
196    //  Attributes.
197
198    unsigned Day()
199    {
200       return day;
201    }
202
203    unsigned Month()
204    {
205       return month;
206    }
207
208    String MonthName(void);
209
210    int Year()
211    {
212       return year;
213    }
214    //  Operations.
215
216    int Leap(void);
217    unsigned DaysInMonth();
218    unsigned YearDay();
219    int WeekDay()
220    {
221       return julian() % 7 + 1;
222    }
223     // TODO:YearDay WeekDay WeekNumber DayName
224
225 //   date operator*(double)
226 //   date& operator*=(double)
227 //   date operator/(double)
228 //   date& operator/=(double)
229
230    friend std::ostream& operator<<(std::ostream&, const date&);
231    friend std::istream& operator>>(std::istream&, date&);
232
233    String format(const char *fmt = "%F");
234 };
235
236 date today(bool localtime = true); 
237
238 class hour
239 {
240    int   hours;
241    short minutes, seconds;
242
243    friend class UTC;
244
245    int compare(hour h)
246    {
247       int cm;
248       
249       cm = this->hours - h.hours;
250       cm = (cm != 0) ? cm : this->minutes - h.minutes;
251       cm = (cm != 0) ? cm : this->seconds - h.seconds;
252       
253       return cm;
254    }
255
256    hour increase();
257    hour decrease();
258
259 public:
260
261    hour()
262    {
263       hours = 0;
264       minutes = 0;
265       seconds = 0;
266    }
267
268    hour(int hr, short minute, short sec)
269    {
270       hours = hr;
271       minutes = minute;
272       seconds = sec;
273    }
274
275    hour(long sec)
276    {
277       hours = sec / 3600;
278       minutes = sec / 60 % 60;
279       seconds = sec % 60;
280    }
281
282    hour(String s);
283
284    bool proper();   // Check wether this is a proper clock time
285
286    //  Attributes.
287
288    short Second()
289    {
290       return seconds;
291    }
292
293    short Minute()
294    {
295       return minutes;
296    }
297
298    int Hour()
299    {
300       return hours;
301    }
302
303    bool operator==(hour h)
304    {
305       return compare(h) == 0;
306    }
307
308    bool operator!=(hour h)
309    {
310       return compare(h) != 0;
311    }
312
313    bool operator<(hour h)
314    {
315       return compare(h) < 0;
316    }
317
318    bool operator>(hour h)
319    {
320       return compare(h) > 0;
321    }
322
323    bool operator<=(hour h)
324    {
325       return compare(h) <= 0;
326    }
327
328    bool operator>=(hour h)
329    {
330       return compare(h) >= 0;
331    }
332
333    friend hour operator+(hour, hour);
334    friend hour operator+(hour, long);
335    hour operator += (hour h);
336
337    hour operator ++ ()        // prefix
338    {
339       return increase();
340    }
341
342    hour operator ++ (int)     // postfix
343    {
344       return increase();
345    }
346
347    friend hour operator-(hour, hour);
348    friend hour operator-(hour, long);
349    hour operator -= (hour h);
350
351    hour operator -- ()        // prefix
352    {
353       return decrease();
354    }
355
356    hour operator -- (int)     // postfix
357    {
358       return decrease();
359    }
360
361
362    friend std::ostream& operator<<(std::ostream &, const hour &);
363    friend std::istream& operator>>(std::istream &, hour &);
364
365    String format(const char *fmt = "%T");
366 };
367
368 hour now(bool localtime = true);
369
370 class UTC
371 {
372    hour  t;
373    date  d;
374
375 public:
376
377    UTC()
378    {
379       /* Leave everything to the default hour and date constructors */
380    }
381
382    UTC(const String s);
383    UTC(date dt, hour tm)
384    {
385       d = dt;
386       t = tm;
387    }
388
389    operator date()
390    {
391       return d;
392    }
393
394    operator hour()
395    {
396       return t;
397    }
398
399    bool proper()
400    {
401       return d.proper() && t.proper();
402    }
403
404    bool operator==(UTC u)
405    {
406       return d == u.d && t == u.t;
407    }
408
409    bool operator!=(UTC u)
410    {
411       return d != u.d || t != u.t;
412    }
413
414    bool operator<=(UTC u)
415    {
416       if (d == u.d)
417       {
418          return t <= u.t;
419       }
420       else
421       {
422          return d <= u.d;
423       }
424    }
425
426    bool operator<(UTC u)
427    {
428       if (d == u.d)
429       {
430          return t < u.t;
431       }
432       else
433       {
434          return d < u.d;
435       }
436    }
437
438    bool operator>=(UTC u)
439    {
440       if (d == u.d)
441       {
442          return t >= u.t;
443       }
444       else
445       {
446          return d >= u.d;
447       }
448    }
449
450    bool operator>(UTC u)
451    {
452       if (d == u.d)
453       {
454          return t > u.t;
455       }
456       else
457       {
458          return d > u.d;
459       }
460    }
461
462    friend UTC operator+(UTC &, UTC &);
463    UTC operator += (UTC h);
464
465    friend std::ostream& operator<<(std::ostream &, const UTC &);
466    friend std::istream& operator>>(std::istream &, UTC &);
467
468    String format(const char *fmt = "%F %T");
469 };
470
471 UTC Now(bool localtime = true);
472
473 #endif /* AXE_DATE_H */