b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
7fd59977 |
14 | |
15 | // ------------------------------------------------------------- |
7fd59977 |
16 | // C matra datavision 1993 |
7fd59977 |
17 | // Period class implementation. |
7fd59977 |
18 | // Updated : |
7fd59977 |
19 | // ------------------------------------------------------------- |
7fd59977 |
20 | |
42cf5bc1 |
21 | #include <Quantity_Period.hxx> |
22 | #include <Quantity_PeriodDefinitionError.hxx> |
7fd59977 |
23 | |
24 | // ----------------------------------------------------------- |
25 | // IsValid : Checks the validity of a date |
26 | // With: |
27 | // 0 <= dd |
28 | // 0 <= hh |
29 | // 0 <= mn |
30 | // 0 <= ss |
31 | // 0 <= mis |
32 | // 0 <= mics |
33 | // ----------------------------------------------------------- |
7fd59977 |
34 | Standard_Boolean Quantity_Period::IsValid( |
35 | const Standard_Integer dd, |
36 | const Standard_Integer hh, |
37 | const Standard_Integer mn, |
38 | const Standard_Integer ss, |
39 | const Standard_Integer mis, |
40 | const Standard_Integer mics){ |
41 | |
42 | return ( (dd < 0 || hh < 0 || mn < 0 || |
43 | ss < 0 || mis < 0 || mics < 0 ) ? Standard_False : Standard_True ); |
44 | } |
45 | // ------------------------------------------------------------- |
46 | // IsValid : Checks the validity of a date |
47 | // With: |
48 | // 0 <= ss |
49 | // 0 <= mics |
50 | // ------------------------------------------------------------- |
51 | Standard_Boolean Quantity_Period::IsValid( |
52 | const Standard_Integer ss, |
53 | const Standard_Integer mics){ |
54 | |
55 | return ( (ss < 0 || mics < 0 ) ? Standard_False : Standard_True ); |
56 | } |
57 | |
58 | // ------------------------------------------------------------- |
59 | // Create : Creates a period with a number of seconds |
60 | // ~~~~~~ and microseconds. |
61 | // |
62 | // ------------------------------------------------------------- |
63 | Quantity_Period::Quantity_Period(const Standard_Integer dd, |
64 | const Standard_Integer hh, |
65 | const Standard_Integer mn, |
66 | const Standard_Integer ss, |
67 | const Standard_Integer mils, |
68 | const Standard_Integer mics){ |
69 | |
70 | SetValues (dd,hh,mn,ss,mils,mics); |
71 | } |
72 | |
73 | // ------------------------------------------------------------- |
74 | // Create : Creates a period with a number of seconds |
75 | // ~~~~~~ and microseconds. |
76 | // |
77 | // ------------------------------------------------------------- |
78 | Quantity_Period::Quantity_Period(const Standard_Integer ss, |
79 | const Standard_Integer mics){ |
80 | |
81 | SetValues(ss,mics); |
82 | } |
83 | |
84 | |
85 | // ------------------------------------------------------------- |
86 | // Values : Returns a period with the number of days,hours, |
87 | // ~~~~~~ minutes,seconds,milliseconds and microseconds. |
88 | // ------------------------------------------------------------- |
89 | void Quantity_Period::Values( |
90 | Standard_Integer& dd, |
91 | Standard_Integer& hh, |
92 | Standard_Integer& mn, |
93 | Standard_Integer& ss, |
94 | Standard_Integer& mis, |
95 | Standard_Integer& mics |
96 | )const{ |
97 | Standard_Integer carry = mySec; |
98 | dd = carry / ( 24 * 3600 ); |
99 | carry -= dd * 24 * 3600 ; |
100 | hh = carry / 3600; |
101 | carry -= 3600 * hh; |
102 | mn = carry / 60; |
103 | carry -= mn * 60; |
104 | ss = carry; |
105 | mis = myUSec / 1000; |
106 | mics = myUSec - ( mis * 1000); |
107 | } |
108 | |
109 | // ------------------------------------------------------------- |
110 | // Values : Returns a period with the number of seconds and |
111 | // ~~~~~~ microseconds. |
112 | // ------------------------------------------------------------- |
113 | void Quantity_Period::Values( |
114 | Standard_Integer& ss, |
115 | Standard_Integer& mics |
116 | )const{ |
117 | |
118 | ss = mySec; |
119 | mics = myUSec; |
120 | } |
121 | |
122 | // ------------------------------------------------------------- |
123 | // SetValues : Sets a period with a number of days,hours,minutes, |
124 | // ~~~~~~~~~ seconds and microseconds. |
125 | // ------------------------------------------------------------- |
126 | void Quantity_Period::SetValues( const Standard_Integer dd, |
127 | const Standard_Integer hh, |
128 | const Standard_Integer mn, |
129 | const Standard_Integer ss, |
130 | const Standard_Integer mils, |
131 | const Standard_Integer mics){ |
132 | SetValues( ( dd * 24 * 3600 ) + ( hh * 3600 ) + ( 60 * mn ) + ss , |
133 | mils * 1000 + mics ); |
134 | } |
135 | |
136 | // ------------------------------------------------------------- |
137 | // SetValues : Sets a period with a number of seconds and |
138 | // ~~~~~~~~~ microseconds. |
139 | // ------------------------------------------------------------- |
140 | void Quantity_Period::SetValues( |
141 | const Standard_Integer ss, |
142 | const Standard_Integer mics) { |
143 | |
144 | if ( ! Quantity_Period::IsValid(ss,mics) ) |
9775fa61 |
145 | throw Quantity_PeriodDefinitionError("Quantity_Period::SetValues invalid parameters"); |
7fd59977 |
146 | |
147 | mySec = ss; |
148 | myUSec = mics; |
149 | while ( myUSec > 1000000 ) |
150 | { |
151 | myUSec -= 1000000; |
152 | mySec++; |
153 | } |
154 | } |
155 | // ------------------------------------------------------------- |
156 | // Subtract : Subtracts a period to another period |
157 | // ~~~~~~~~ |
158 | // ------------------------------------------------------------- |
159 | Quantity_Period Quantity_Period::Subtract(const Quantity_Period& |
160 | OtherPeriod)const{ |
161 | Quantity_Period result (mySec,myUSec); |
162 | |
163 | |
164 | result.mySec -= OtherPeriod.mySec; |
165 | result.myUSec -= OtherPeriod.myUSec; |
166 | |
167 | if ( result.mySec >= 0 && result.myUSec < 0 ) { |
168 | result.mySec--; |
169 | result.myUSec = 1000000 + result.myUSec ; |
170 | } |
171 | else if ( result.mySec <0 && result.myUSec >= 0 ) { |
172 | result.mySec = Abs(result.mySec); |
173 | if ( result.myUSec > 0 ){ |
174 | result.mySec--; |
175 | result.myUSec = 1000000 - result.myUSec ; |
176 | } |
177 | } |
178 | else if ( result.mySec <0 && result.myUSec < 0 ) { |
179 | result.mySec = Abs(result.mySec); |
180 | result.myUSec = Abs(result.myUSec); |
181 | } |
182 | return (result); |
183 | } |
184 | |
185 | // ------------------------------------------------------------- |
186 | // Add : Adds a period to another period |
187 | // ~~~ |
188 | // ------------------------------------------------------------- |
189 | Quantity_Period Quantity_Period::Add(const Quantity_Period& OtherPeriod) |
190 | const{ |
191 | |
192 | Quantity_Period result (mySec,myUSec); |
193 | result.mySec += OtherPeriod.mySec; |
194 | result.myUSec += OtherPeriod.myUSec; |
195 | if (result.myUSec > 1000000) |
196 | { |
197 | result.myUSec -= 1000000; |
198 | result.mySec++; |
199 | } |
200 | return (result); |
201 | } |
202 | |
203 | |
204 | // ------------------------------------------------------------- |
205 | // IsEqual : returns true if two periods are equal |
206 | // ~~~~~~~ |
207 | // ------------------------------------------------------------- |
208 | Standard_Boolean Quantity_Period::IsEqual(const Quantity_Period& |
209 | OtherPeriod)const{ |
210 | |
211 | return |
212 | ( ( mySec == OtherPeriod.mySec && |
213 | myUSec == OtherPeriod.myUSec ) ? Standard_True : Standard_False); |
214 | } |
215 | |
216 | |
217 | // ------------------------------------------------------------- |
218 | // IsShorter : returns true if a date is shorter then an other |
219 | // ~~~~~~~~~ date |
220 | // ------------------------------------------------------------- |
221 | Standard_Boolean Quantity_Period::IsShorter( |
222 | const Quantity_Period& OtherPeriod)const{ |
223 | |
224 | if ( mySec < OtherPeriod.mySec ) return Standard_True; |
225 | else if ( mySec > OtherPeriod.mySec ) return Standard_False; |
226 | else return |
227 | ( ( myUSec < OtherPeriod.myUSec ) ? Standard_True : Standard_False); |
228 | } |
229 | |
230 | |
231 | // ------------------------------------------------------------- |
232 | // IsLonger : returns true if a date is longer then an other |
233 | // ~~~~~~~~ date |
234 | // ------------------------------------------------------------- |
235 | Standard_Boolean Quantity_Period::IsLonger( |
236 | const Quantity_Period& OtherPeriod)const{ |
237 | |
238 | if ( mySec > OtherPeriod.mySec ) return Standard_True; |
239 | else if ( mySec < OtherPeriod.mySec ) return Standard_False; |
240 | else return |
241 | ( ( myUSec > OtherPeriod.myUSec ) ? Standard_True : Standard_False); |
242 | } |
243 | |
244 | |