]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/timeflow.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // timeflow - abstract view of the flow of time
22 // We happily publish both absolute and relative times as floating-point values.
23 // Absolute times are off the UNIX Epoch (1/1/1970, midnight). This leaves us about
24 // microsecond resolution in Modern Times.
30 #include <math.h> // for MAXFLOAT
38 // A Time::Interval is a time difference (distance).
41 friend class Absolute
;
44 Interval(int seconds
) { mValue
= seconds
; }
45 Interval(double seconds
) { mValue
= seconds
; }
46 explicit Interval(time_t seconds
) { mValue
= seconds
; }
48 Interval
&operator += (Interval rel
) { mValue
+= rel
.mValue
; return *this; }
49 Interval
&operator -= (Interval rel
) { mValue
-= rel
.mValue
; return *this; }
50 Interval
&operator *= (double f
) { mValue
*= f
; return *this; }
51 Interval
&operator /= (double f
) { mValue
/= f
; return *this; }
53 bool operator < (Interval other
) const { return mValue
< other
.mValue
; }
54 bool operator <= (Interval other
) const { return mValue
<= other
.mValue
; }
55 bool operator > (Interval other
) const { return mValue
> other
.mValue
; }
56 bool operator >= (Interval other
) const { return mValue
>= other
.mValue
; }
57 bool operator == (Interval other
) const { return mValue
== other
.mValue
; }
58 bool operator != (Interval other
) const { return mValue
!= other
.mValue
; }
60 // express as (fractions of) seconds, milliseconds, or microseconds
61 double seconds() const { return mValue
; }
62 double mSeconds() const { return mValue
* 1E3
; }
63 double uSeconds() const { return mValue
* 1E6
; }
65 // struct timeval is sometimes used for time intervals, but not often - so be explicit
66 struct timeval
timevalInterval() const;
74 // A Time::Absolute is a moment in time.
77 friend class Interval
;
78 friend Interval
operator - (Absolute
, Absolute
);
79 friend Absolute
now();
80 friend Absolute
bigBang();
81 friend Absolute
heatDeath();
83 Absolute() { } // uninitialized
84 Absolute(time_t t
) { mValue
= t
; } // from time_t
85 Absolute(const struct timeval
&tv
); // from timeval
88 Absolute
&operator += (Interval rel
) { mValue
+= rel
.mValue
; return *this; }
89 Absolute
&operator -= (Interval rel
) { mValue
-= rel
.mValue
; return *this; }
92 bool operator < (Absolute other
) const { return mValue
< other
.mValue
; }
93 bool operator <= (Absolute other
) const { return mValue
<= other
.mValue
; }
94 bool operator > (Absolute other
) const { return mValue
> other
.mValue
; }
95 bool operator >= (Absolute other
) const { return mValue
>= other
.mValue
; }
96 bool operator == (Absolute other
) const { return mValue
== other
.mValue
; }
97 bool operator != (Absolute other
) const { return mValue
!= other
.mValue
; }
99 // express as conventional (absolute!) time measures
100 operator struct timeval() const;
101 operator time_t () const { return time_t(mValue
); }
103 // internal form for debugging ONLY
104 double internalForm() const { return mValue
; }
109 Absolute(double value
) : mValue(value
) { }
114 // Time::now produces the current time
116 Absolute
now(); // get "now"
120 // Time::resolution(when) gives a conservative estimate of the available resolution
123 Interval
resolution(Absolute at
); // estimate available resolution at given time
127 // Some useful "constants"
129 inline Absolute
bigBang() { return -MAXFLOAT
; }
130 inline Absolute
heatDeath() { return +MAXFLOAT
; }
135 // More inline arithmetic
137 inline Interval
operator + (Interval r
, Interval r2
) { r
+= r2
; return r
; }
138 inline Interval
operator - (Interval r
, Interval r2
) { r
-= r2
; return r
; }
139 inline Interval
operator * (Interval r
, double f
) { r
*= f
; return r
; }
140 inline Interval
operator / (Interval r
, double f
) { r
/= f
; return r
; }
142 inline Absolute
operator + (Absolute a
, Interval r
) { return a
+= r
; }
143 inline Absolute
operator + (Interval r
, Absolute a
) { return a
+= r
; }
144 inline Absolute
operator - (Absolute a
, Interval r
) { return a
-= r
; }
146 inline Interval
operator - (Absolute t1
, Absolute t0
)
147 { return t1
.mValue
- t0
.mValue
; }
150 } // end namespace Time
151 } // end namespace Security