1 /*****************************************************************************
2 * DynaMechs: A Multibody Dynamic Simulation Library
3 *
4 * Copyright (C) 1994-2001 Scott McMillan All Rights Reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *****************************************************************************
20 * File: dmTime.h
21 * Author: Scott McMillan
22 * Created: 30 June 1999
23 * Summary: Cross platform timing for test programs
24 *****************************************************************************/
25
26 #ifndef __DM_TIME_H
27 #define __DM_TIME_H
28
29 #include <dm.h>
30
31 //----------------------------------------------------------------------------
32 #if defined(WIN32)
33 # include <sys/timeb.h>
34 # include <time.h>
35
36 struct dmTimespec
37 {
38 time_t tv_sec; // seconds
39 long tv_nsec; // and nanoseconds
40 };
41
42 #else
43 # include <unistd.h>
44 # if defined(_POSIX_TIMERS)
45 # include <time.h>
46 # else
47 # include <sys/time.h>
48 # endif
49 typedef timespec dmTimespec;
50 #endif
51
dmGetSysTime(dmTimespec * ts)52 inline void dmGetSysTime(dmTimespec *ts)
53 {
54 #if defined(WIN32)
55 struct _timeb timebuffer;
56 _ftime(&timebuffer);
57
58 ts->tv_sec = timebuffer.time;
59 ts->tv_nsec = timebuffer.millitm*1000000;
60
61 #elif defined(_POSIX_TIMERS)
62 if (clock_gettime(CLOCK_REALTIME, ts) != 0)
63 {
64 throw ts;
65 }
66 #else
67 struct timeval tv;
68 gettimeofday(&tv, NULL);
69 ts->tv_sec = tv.tv_sec;
70 ts->tv_nsec = 1000*tv.tv_usec;
71 #endif
72 }
73
dmTimespecToDouble(dmTimespec * ts)74 inline double dmTimespecToDouble(dmTimespec *ts)
75 {
76 return (double)ts->tv_sec + ((double)ts->tv_nsec * 1.0e-9);
77 }
78
79 #endif
80