0030991: Draw Harness - ViewerTest::ParseColor() defines out-of-range alpha component
[occt.git] / src / Quantity / Quantity_Period.cxx
CommitLineData
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 34Standard_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
42return ( (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// -------------------------------------------------------------
51Standard_Boolean Quantity_Period::IsValid(
52 const Standard_Integer ss,
53 const Standard_Integer mics){
54
55return ( (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// -------------------------------------------------------------
63Quantity_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
70SetValues (dd,hh,mn,ss,mils,mics);
71}
72
73// -------------------------------------------------------------
74// Create : Creates a period with a number of seconds
75// ~~~~~~ and microseconds.
76//
77// -------------------------------------------------------------
78Quantity_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// -------------------------------------------------------------
89void 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{
97Standard_Integer carry = mySec;
98dd = carry / ( 24 * 3600 );
99carry -= dd * 24 * 3600 ;
100hh = carry / 3600;
101carry -= 3600 * hh;
102mn = carry / 60;
103carry -= mn * 60;
104ss = carry;
105mis = myUSec / 1000;
106mics = myUSec - ( mis * 1000);
107}
108
109// -------------------------------------------------------------
110// Values : Returns a period with the number of seconds and
111// ~~~~~~ microseconds.
112// -------------------------------------------------------------
113void Quantity_Period::Values(
114 Standard_Integer& ss,
115 Standard_Integer& mics
116 )const{
117
118ss = mySec;
119mics = myUSec;
120}
121
122// -------------------------------------------------------------
123// SetValues : Sets a period with a number of days,hours,minutes,
124// ~~~~~~~~~ seconds and microseconds.
125// -------------------------------------------------------------
126void 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){
132SetValues( ( 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// -------------------------------------------------------------
140void Quantity_Period::SetValues(
141 const Standard_Integer ss,
142 const Standard_Integer mics) {
143
144if ( ! Quantity_Period::IsValid(ss,mics) )
9775fa61 145 throw Quantity_PeriodDefinitionError("Quantity_Period::SetValues invalid parameters");
7fd59977 146
147mySec = ss;
148myUSec = mics;
149while ( myUSec > 1000000 )
150 {
151 myUSec -= 1000000;
152 mySec++;
153 }
154}
155// -------------------------------------------------------------
156// Subtract : Subtracts a period to another period
157// ~~~~~~~~
158// -------------------------------------------------------------
159Quantity_Period Quantity_Period::Subtract(const Quantity_Period&
160 OtherPeriod)const{
161Quantity_Period result (mySec,myUSec);
162
163
164result.mySec -= OtherPeriod.mySec;
165result.myUSec -= OtherPeriod.myUSec;
166
167if ( result.mySec >= 0 && result.myUSec < 0 ) {
168 result.mySec--;
169 result.myUSec = 1000000 + result.myUSec ;
170 }
171else 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}
178else if ( result.mySec <0 && result.myUSec < 0 ) {
179 result.mySec = Abs(result.mySec);
180 result.myUSec = Abs(result.myUSec);
181}
182return (result);
183}
184
185// -------------------------------------------------------------
186// Add : Adds a period to another period
187// ~~~
188// -------------------------------------------------------------
189Quantity_Period Quantity_Period::Add(const Quantity_Period& OtherPeriod)
190 const{
191
192Quantity_Period result (mySec,myUSec);
193result.mySec += OtherPeriod.mySec;
194result.myUSec += OtherPeriod.myUSec;
195if (result.myUSec > 1000000)
196 {
197 result.myUSec -= 1000000;
198 result.mySec++;
199 }
200return (result);
201}
202
203
204// -------------------------------------------------------------
205// IsEqual : returns true if two periods are equal
206// ~~~~~~~
207// -------------------------------------------------------------
208Standard_Boolean Quantity_Period::IsEqual(const Quantity_Period&
209 OtherPeriod)const{
210
211return
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// -------------------------------------------------------------
221Standard_Boolean Quantity_Period::IsShorter(
222 const Quantity_Period& OtherPeriod)const{
223
224if ( mySec < OtherPeriod.mySec ) return Standard_True;
225else if ( mySec > OtherPeriod.mySec ) return Standard_False;
226else 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// -------------------------------------------------------------
235Standard_Boolean Quantity_Period::IsLonger(
236 const Quantity_Period& OtherPeriod)const{
237
238if ( mySec > OtherPeriod.mySec ) return Standard_True;
239else if ( mySec < OtherPeriod.mySec ) return Standard_False;
240else return
241 ( ( myUSec > OtherPeriod.myUSec ) ? Standard_True : Standard_False);
242}
243
244