]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/timeflow.h
Security-54.1.3.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / timeflow.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // timeflow - abstract view of the flow of time
21 //
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.
25 //
26 #ifndef _H_TIMEFLOW
27 #define _H_TIMEFLOW
28
29 #include <sys/time.h>
30 #include <math.h> // for MAXFLOAT
31
32
33 namespace Security {
34 namespace Time {
35
36
37 //
38 // A Time::Interval is a time difference (distance).
39 //
40 class Interval {
41 friend class Absolute;
42 public:
43 Interval() { }
44 Interval(int seconds) { mValue = seconds; }
45 Interval(double seconds) { mValue = seconds; }
46 explicit Interval(time_t seconds) { mValue = seconds; }
47
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; }
52
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; }
59
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; }
64
65 // struct timeval is sometimes used for time intervals, but not often - so be explicit
66 struct timeval timevalInterval() const;
67
68 private:
69 double mValue;
70 };
71
72
73 //
74 // A Time::Absolute is a moment in time.
75 //
76 class Absolute {
77 friend class Interval;
78 friend Interval operator - (Absolute, Absolute);
79 friend Absolute now();
80 friend Absolute bigBang();
81 friend Absolute heatDeath();
82 public:
83 Absolute() { } // uninitialized
84 Absolute(time_t t) { mValue = t; } // from time_t
85 Absolute(const struct timeval &tv); // from timeval
86
87 // *crement operators
88 Absolute &operator += (Interval rel) { mValue += rel.mValue; return *this; }
89 Absolute &operator -= (Interval rel) { mValue -= rel.mValue; return *this; }
90
91 // comparisons
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; }
98
99 // express as conventional (absolute!) time measures
100 operator struct timeval() const;
101 operator time_t () const { return time_t(mValue); }
102
103 // internal form for debugging ONLY
104 double internalForm() const { return mValue; }
105
106 private:
107 double mValue;
108
109 Absolute(double value) : mValue(value) { }
110 };
111
112
113 //
114 // Time::now produces the current time
115 //
116 Absolute now(); // get "now"
117
118
119 //
120 // Time::resolution(when) gives a conservative estimate of the available resolution
121 // at a given time.
122 //
123 Interval resolution(Absolute at); // estimate available resolution at given time
124
125
126 //
127 // Some useful "constants"
128 //
129 inline Absolute bigBang() { return -MAXFLOAT; }
130 inline Absolute heatDeath() { return +MAXFLOAT; }
131
132
133
134 //
135 // More inline arithmetic
136 //
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; }
141
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; }
145
146 inline Interval operator - (Absolute t1, Absolute t0)
147 { return t1.mValue - t0.mValue; }
148
149
150 } // end namespace Time
151 } // end namespace Security
152
153 #endif //_H_TIMEFLOW