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 | // |
973c2be1 |
6 | // This library is free software; you can redistribute it and / or modify it |
7 | // under the terms of the GNU Lesser General Public version 2.1 as published |
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 | // ------------------------------------------------------------- |
20 | #include <Quantity_PeriodDefinitionError.hxx> |
21 | #include <Quantity_Period.ixx> |
22 | |
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 | // ----------------------------------------------------------- |
34 | |
35 | |
36 | Standard_Boolean Quantity_Period::IsValid( |
37 | const Standard_Integer dd, |
38 | const Standard_Integer hh, |
39 | const Standard_Integer mn, |
40 | const Standard_Integer ss, |
41 | const Standard_Integer mis, |
42 | const Standard_Integer mics){ |
43 | |
44 | return ( (dd < 0 || hh < 0 || mn < 0 || |
45 | ss < 0 || mis < 0 || mics < 0 ) ? Standard_False : Standard_True ); |
46 | } |
47 | // ------------------------------------------------------------- |
48 | // IsValid : Checks the validity of a date |
49 | // With: |
50 | // 0 <= ss |
51 | // 0 <= mics |
52 | // ------------------------------------------------------------- |
53 | Standard_Boolean Quantity_Period::IsValid( |
54 | const Standard_Integer ss, |
55 | const Standard_Integer mics){ |
56 | |
57 | return ( (ss < 0 || mics < 0 ) ? Standard_False : Standard_True ); |
58 | } |
59 | |
60 | // ------------------------------------------------------------- |
61 | // Create : Creates a period with a number of seconds |
62 | // ~~~~~~ and microseconds. |
63 | // |
64 | // ------------------------------------------------------------- |
65 | Quantity_Period::Quantity_Period(const Standard_Integer dd, |
66 | const Standard_Integer hh, |
67 | const Standard_Integer mn, |
68 | const Standard_Integer ss, |
69 | const Standard_Integer mils, |
70 | const Standard_Integer mics){ |
71 | |
72 | SetValues (dd,hh,mn,ss,mils,mics); |
73 | } |
74 | |
75 | // ------------------------------------------------------------- |
76 | // Create : Creates a period with a number of seconds |
77 | // ~~~~~~ and microseconds. |
78 | // |
79 | // ------------------------------------------------------------- |
80 | Quantity_Period::Quantity_Period(const Standard_Integer ss, |
81 | const Standard_Integer mics){ |
82 | |
83 | SetValues(ss,mics); |
84 | } |
85 | |
86 | |
87 | // ------------------------------------------------------------- |
88 | // Values : Returns a period with the number of days,hours, |
89 | // ~~~~~~ minutes,seconds,milliseconds and microseconds. |
90 | // ------------------------------------------------------------- |
91 | void Quantity_Period::Values( |
92 | Standard_Integer& dd, |
93 | Standard_Integer& hh, |
94 | Standard_Integer& mn, |
95 | Standard_Integer& ss, |
96 | Standard_Integer& mis, |
97 | Standard_Integer& mics |
98 | )const{ |
99 | Standard_Integer carry = mySec; |
100 | dd = carry / ( 24 * 3600 ); |
101 | carry -= dd * 24 * 3600 ; |
102 | hh = carry / 3600; |
103 | carry -= 3600 * hh; |
104 | mn = carry / 60; |
105 | carry -= mn * 60; |
106 | ss = carry; |
107 | mis = myUSec / 1000; |
108 | mics = myUSec - ( mis * 1000); |
109 | } |
110 | |
111 | // ------------------------------------------------------------- |
112 | // Values : Returns a period with the number of seconds and |
113 | // ~~~~~~ microseconds. |
114 | // ------------------------------------------------------------- |
115 | void Quantity_Period::Values( |
116 | Standard_Integer& ss, |
117 | Standard_Integer& mics |
118 | )const{ |
119 | |
120 | ss = mySec; |
121 | mics = myUSec; |
122 | } |
123 | |
124 | // ------------------------------------------------------------- |
125 | // SetValues : Sets a period with a number of days,hours,minutes, |
126 | // ~~~~~~~~~ seconds and microseconds. |
127 | // ------------------------------------------------------------- |
128 | void Quantity_Period::SetValues( const Standard_Integer dd, |
129 | const Standard_Integer hh, |
130 | const Standard_Integer mn, |
131 | const Standard_Integer ss, |
132 | const Standard_Integer mils, |
133 | const Standard_Integer mics){ |
134 | SetValues( ( dd * 24 * 3600 ) + ( hh * 3600 ) + ( 60 * mn ) + ss , |
135 | mils * 1000 + mics ); |
136 | } |
137 | |
138 | // ------------------------------------------------------------- |
139 | // SetValues : Sets a period with a number of seconds and |
140 | // ~~~~~~~~~ microseconds. |
141 | // ------------------------------------------------------------- |
142 | void Quantity_Period::SetValues( |
143 | const Standard_Integer ss, |
144 | const Standard_Integer mics) { |
145 | |
146 | if ( ! Quantity_Period::IsValid(ss,mics) ) |
147 | Quantity_PeriodDefinitionError::Raise( |
148 | "Quantity_Period::SetValues invalid parameters"); |
149 | |
150 | mySec = ss; |
151 | myUSec = mics; |
152 | while ( myUSec > 1000000 ) |
153 | { |
154 | myUSec -= 1000000; |
155 | mySec++; |
156 | } |
157 | } |
158 | // ------------------------------------------------------------- |
159 | // Subtract : Subtracts a period to another period |
160 | // ~~~~~~~~ |
161 | // ------------------------------------------------------------- |
162 | Quantity_Period Quantity_Period::Subtract(const Quantity_Period& |
163 | OtherPeriod)const{ |
164 | Quantity_Period result (mySec,myUSec); |
165 | |
166 | |
167 | result.mySec -= OtherPeriod.mySec; |
168 | result.myUSec -= OtherPeriod.myUSec; |
169 | |
170 | if ( result.mySec >= 0 && result.myUSec < 0 ) { |
171 | result.mySec--; |
172 | result.myUSec = 1000000 + result.myUSec ; |
173 | } |
174 | else if ( result.mySec <0 && result.myUSec >= 0 ) { |
175 | result.mySec = Abs(result.mySec); |
176 | if ( result.myUSec > 0 ){ |
177 | result.mySec--; |
178 | result.myUSec = 1000000 - result.myUSec ; |
179 | } |
180 | } |
181 | else if ( result.mySec <0 && result.myUSec < 0 ) { |
182 | result.mySec = Abs(result.mySec); |
183 | result.myUSec = Abs(result.myUSec); |
184 | } |
185 | return (result); |
186 | } |
187 | |
188 | // ------------------------------------------------------------- |
189 | // Add : Adds a period to another period |
190 | // ~~~ |
191 | // ------------------------------------------------------------- |
192 | Quantity_Period Quantity_Period::Add(const Quantity_Period& OtherPeriod) |
193 | const{ |
194 | |
195 | Quantity_Period result (mySec,myUSec); |
196 | result.mySec += OtherPeriod.mySec; |
197 | result.myUSec += OtherPeriod.myUSec; |
198 | if (result.myUSec > 1000000) |
199 | { |
200 | result.myUSec -= 1000000; |
201 | result.mySec++; |
202 | } |
203 | return (result); |
204 | } |
205 | |
206 | |
207 | // ------------------------------------------------------------- |
208 | // IsEqual : returns true if two periods are equal |
209 | // ~~~~~~~ |
210 | // ------------------------------------------------------------- |
211 | Standard_Boolean Quantity_Period::IsEqual(const Quantity_Period& |
212 | OtherPeriod)const{ |
213 | |
214 | return |
215 | ( ( mySec == OtherPeriod.mySec && |
216 | myUSec == OtherPeriod.myUSec ) ? Standard_True : Standard_False); |
217 | } |
218 | |
219 | |
220 | // ------------------------------------------------------------- |
221 | // IsShorter : returns true if a date is shorter then an other |
222 | // ~~~~~~~~~ date |
223 | // ------------------------------------------------------------- |
224 | Standard_Boolean Quantity_Period::IsShorter( |
225 | const Quantity_Period& OtherPeriod)const{ |
226 | |
227 | if ( mySec < OtherPeriod.mySec ) return Standard_True; |
228 | else if ( mySec > OtherPeriod.mySec ) return Standard_False; |
229 | else return |
230 | ( ( myUSec < OtherPeriod.myUSec ) ? Standard_True : Standard_False); |
231 | } |
232 | |
233 | |
234 | // ------------------------------------------------------------- |
235 | // IsLonger : returns true if a date is longer then an other |
236 | // ~~~~~~~~ date |
237 | // ------------------------------------------------------------- |
238 | Standard_Boolean Quantity_Period::IsLonger( |
239 | const Quantity_Period& OtherPeriod)const{ |
240 | |
241 | if ( mySec > OtherPeriod.mySec ) return Standard_True; |
242 | else if ( mySec < OtherPeriod.mySec ) return Standard_False; |
243 | else return |
244 | ( ( myUSec > OtherPeriod.myUSec ) ? Standard_True : Standard_False); |
245 | } |
246 | |
247 | |