]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/timeflow.cpp
Security-179.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::Absolute(const struct timespec &tv)
49 { mValue = tv.tv_sec + double(tv.tv_nsec) / 1E9; }
50
51 Absolute::operator struct timeval () const
52 {
53 struct timeval tv;
54 if (mValue > LONG_MAX) {
55 tv.tv_sec = LONG_MAX;
56 tv.tv_usec = 0;
57 } else {
58 tv.tv_sec = int32_t(mValue);
59 double intPart;
60 tv.tv_usec = int32_t(modf(mValue, &intPart) * 1E6);
61 }
62 return tv;
63 }
64
65 Absolute::operator struct timespec () const
66 {
67 struct timespec ts;
68 if (mValue > LONG_MAX) {
69 ts.tv_sec = LONG_MAX;
70 ts.tv_nsec = 0;
71 } else {
72 ts.tv_sec = time_t(mValue);
73 double intPart;
74 ts.tv_nsec = int32_t(modf(mValue, &intPart) * 1E9);
75 }
76 return ts;
77 }
78
79 struct timeval Interval::timevalInterval() const
80 {
81 struct timeval tv;
82 if (mValue > LONG_MAX) {
83 tv.tv_sec = LONG_MAX;
84 tv.tv_usec = 0;
85 } else if (mValue < 0) {
86 tv.tv_sec = tv.tv_usec = 0;
87 } else {
88 tv.tv_sec = int32_t(mValue);
89 double intPart;
90 tv.tv_usec = int32_t(modf(mValue, &intPart) * 1E6);
91 }
92 return tv;
93 }
94
95
96 //
97 // Estimate resolution at given time.
98 //
99 // BSD select(2) has theoretical microsecond resolution, but the underlying
100 // Mach system deals with milliseconds, so we report that conservatively.
101 // Sometime in the future when the sun is near collapse, residual resolution
102 // of a double will drop under 1E-3, but we won't worry about that just yet.
103 //
104 Interval resolution(Absolute)
105 {
106 return 0.001;
107 }
108
109
110 } // end namespace Time
111 } // end namespace Security