Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Quantity / Quantity_Date.cxx
CommitLineData
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
16static 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
30static int SecondsByYear = 365 * 24 * 3600 ; // Normal Year
31static int SecondsByLeapYear = 366 * 24 * 3600 ; // Leap Year
32
33// -----------------------------------------
34// Initialize a date to January,1 1979 00:00
35// -----------------------------------------
36
37Quantity_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
47Standard_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
56if (mm < 1 || mm > 12) return Standard_False;
57
58
59if (yy < 1979 ) return Standard_False;
60
61
62if ( Quantity_Date::IsLeap (yy) ) month_table[1] = 29;
63else month_table[1] = 28;
64
65if (dd < 1 || dd > month_table[mm-1]) return Standard_False;
66
67
68if (hh < 0 || hh > 23) return Standard_False;
69
70
71if (mn < 0 || mn > 59) return Standard_False;
72
73
74if (ss < 0 || ss > 59) return Standard_False;
75
76
77if (mis < 0 || mis > 999) return Standard_False;
78
79
80if (mics < 0 || mics > 999) return Standard_False;
81
82return Standard_True;
83
84}
85
86// -----------------------------------------------------------
87// Initialize a Date :
88// This is the complete way month, day, year, ... micro second
89// -----------------------------------------------------------
90
91
92Quantity_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
101SetValues (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
109void 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
118Standard_Integer i;
119
120if ( ! Quantity_Date::IsValid (mm,dd,yy,hh,mn,ss,mis,mics))
121 Quantity_DateDefinitionError::Raise(
122 "Quantity_Date::Quantity_Date invalid parameters");
123
124
125if ( Quantity_Date::IsLeap (yy) ) month_table[1] = 29;
126else month_table[1] = 28;
127
128mySec = 0;
129myUSec = 0;
130for(i = 1979; i < yy; i++) {
131 if ( ! Quantity_Date::IsLeap (i) ) mySec += SecondsByYear;
132 else mySec += SecondsByLeapYear;
133 }
134
135for(i = 1; i< mm; i++) {
136 mySec += month_table[i-1] * 3600 * 24 ;
137 }
138
139
140mySec += 3600 * 24 * (dd-1);
141
142mySec += 3600 * hh;
143
144mySec += 60 * mn;
145
146mySec += ss;
147
148myUSec += mis * 1000;
149
150myUSec += mics;
151
152}
153
154
155// ---------------------------------------------
156// Values : Returns the values of a date
157// ~~~~~~
158// ---------------------------------------------
159
160void 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
170Standard_Integer i,carry;
171
172
173for(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
189for(mm = 1 ; ; mm++) {
190 i = month_table[mm-1] * 3600 * 24;
191 if ( carry >= i ) carry -= i;
192 else break;
193 }
194
195i = 3600 * 24;
196for(dd = 1 ; ; dd++) {
197 if ( carry >= i ) carry -= i;
198 else break;
199 }
200
201for(hh = 0 ; ; hh++) {
202 if ( carry >= 3600 ) carry -= 3600;
203 else break;
204 }
205
206for(mn = 0 ; ; mn++) {
207 if ( carry >= 60 ) carry -= 60;
208 else break;
209 }
210
211ss = carry;
212
213mis = myUSec / 1000;
214mics = 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
223Quantity_Period Quantity_Date::Difference(const Quantity_Date& OtherDate){
224
225Standard_Integer i1,i2;
226
227if (mySec == 0 && myUSec == 0)
228 {
229 i1 = OtherDate.mySec;
230 i2 = OtherDate.myUSec;
231 }
232else {
233 i1 = mySec - OtherDate.mySec ;
234 i2 = myUSec - OtherDate.myUSec;
235 }
236
237if ( i1 >= 0 && i2 < 0 ) {
238 i1--;
239 i2 = 1000000 + i2 ;
240 }
241else if ( i1 <0 && i2 >= 0 ) {
242 i1 = Abs(i1);
243 if ( i2 > 0 ){
244 i1--;
245 i2 = 1000000 - i2 ;
246 }
247}
248else if ( i1 <0 && i2 < 0 ) {
249 i1 = Abs(i1);
250 i2 = Abs(i2);
251}
252
253Quantity_Period result ( i1 , i2 );
254
255return (result);
256}
257
258
259// ------------------------------------------------------------------
260// Subtract : subtracts a period to a date and returns a date.
261// ~~~~~~~~
262// ------------------------------------------------------------------
263
264Quantity_Date Quantity_Date::Subtract(const Quantity_Period& During){
265
266Standard_Integer ss,mics;
267Quantity_Date result;
268result.mySec = mySec;
269result.myUSec = myUSec;
270During.Values (ss,mics);
271
272result.mySec -= ss;
273result.myUSec -= mics;
274
275if ( result.mySec >= 0 && result.myUSec < 0 ) {
276 result.mySec--;
277 result.myUSec = 1000000 + result.myUSec ;
278 }
279
280
281if ( result.mySec <0 )
282 Quantity_DateDefinitionError::Raise(
283 "Quantity_Date::Subtract : The result date is anterior to Jan,1 1979");
284
285return (result);
286
287}
288
289
290// ----------------------------------------------------------------------
291// Add : Adds a period of time to a date
292// ~~~
293// ----------------------------------------------------------------------
294Quantity_Date Quantity_Date::Add(const Quantity_Period& During){
295
296Quantity_Date result;
297During.Values (result.mySec,result.myUSec);
298result.mySec += mySec;
299result.myUSec += myUSec;
300if ( result.myUSec >= 1000000 ) {
301 result.mySec++;
302 result.myUSec -= 1000000;
303 }
304return (result);
305}
306
307
308// ----------------------------------------------------------------------
309// Year : Return the year of a date
310// ~~~~
311// ----------------------------------------------------------------------
312Standard_Integer Quantity_Date::Year(){
313Standard_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// ----------------------------------------------------------------------
323Standard_Integer Quantity_Date::Month(){
324Standard_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
334Standard_Integer Quantity_Date::Day(){
335Standard_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
345Standard_Integer Quantity_Date::Hour(){
346Standard_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
356Standard_Integer Quantity_Date::Minute(){
357Standard_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
367Standard_Integer Quantity_Date::Second(){
368Standard_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
378Standard_Integer Quantity_Date::MilliSecond(){
379Standard_Integer dummy, msec;
380 Values(dummy, dummy, dummy, dummy, dummy, dummy, msec, dummy);
381return(msec);
382}
383
384// ----------------------------------------------------------------------
385// Day : Return the day of a date
386// ~~~
387// ----------------------------------------------------------------------
388
389Standard_Integer Quantity_Date::MicroSecond(){
390Standard_Integer dummy, msec;
391 Values(dummy, dummy, dummy, dummy, dummy, dummy, dummy, msec);
392return(msec);
393}
394
395// ----------------------------------------------------------------------
396// IsEarlier : Return true if the date is earlier than an other date
397// ~~~~~~~~~
398// ----------------------------------------------------------------------
399
400Standard_Boolean Quantity_Date::IsEarlier(const Quantity_Date& other)const{
401if (mySec < other.mySec) return Standard_True;
402else if (mySec > other.mySec) return Standard_False;
403else 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
411Standard_Boolean Quantity_Date::IsLater(const Quantity_Date& other)const{
412if (mySec > other.mySec) return Standard_True;
413else if (mySec < other.mySec) return Standard_False;
414else 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
423Standard_Boolean Quantity_Date::IsEqual(const Quantity_Date& other)const{
424return ( ( myUSec == other.myUSec &&
425 mySec == other.mySec ) ? Standard_True : Standard_False);
426}
427