1 | /* |
---|
2 | * Copyright (c) 2005, Eric Crahen |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
5 | * of this software and associated documentation files (the "Software"), to deal |
---|
6 | * in the Software without restriction, including without limitation the rights |
---|
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
8 | * copies of the Software, and to permit persons to whom the Software is furnished |
---|
9 | * to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in all |
---|
12 | * copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __ZTTIME_H__ |
---|
24 | #define __ZTTIME_H__ |
---|
25 | |
---|
26 | #include "zthread/Config.h" |
---|
27 | |
---|
28 | namespace ZThread { |
---|
29 | |
---|
30 | /** |
---|
31 | * @class Time |
---|
32 | * @author Eric Crahen <http://www.code-foo.com> |
---|
33 | * @date <2003-07-16T17:52:46-0400> |
---|
34 | * @version 2.2.11 |
---|
35 | * |
---|
36 | * The Time class provides access to time values relative to when the program |
---|
37 | * was started. In other words, this class might be thought of as a timer that |
---|
38 | * starts at 0 and counts upwards. This class offers millisecond resolution. |
---|
39 | */ |
---|
40 | class ZTHREAD_API Time { |
---|
41 | |
---|
42 | unsigned long _seconds; |
---|
43 | unsigned long _milliseconds; |
---|
44 | |
---|
45 | //! Create a new Time object |
---|
46 | Time(unsigned long secs, unsigned long millis) |
---|
47 | : _seconds(secs), _milliseconds(millis) { } |
---|
48 | |
---|
49 | /** |
---|
50 | * Set the number of milliseconds in this Time object. |
---|
51 | * |
---|
52 | * @param millis - new milliseconds value |
---|
53 | * @return unsigned long old milliseconds value |
---|
54 | */ |
---|
55 | unsigned long milliseconds(unsigned long millis) { |
---|
56 | |
---|
57 | unsigned long n = _milliseconds; |
---|
58 | _milliseconds = millis; |
---|
59 | |
---|
60 | return n; |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Set the number of seconds in this Time object. |
---|
66 | * |
---|
67 | * @param secs - new seconds value |
---|
68 | * @return unsigned long old seconds value |
---|
69 | */ |
---|
70 | unsigned long seconds(unsigned long secs) { |
---|
71 | |
---|
72 | unsigned long n = _seconds; |
---|
73 | _seconds = secs; |
---|
74 | |
---|
75 | return n; |
---|
76 | |
---|
77 | } |
---|
78 | |
---|
79 | public: |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | * Create a Time object with the current time relative to the |
---|
84 | * beginning of the program. |
---|
85 | */ |
---|
86 | Time(); |
---|
87 | |
---|
88 | /** |
---|
89 | * Create a Time object by copying another. |
---|
90 | * |
---|
91 | * @param t - Time object to copy. |
---|
92 | */ |
---|
93 | Time(const Time& t) |
---|
94 | : _seconds(t._seconds), _milliseconds(t._milliseconds) { } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | * Get the number of milliseconds in this Time object. |
---|
99 | * |
---|
100 | * @return unsigned long milliseconds value |
---|
101 | */ |
---|
102 | unsigned long milliseconds() const { |
---|
103 | return _milliseconds; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Get the number of seconds in this Time object. |
---|
108 | * |
---|
109 | * @return unsigned long seconds value |
---|
110 | */ |
---|
111 | unsigned long seconds() const { |
---|
112 | return _seconds; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Add some number of milliseconds to this Time object. |
---|
117 | * |
---|
118 | * @param millis - number of milliseconds to add to this Time object |
---|
119 | * @return const Time& this object |
---|
120 | */ |
---|
121 | const Time& operator+=(unsigned long millis) { |
---|
122 | |
---|
123 | _milliseconds += millis; |
---|
124 | _seconds += (_milliseconds / 1000); |
---|
125 | _milliseconds %= 1000; |
---|
126 | |
---|
127 | return *this; |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Subtract some number of milliseconds to this Time object. |
---|
133 | * |
---|
134 | * @param millis - number of milliseconds to subtract from this Time object |
---|
135 | * @return const Time& this object |
---|
136 | */ |
---|
137 | const Time& operator-=(unsigned long millis) { |
---|
138 | |
---|
139 | if(_milliseconds > millis) |
---|
140 | _milliseconds -= millis; |
---|
141 | |
---|
142 | else { |
---|
143 | |
---|
144 | while(_seconds > 0 && _milliseconds < millis) { |
---|
145 | |
---|
146 | _milliseconds += 1000; |
---|
147 | _seconds -= 1; |
---|
148 | |
---|
149 | } |
---|
150 | |
---|
151 | _milliseconds = (_milliseconds < millis) ? 0 : (_milliseconds - millis); |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | return *this; |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * Add the value of another Time object to this one. |
---|
162 | * |
---|
163 | * @param t - Time object whose value should be added to this object |
---|
164 | * @return const Time& this object |
---|
165 | */ |
---|
166 | const Time& operator+=(const Time& t) { |
---|
167 | |
---|
168 | _milliseconds += t.milliseconds(); |
---|
169 | _seconds += (_milliseconds / 1000) + t.seconds(); |
---|
170 | _milliseconds %= 1000; |
---|
171 | |
---|
172 | return *this; |
---|
173 | |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Subtract the value of another Time object from this one. |
---|
178 | * This function has a floor of 0. |
---|
179 | * |
---|
180 | * @param t - Time object whose value should be subtracted from this object |
---|
181 | * @return const Time& this object |
---|
182 | */ |
---|
183 | const Time& operator-=(const Time& t) { |
---|
184 | |
---|
185 | unsigned long millis = t.milliseconds(); |
---|
186 | unsigned long secs = t.seconds(); |
---|
187 | |
---|
188 | if(_seconds >= secs) { |
---|
189 | |
---|
190 | if(_milliseconds > millis) { |
---|
191 | _milliseconds -= millis; |
---|
192 | _seconds -= secs; |
---|
193 | |
---|
194 | } else { |
---|
195 | |
---|
196 | while(_seconds > 0 && _milliseconds < millis) { |
---|
197 | |
---|
198 | _milliseconds += 1000; |
---|
199 | _seconds -= 1; |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | _milliseconds = (_milliseconds < millis) ? 0 : (_milliseconds - millis); |
---|
204 | _seconds = (_seconds < secs) ? 0 : (_seconds - secs); |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | } else { |
---|
209 | |
---|
210 | _milliseconds = 0; |
---|
211 | _seconds = 0; |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | return *this; |
---|
216 | |
---|
217 | } |
---|
218 | |
---|
219 | }; |
---|
220 | |
---|
221 | |
---|
222 | |
---|
223 | } // namespace ZThread |
---|
224 | |
---|
225 | #endif // __ZTTIME_H__ |
---|