Debian Codegraph @

lib/database/src/sb/SbTime.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.6 $
44 |
45 | Classes:
46 | SbTime
47 |
48 | Author(s) : Nick Thompson
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <machine.h>
55 #include <Inventor/SbTime.h>
56 #include <time.h>
57 #ifndef __STDC_FORMAT_MACROS
58 #define __STDC_FORMAT_MACROS
59 #endif
60 #include <inttypes.h>
61
62 #ifdef _CRAY
63 extern "C" {
64 int gettimeofday(struct timeval *tp,struct timezone *tzp);
65 }
66 #endif /* _CRAY */
67
68 ////////////////////////////////////////////////////////////////////////
69 //
70 // Description:
71 // Constructor that takes the number of seconds as a double
72 //
73 // Use: public
74
75 SbTime::SbTime(double sec)
76 //
77 ////////////////////////////////////////////////////////////////////////
78 {
79 if (sec >= 0) {
80 t.tv_sec = int(sec);
81 t.tv_usec = (time_t) (0.5 + (sec - t.tv_sec) * 1000000.0);
82 }
83 else
84 *this = -SbTime(-sec);
85 }
86
87
88 ////////////////////////////////////////////////////////////////////////
89 //
90 // Description:
91 // Returns the current time in an SbTime.
92 //
93 // Use: public
94
95 SbTime
96 SbTime::getTimeOfDay()
97 //
98 ////////////////////////////////////////////////////////////////////////
99 {
100 SbTime tm;
101
102 if (-1 == gettimeofday(&tm.t, NULL))
103 perror("gettimeofday");
104
105 return tm;
106 }
107
108 ////////////////////////////////////////////////////////////////////////
109 //
110 // Description:
111 // Sets to the current time.
112 //
113 // Use: public
114
115 void
116 SbTime::setToTimeOfDay()
117 //
118 ////////////////////////////////////////////////////////////////////////
119 {
120 if (-1 == gettimeofday(&t, NULL))
121 perror("gettimeofday");
122 }
123
124 ////////////////////////////////////////////////////////////////////////
125 //
126 // Description:
127 // Converts to a formatted string. The format string supports the following:
128 //
129 // %% the '%' character
130 // %D total number of days
131 // %H total number of hours
132 // %M total number of minutes
133 // %S total number of seconds
134 // %I total number of milliseconds
135 // %U total number of microseconds
136 // %h 00-23 hours remaining after the days
137 // %m 00-59 minutes remaining after the hours
138 // %s 00-59 seconds remaining after the minutes
139 // %i 000-999 milliseconds remaining after the seconds
140 // %u 000000-999999 microseconds remaining after the seconds
141 //
142 // uppercase descriptors are formatted with a leading '-' for negative times
143 // lowercase descriptors are formatted fixed width with leading zeros
144 //
145 // Use: public
146
147 SbString
148 SbTime::format(const char *fmt) const
149 //
150 ////////////////////////////////////////////////////////////////////////
151 {
152 SbBool negative;
153 struct timeval tv;
154
155 // turn into sign-magnitude form
156 if (t.tv_sec >= 0) {
157 negative = 0;
158 tv = t;
159 }
160 else {
161 negative = 1;
162 tv = (-(*this)).t;
163 }
164
165 // first calculate total durations
166 const int32_t tday = tv.tv_sec / (60*60*24);
167 const int32_t thour = tv.tv_sec / (60*60);
168 const int32_t tmin = tv.tv_sec / 60;
169 const int32_t tsec = tv.tv_sec;
170 const int32_t tmilli = 1000*tv.tv_sec + tv.tv_usec / 1000;
171 const int32_t tmicro = 1000000*tv.tv_sec + tv.tv_usec;
172
173 // then calculate remaining durations
174 const int32_t rhour = thour - 24*tday;
175 const int32_t rmin = tmin - 60*thour;
176 const int32_t rsec = tsec - 60*tmin;
177 const int32_t rmilli = tmilli - 1000*tsec;
178 const int32_t rmicro = tmicro - 1000000*tsec;
179
180 char buf[200];
181 char *s = buf;
182
183 for (; *fmt; fmt++) {
184 if (*fmt != '%')
185 *s++ = *fmt;
186 else
187 switch(*++fmt) {
188 case 0:
189 fmt--; // trailing '%' in format string
190 break;
191
192 case '%':
193 *s++ = '%'; // "%%" in format string
194 break;
195
196 case 'D':
197 if (negative) *s++ = '-';
198 s += sprintf(s, "%" PRId32, tday);
199 break;
200
201 case 'H':
202 if (negative) *s++ = '-';
203 s += sprintf(s, "%" PRId32, thour);
204 break;
205
206 case 'M':
207 if (negative) *s++ = '-';
208 s += sprintf(s, "%" PRId32, tmin);
209 break;
210
211 case 'S':
212 if (negative) *s++ = '-';
213 s += sprintf(s, "%" PRId32, tsec);
214 break;
215
216 case 'I':
217 if (negative) *s++ = '-';
218 s += sprintf(s, "%" PRId32, tmilli);
219 break;
220
221 case 'U':
222 if (negative) *s++ = '-';
223 s += sprintf(s, "%" PRId32, tmicro);
224 break;
225
226 case 'h':
227 s += sprintf(s, "%.2" PRId32, rhour);
228 break;
229
230 case 'm':
231 s += sprintf(s, "%.2" PRId32, rmin);
232 break;
233
234 case 's':
235 s += sprintf(s, "%.2" PRId32, rsec);
236 break;
237
238 case 'i':
239 s += sprintf(s, "%.3" PRId32, rmilli);
240 break;
241
242 case 'u':
243 s += sprintf(s, "%.6" PRId32, rmicro);
244 break;
245
246 default:
247 *s++ = '%'; // echo any bad '%?'
248 *s++ = *fmt; // specifier
249 }
250 if (s-buf >= sizeof(buf)-7) // don't overshoot the buffer
251 break;
252 }
253 *s = 0;
254
255 return buf;
256 }
257
258 ////////////////////////////////////////////////////////////////////////
259 //
260 // Description:
261 // Formats as an absolute date/time, using unix "strftime" mechanism.
262 //
263 // Use: public
264
265 SbString
266 SbTime::formatDate(const char *fmt) const
267 //
268 ////////////////////////////////////////////////////////////////////////
269 {
270 char buf[200];
271
272 time_t seconds = t.tv_sec;
273 strftime(buf, sizeof(buf), fmt, localtime(&seconds));
274
275 return buf;
276 }
277
278 ////////////////////////////////////////////////////////////////////////
279 //
280 // Description:
281 //
282 // Use: public
283
284 SbTime
285 operator +(const SbTime &t0, const SbTime &t1)
286 //
287 ////////////////////////////////////////////////////////////////////////
288 {
289 SbTime tm(t0.t.tv_sec + t1.t.tv_sec,
290 t0.t.tv_usec + t1.t.tv_usec);
291 if (tm.t.tv_usec >= 1000000) {
292 tm.t.tv_sec += 1;
293 tm.t.tv_usec -= 1000000;
294 }
295 return tm;
296 }
297
298 ////////////////////////////////////////////////////////////////////////
299 //
300 // Description:
301 //
302 // Use: public
303
304 SbTime
305 operator -(const SbTime &t0, const SbTime &t1)
306 //
307 ////////////////////////////////////////////////////////////////////////
308 {
309 int32_t sec;
310 long usec; // System long
311
312 sec = t0.t.tv_sec - t1.t.tv_sec;
313 usec = t0.t.tv_usec - t1.t.tv_usec;
314
315 while (usec < 0 && sec > 0) {
316 usec += 1000000;
317 sec -= 1;
318 }
319
320 return SbTime(sec, usec);
321 }
322
323 ////////////////////////////////////////////////////////////////////////
324 //
325 // Description:
326 //
327 // Use: public
328
329 SbTime
330 operator *(const SbTime &tm, double s)
331 //
332 ////////////////////////////////////////////////////////////////////////
333 {
334 return SbTime(tm.getValue() * s);
335 }
336
337 ////////////////////////////////////////////////////////////////////////
338 //
339 // Description:
340 //
341 // Use: public
342
343 SbTime
344 operator /(const SbTime &tm, double s)
345 //
346 ////////////////////////////////////////////////////////////////////////
347 {
348 return tm * (1.0 / s);
349 }
350