Fixed some date and time conversion functions
[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.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 **      MODIFICATIONS   : 
22 **************************************************************************/
23
24 /*****************************
25    $Log: date.h,v $
26    Revision 1.2  2002-09-02 06:18:20  arjen
27    Fixed some date and time conversion functions
28
29    Revision 1.1  2002/07/25 08:01:26  arjen
30    First checkin, AXE release 0.2
31
32 *****************************/
33
34 /* static const char *RCSID = "$Id: date.h,v 1.2 2002-09-02 06:18:20 arjen Exp $"; */
35
36 #ifndef AXE_DATE_H
37 #define AXE_DATE_H
38
39 #include "String.h"
40
41 class date
42 {
43    unsigned char  month, day;
44    short year;
45
46    long julian();
47    date add(date D);
48    date add(unsigned long days);
49
50    date subtract(unsigned long days);
51
52 public:
53
54    date()
55    {
56       month = 0;
57       day   = 0;
58       year  = 0;
59    }       
60    
61    date(unsigned  d, unsigned  m=0, short y=0)
62    {
63       day   = d;
64       month = m;
65       year  = y;
66    }
67
68    date(String s);
69
70    friend date operator+(date, date);
71    friend date operator+(unsigned long,  date);
72    friend date operator+(date,  unsigned long);
73
74    date operator += (date D)
75    {
76       return add(D);
77    }
78
79    date operator += (unsigned long l)
80    {
81       return add(l);
82    }
83
84    friend date operator-(unsigned long,  date);
85    friend date operator-(date,  unsigned long);
86    friend long operator-(date&, date&);
87
88    date operator -= (unsigned long l)
89    {
90       return subtract(l);
91    }
92
93 //   long(date);        // Calculate nr. of days since...
94
95    int operator==(date d)
96    {
97       register int cm;
98       
99       cm = this->year - d.year;
100       cm = (cm != 0) ? cm : this->month - d.month;
101       cm = (cm != 0) ? cm : this->day   - d.day;
102       
103       return cm == 0;
104    }
105
106    int operator!=(date d)
107    {
108       register int cm;
109       
110       cm = this->year - d.year;
111       cm = (cm != 0) ? cm : this->month - d.month;
112       cm = (cm != 0) ? cm : this->day   - d.day;
113       
114       return cm != 0;
115    }             
116    
117    int operator<=(date d)
118    {
119       register int cm;
120       
121       cm = this->year - d.year;
122       cm = (cm != 0) ? cm : this->month - d.month;
123       cm = (cm != 0) ? cm : this->day   - d.day;
124       
125       return cm <= 0;
126    }             
127    
128    int operator>=(date d)
129    {
130       register int cm;
131       
132       cm = this->year - d.year;
133       cm = (cm != 0) ? cm : this->month - d.month;
134       cm = (cm != 0) ? cm : this->day   - d.day;
135       
136       return cm >= 0;
137    }             
138    
139    int operator>(date d)
140    {
141       register int cm;
142       
143       cm = this->year - d.year;
144       cm = (cm != 0) ? cm : this->month - d.month;
145       cm = (cm != 0) ? cm : this->day   - d.day;
146       
147       return cm > 0;
148    }             
149    
150    int operator<(date d)
151    {
152       register int cm;
153       
154       cm = this->year - d.year;
155       cm = (cm != 0) ? cm : this->month - d.month;
156       cm = (cm != 0) ? cm : this->day   - d.day;
157       
158       return cm < 0;
159    }
160
161    //  Attributes.
162
163    unsigned Day()
164    {
165       return day;
166    }
167
168    unsigned Month()
169    {
170       return month;
171    }
172
173    String MonthName(void);
174
175    int Year()
176    {
177       return year;
178    }
179    //  Operations.
180
181    int Leap(void);
182    unsigned DaysInMonth();
183
184 //   date operator*(double)
185 //   date& operator*=(double)
186 //   date operator/(double)
187 //   date& operator/=(double)
188
189    friend ostream& operator<<(ostream&, const date&);
190    friend istream& operator>>(istream&, date&);
191
192    String format(const char *fmt = "%F");
193 };
194
195 date today(); 
196
197 class hour
198 {
199    int   hours;
200    short minutes, seconds;
201
202 public:
203
204    hour()
205    {
206       hours = 0;
207       minutes = 0;
208       seconds = 0;
209    }
210
211    hour(int hr, short minute, short sec)
212    {
213       hours = hr;
214       minutes = minute;
215       seconds = sec;
216    }
217
218    hour(String s);
219
220    friend hour operator+(hour &, hour &);
221    hour operator += (hour h);
222    friend hour operator-(hour &, hour &);
223
224    friend ostream& operator<<(ostream &, const hour &);
225    friend istream& operator>>(istream &, hour &);
226
227    String format(const char *fmt = "%T");
228 };
229
230 hour now();
231
232 class UTC
233 {
234    hour  t;
235    date  d;
236
237 public:
238
239    UTC()
240    {
241    }
242 };
243
244 #endif /* AXE_DATE_H */