]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/timeflow.h
Security-163.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 Absolute(const struct timespec &ts); // from timespec
87
88 // *crement operators
89 Absolute &operator += (Interval rel) { mValue += rel.mValue; return *this; }
90 Absolute &operator -= (Interval rel) { mValue -= rel.mValue; return *this; }
91
92 // comparisons
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 bool operator != (Absolute other) const { return mValue != other.mValue; }
99
100 // express as conventional (absolute!) time measures
101 operator struct timeval() const;
102 operator struct timespec() const;
103 operator time_t () const { return time_t(mValue); }
104
105 // internal form for debugging ONLY
106 double internalForm() const { return mValue; }
107
108 private:
109 double mValue;
110
111 Absolute(double value) : mValue(value) { }
112 };
113
114
115 //
116 // Time::now produces the current time
117 //
118 Absolute now(); // get "now"
119
120
121 //
122 // Time::resolution(when) gives a conservative estimate of the available resolution
123 // at a given time.
124 //
125 Interval resolution(Absolute at); // estimate available resolution at given time
126
127
128 //
129 // Some useful "constants"
130 //
131 inline Absolute bigBang() { return -MAXFLOAT; }
132 inline Absolute heatDeath() { return +MAXFLOAT; }
133
134
135
136 //
137 // More inline arithmetic
138 //
139 inline Interval operator + (Interval r, Interval r2) { r += r2; return r; }
140 inline Interval operator - (Interval r, Interval r2) { r -= r2; return r; }
141 inline Interval operator * (Interval r, double f) { r *= f; return r; }
142 inline Interval operator / (Interval r, double f) { r /= f; return r; }
143
144 inline Absolute operator + (Absolute a, Interval r) { return a += r; }
145 inline Absolute operator + (Interval r, Absolute a) { return a += r; }
146 inline Absolute operator - (Absolute a, Interval r) { return a -= r; }
147
148 inline Interval operator - (Absolute t1, Absolute t0)
149 { return t1.mValue - t0.mValue; }
150
151
152 } // end namespace Time
153 } // end namespace Security
154
155 #endif //_H_TIMEFLOW