]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/timeflow.cpp
Security-28.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / timeflow.cpp
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 #include "timeflow.h"
23 #include <sys/time.h>
24 #include <math.h>
25
26
27 namespace Security {
28 namespace Time {
29
30
31 //
32 // Get "now"
33 //
34 Absolute now()
35 {
36 struct timeval tv;
37 gettimeofday(&tv, NULL);
38 return tv.tv_sec + double(tv.tv_usec) / 1E6;
39 }
40
41
42 //
43 // OOL Conversions
44 //
45 Absolute::Absolute(const struct timeval &tv)
46 { mValue = tv.tv_sec + double(tv.tv_usec) / 1E6; }
47
48 Absolute::operator struct timeval () const
49 {
50 struct timeval tv;
51 if (mValue > LONG_MAX) {
52 tv.tv_sec = LONG_MAX;
53 tv.tv_usec = 0;
54 } else {
55 tv.tv_sec = int32_t(mValue);
56 double intPart;
57 tv.tv_usec = int32_t(modf(mValue, &intPart));
58 }
59 return tv;
60 }
61
62 struct timeval Interval::timevalInterval() const
63 {
64 struct timeval tv;
65 if (mValue > LONG_MAX) {
66 tv.tv_sec = LONG_MAX;
67 tv.tv_usec = 0;
68 } else if (mValue < 0) {
69 tv.tv_sec = tv.tv_usec = 0;
70 } else {
71 tv.tv_sec = int32_t(mValue);
72 double intPart;
73 tv.tv_usec = int32_t(modf(mValue, &intPart));
74 }
75 return tv;
76 }
77
78
79 //
80 // Estimate resolution at given time.
81 //
82 // BSD select(2) has theoretical microsecond resolution, but the underlying
83 // Mach system deals with milliseconds, so we report that conservatively.
84 // Sometime in the future when the sun is near collapse, residual resolution
85 // of a double will drop under 1E-3, but we won't worry about that just yet.
86 //
87 Interval resolution(Absolute)
88 {
89 return 0.001;
90 }
91
92
93 } // end namespace Time
94 } // end namespace Security