7fd59977 |
1 | |
2 | // ------------------------------------------------------------- |
3 | // |
4 | // Created by J.P BOUDIER J.P. TIRAULT Jan,5th 1993 |
5 | // C matra datavision 1993 |
6 | // |
7 | // Date class implementation. |
8 | // |
9 | // Updated : |
10 | // |
11 | // ------------------------------------------------------------- |
12 | #include <Quantity_DateDefinitionError.hxx> |
13 | #include <Standard_OutOfRange.hxx> |
14 | #include <Quantity_Date.ixx> |
15 | |
16 | static int month_table[12] = { |
17 | 31, // January |
18 | 28, // February |
19 | 31, // March |
20 | 30, // April |
21 | 31, // May |
22 | 30, // June |
23 | 31, // July |
24 | 31, // August |
25 | 30, // September |
26 | 31, // October |
27 | 30, // November |
28 | 31}; // December |
29 | |
30 | static int SecondsByYear = 365 * 24 * 3600 ; // Normal Year |
31 | static int SecondsByLeapYear = 366 * 24 * 3600 ; // Leap Year |
32 | |
33 | // ----------------------------------------- |
34 | // Initialize a date to January,1 1979 00:00 |
35 | // ----------------------------------------- |
36 | |
37 | Quantity_Date::Quantity_Date(): mySec(0),myUSec(0) {} |
38 | |
39 | |
40 | |
41 | // ----------------------------------------------------------- |
42 | // IsValid : Checks the validity of a date |
43 | // This is the complete way month, day, year, ... micro second |
44 | // ----------------------------------------------------------- |
45 | |
46 | |
47 | Standard_Boolean Quantity_Date::IsValid(const Standard_Integer mm, |
48 | const Standard_Integer dd, |
49 | const Standard_Integer yy, |
50 | const Standard_Integer hh, |
51 | const Standard_Integer mn, |
52 | const Standard_Integer ss, |
53 | const Standard_Integer mis, |
54 | const Standard_Integer mics){ |
55 | |
56 | if (mm < 1 || mm > 12) return Standard_False; |
57 | |
58 | |
59 | if (yy < 1979 ) return Standard_False; |
60 | |
61 | |
62 | if ( Quantity_Date::IsLeap (yy) ) month_table[1] = 29; |
63 | else month_table[1] = 28; |
64 | |
65 | if (dd < 1 || dd > month_table[mm-1]) return Standard_False; |
66 | |
67 | |
68 | if (hh < 0 || hh > 23) return Standard_False; |
69 | |
70 | |
71 | if (mn < 0 || mn > 59) return Standard_False; |
72 | |
73 | |
74 | if (ss < 0 || ss > 59) return Standard_False; |
75 | |
76 | |
77 | if (mis < 0 || mis > 999) return Standard_False; |
78 | |
79 | |
80 | if (mics < 0 || mics > 999) return Standard_False; |
81 | |
82 | return Standard_True; |
83 | |
84 | } |
85 | |
86 | // ----------------------------------------------------------- |
87 | // Initialize a Date : |
88 | // This is the complete way month, day, year, ... micro second |
89 | // ----------------------------------------------------------- |
90 | |
91 | |
92 | Quantity_Date::Quantity_Date(const Standard_Integer mm, |
93 | const Standard_Integer dd, |
94 | const Standard_Integer yy, |
95 | const Standard_Integer hh, |
96 | const Standard_Integer mn, |
97 | const Standard_Integer ss, |
98 | const Standard_Integer mis, |
99 | const Standard_Integer mics){ |
100 | |
101 | SetValues (mm,dd,yy,hh,mn,ss,mis,mics); |
102 | } |
103 | |
104 | // ------------------------------------------------------------ |
105 | // Set values of a Date : |
106 | // This is the complete way month, day, year, ... micro second |
107 | // ------------------------------------------------------------ |
108 | |
109 | void Quantity_Date::SetValues(const Standard_Integer mm, |
110 | const Standard_Integer dd, |
111 | const Standard_Integer yy, |
112 | const Standard_Integer hh, |
113 | const Standard_Integer mn, |
114 | const Standard_Integer ss, |
115 | const Standard_Integer mis, |
116 | const Standard_Integer mics){ |
117 | |
118 | Standard_Integer i; |
119 | |
120 | if ( ! Quantity_Date::IsValid (mm,dd,yy,hh,mn,ss,mis,mics)) |
121 | Quantity_DateDefinitionError::Raise( |
122 | "Quantity_Date::Quantity_Date invalid parameters"); |
123 | |
124 | |
125 | if ( Quantity_Date::IsLeap (yy) ) month_table[1] = 29; |
126 | else month_table[1] = 28; |
127 | |
128 | mySec = 0; |
129 | myUSec = 0; |
130 | for(i = 1979; i < yy; i++) { |
131 | if ( ! Quantity_Date::IsLeap (i) ) mySec += SecondsByYear; |
132 | else mySec += SecondsByLeapYear; |
133 | } |
134 | |
135 | for(i = 1; i< mm; i++) { |
136 | mySec += month_table[i-1] * 3600 * 24 ; |
137 | } |
138 | |
139 | |
140 | mySec += 3600 * 24 * (dd-1); |
141 | |
142 | mySec += 3600 * hh; |
143 | |
144 | mySec += 60 * mn; |
145 | |
146 | mySec += ss; |
147 | |
148 | myUSec += mis * 1000; |
149 | |
150 | myUSec += mics; |
151 | |
152 | } |
153 | |
154 | |
155 | // --------------------------------------------- |
156 | // Values : Returns the values of a date |
157 | // ~~~~~~ |
158 | // --------------------------------------------- |
159 | |
160 | void Quantity_Date::Values(Standard_Integer& mm, |
161 | Standard_Integer& dd, |
162 | Standard_Integer& yy, |
163 | Standard_Integer& hh, |
164 | Standard_Integer& mn, |
165 | Standard_Integer& ss, |
166 | Standard_Integer& mis, |
167 | Standard_Integer& mics)const{ |
168 | |
169 | |
170 | Standard_Integer i,carry; |
171 | |
172 | |
173 | for(yy = 1979, carry = mySec ;; yy++) { |
174 | if ( ! Quantity_Date::IsLeap (yy) ) |
175 | { |
176 | month_table[1] = 28; // normal year |
177 | if (carry >= SecondsByYear ) carry -= SecondsByYear; |
178 | else break; |
179 | } |
180 | else |
181 | { |
182 | month_table[1] = 29; // Leap year |
183 | if (carry >= SecondsByLeapYear) carry -= SecondsByLeapYear; |
184 | else break; |
185 | } |
186 | } |
187 | |
188 | |
189 | for(mm = 1 ; ; mm++) { |
190 | i = month_table[mm-1] * 3600 * 24; |
191 | if ( carry >= i ) carry -= i; |
192 | else break; |
193 | } |
194 | |
195 | i = 3600 * 24; |
196 | for(dd = 1 ; ; dd++) { |
197 | if ( carry >= i ) carry -= i; |
198 | else break; |
199 | } |
200 | |
201 | for(hh = 0 ; ; hh++) { |
202 | if ( carry >= 3600 ) carry -= 3600; |
203 | else break; |
204 | } |
205 | |
206 | for(mn = 0 ; ; mn++) { |
207 | if ( carry >= 60 ) carry -= 60; |
208 | else break; |
209 | } |
210 | |
211 | ss = carry; |
212 | |
213 | mis = myUSec / 1000; |
214 | mics = myUSec - ( mis * 1000); |
215 | } |
216 | |
217 | |
218 | // --------------------------------------------------------------------- |
219 | // Difference : Subtract a date to a given date; the result is a period |
220 | // ~~~~~~~~~~ of time |
221 | // --------------------------------------------------------------------- |
222 | |
223 | Quantity_Period Quantity_Date::Difference(const Quantity_Date& OtherDate){ |
224 | |
225 | Standard_Integer i1,i2; |
226 | |
227 | if (mySec == 0 && myUSec == 0) |
228 | { |
229 | i1 = OtherDate.mySec; |
230 | i2 = OtherDate.myUSec; |
231 | } |
232 | else { |
233 | i1 = mySec - OtherDate.mySec ; |
234 | i2 = myUSec - OtherDate.myUSec; |
235 | } |
236 | |
237 | if ( i1 >= 0 && i2 < 0 ) { |
238 | i1--; |
239 | i2 = 1000000 + i2 ; |
240 | } |
241 | else if ( i1 <0 && i2 >= 0 ) { |
242 | i1 = Abs(i1); |
243 | if ( i2 > 0 ){ |
244 | i1--; |
245 | i2 = 1000000 - i2 ; |
246 | } |
247 | } |
248 | else if ( i1 <0 && i2 < 0 ) { |
249 | i1 = Abs(i1); |
250 | i2 = Abs(i2); |
251 | } |
252 | |
253 | Quantity_Period result ( i1 , i2 ); |
254 | |
255 | return (result); |
256 | } |
257 | |
258 | |
259 | // ------------------------------------------------------------------ |
260 | // Subtract : subtracts a period to a date and returns a date. |
261 | // ~~~~~~~~ |
262 | // ------------------------------------------------------------------ |
263 | |
264 | Quantity_Date Quantity_Date::Subtract(const Quantity_Period& During){ |
265 | |
266 | Standard_Integer ss,mics; |
267 | Quantity_Date result; |
268 | result.mySec = mySec; |
269 | result.myUSec = myUSec; |
270 | During.Values (ss,mics); |
271 | |
272 | result.mySec -= ss; |
273 | result.myUSec -= mics; |
274 | |
275 | if ( result.mySec >= 0 && result.myUSec < 0 ) { |
276 | result.mySec--; |
277 | result.myUSec = 1000000 + result.myUSec ; |
278 | } |
279 | |
280 | |
281 | if ( result.mySec <0 ) |
282 | Quantity_DateDefinitionError::Raise( |
283 | "Quantity_Date::Subtract : The result date is anterior to Jan,1 1979"); |
284 | |
285 | return (result); |
286 | |
287 | } |
288 | |
289 | |
290 | // ---------------------------------------------------------------------- |
291 | // Add : Adds a period of time to a date |
292 | // ~~~ |
293 | // ---------------------------------------------------------------------- |
294 | Quantity_Date Quantity_Date::Add(const Quantity_Period& During){ |
295 | |
296 | Quantity_Date result; |
297 | During.Values (result.mySec,result.myUSec); |
298 | result.mySec += mySec; |
299 | result.myUSec += myUSec; |
300 | if ( result.myUSec >= 1000000 ) { |
301 | result.mySec++; |
302 | result.myUSec -= 1000000; |
303 | } |
304 | return (result); |
305 | } |
306 | |
307 | |
308 | // ---------------------------------------------------------------------- |
309 | // Year : Return the year of a date |
310 | // ~~~~ |
311 | // ---------------------------------------------------------------------- |
312 | Standard_Integer Quantity_Date::Year(){ |
313 | Standard_Integer dummy, year; |
314 | Values(dummy, dummy, year, dummy, dummy, dummy, dummy, dummy); |
315 | return (year); |
316 | } |
317 | |
318 | |
319 | // ---------------------------------------------------------------------- |
320 | // Month : Return the month of a date |
321 | // ~~~~~ |
322 | // ---------------------------------------------------------------------- |
323 | Standard_Integer Quantity_Date::Month(){ |
324 | Standard_Integer dummy, month; |
325 | Values(month, dummy, dummy, dummy, dummy, dummy, dummy, dummy); |
326 | return(month); |
327 | } |
328 | |
329 | // ---------------------------------------------------------------------- |
330 | // Day : Return the day of a date |
331 | // ~~~ |
332 | // ---------------------------------------------------------------------- |
333 | |
334 | Standard_Integer Quantity_Date::Day(){ |
335 | Standard_Integer dummy, day; |
336 | Values(dummy, day, dummy, dummy, dummy, dummy, dummy, dummy); |
337 | return(day); |
338 | } |
339 | |
340 | // ---------------------------------------------------------------------- |
341 | // hour : Return the hour of a date |
342 | // ~~~~ |
343 | // ---------------------------------------------------------------------- |
344 | |
345 | Standard_Integer Quantity_Date::Hour(){ |
346 | Standard_Integer dummy, hour; |
347 | Values(dummy, dummy, dummy, hour, dummy, dummy, dummy, dummy); |
348 | return(hour); |
349 | } |
350 | |
351 | // ---------------------------------------------------------------------- |
352 | // Minute : Return the minute of a date |
353 | // ~~~~~~ |
354 | // ---------------------------------------------------------------------- |
355 | |
356 | Standard_Integer Quantity_Date::Minute(){ |
357 | Standard_Integer dummy, min; |
358 | Values(dummy, dummy, dummy, dummy, min, dummy, dummy, dummy); |
359 | return(min); |
360 | } |
361 | |
362 | // ---------------------------------------------------------------------- |
363 | // Second : Return the second of a date |
364 | // ~~~~~~ |
365 | // ---------------------------------------------------------------------- |
366 | |
367 | Standard_Integer Quantity_Date::Second(){ |
368 | Standard_Integer dummy, sec; |
369 | Values(dummy, dummy, dummy, dummy, dummy, sec , dummy, dummy); |
370 | return(sec); |
371 | } |
372 | |
373 | // ---------------------------------------------------------------------- |
374 | // millisecond : Return the millisecond of a date |
375 | // ~~~~~~~~~~~ |
376 | // ---------------------------------------------------------------------- |
377 | |
378 | Standard_Integer Quantity_Date::MilliSecond(){ |
379 | Standard_Integer dummy, msec; |
380 | Values(dummy, dummy, dummy, dummy, dummy, dummy, msec, dummy); |
381 | return(msec); |
382 | } |
383 | |
384 | // ---------------------------------------------------------------------- |
385 | // Day : Return the day of a date |
386 | // ~~~ |
387 | // ---------------------------------------------------------------------- |
388 | |
389 | Standard_Integer Quantity_Date::MicroSecond(){ |
390 | Standard_Integer dummy, msec; |
391 | Values(dummy, dummy, dummy, dummy, dummy, dummy, dummy, msec); |
392 | return(msec); |
393 | } |
394 | |
395 | // ---------------------------------------------------------------------- |
396 | // IsEarlier : Return true if the date is earlier than an other date |
397 | // ~~~~~~~~~ |
398 | // ---------------------------------------------------------------------- |
399 | |
400 | Standard_Boolean Quantity_Date::IsEarlier(const Quantity_Date& other)const{ |
401 | if (mySec < other.mySec) return Standard_True; |
402 | else if (mySec > other.mySec) return Standard_False; |
403 | else return ( ( myUSec < other.myUSec ) ? Standard_True : Standard_False); |
404 | } |
405 | |
406 | // ---------------------------------------------------------------------- |
407 | // IsLater : Return true if the date is later than an other date |
408 | // ~~~~~~~ |
409 | // ---------------------------------------------------------------------- |
410 | |
411 | Standard_Boolean Quantity_Date::IsLater(const Quantity_Date& other)const{ |
412 | if (mySec > other.mySec) return Standard_True; |
413 | else if (mySec < other.mySec) return Standard_False; |
414 | else return ( ( myUSec > other.myUSec ) ? Standard_True : Standard_False); |
415 | } |
416 | |
417 | |
418 | // ---------------------------------------------------------------------- |
419 | // IsEqual : Return true if the date is the same than an other date |
420 | // ~~~~~~~ |
421 | // ---------------------------------------------------------------------- |
422 | |
423 | Standard_Boolean Quantity_Date::IsEqual(const Quantity_Date& other)const{ |
424 | return ( ( myUSec == other.myUSec && |
425 | mySec == other.mySec ) ? Standard_True : Standard_False); |
426 | } |
427 | |