OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
TimeValueC.cpp
Go to the documentation of this file.
1 
11 // Copyright 2014 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
26 #include <osvr/Util/TimeValueC.h>
27 
28 // Library/third-party includes
29 #include <vrpn_Shared.h>
30 
31 // Standard includes
32 #include <ratio>
33 
34 #if defined(OSVR_HAVE_STRUCT_TIMEVAL_IN_SYS_TIME_H)
35 #include <sys/time.h>
36 typedef time_t tv_seconds_type;
37 typedef suseconds_t tv_microseconds_type;
38 #elif defined(OSVR_HAVE_STRUCT_TIMEVAL_IN_WINSOCK2_H)
39 //#include <winsock2.h>
40 typedef long tv_seconds_type;
41 typedef long tv_microseconds_type;
42 #endif
43 
44 #define OSVR_USEC_PER_SEC std::micro::den;
45 
46 void osvrTimeValueNormalize(OSVR_INOUT_PTR OSVR_TimeValue *tv) {
47  if (!tv) {
48  return;
49  }
50  const OSVR_TimeValue_Microseconds rem =
51  tv->microseconds / OSVR_USEC_PER_SEC;
52  tv->seconds += rem;
53  tv->microseconds -= rem * OSVR_USEC_PER_SEC;
54  /* By here, abs(microseconds) < OSVR_USEC_PER_SEC:
55  now let's get signs the same. */
56  if (tv->seconds > 0 && tv->microseconds < 0) {
57  tv->seconds--;
58  tv->microseconds += OSVR_USEC_PER_SEC;
59  } else if (tv->seconds < 0 && tv->microseconds > 0) {
60  tv->seconds++;
61  tv->microseconds -= OSVR_USEC_PER_SEC;
62  }
63 }
64 
65 void osvrTimeValueSum(OSVR_INOUT_PTR OSVR_TimeValue *tvA,
66  OSVR_IN_PTR const OSVR_TimeValue *tvB) {
67  if (!tvA || !tvB) {
68  return;
69  }
70  tvA->seconds += tvB->seconds;
71  tvA->microseconds += tvB->microseconds;
73 }
74 
75 void osvrTimeValueDifference(OSVR_INOUT_PTR OSVR_TimeValue *tvA,
76  OSVR_IN_PTR const OSVR_TimeValue *tvB) {
77  if (!tvA || !tvB) {
78  return;
79  }
80  tvA->seconds -= tvB->seconds;
81  tvA->microseconds -= tvB->microseconds;
83 }
84 
85 template <typename T> inline int numcmp(T a, T b) {
86  return (a == b) ? 0 : (a < b ? -1 : 1);
87 }
88 
89 int osvrTimeValueCmp(OSVR_IN_PTR const OSVR_TimeValue *tvA,
90  OSVR_IN_PTR const OSVR_TimeValue *tvB) {
91  if (!tvA || !tvB) {
92  return 0;
93  }
94  auto major = numcmp(tvA->seconds, tvB->seconds);
95  return (major != 0) ? major : numcmp(tvA->microseconds, tvB->microseconds);
96 }
97 
98 #ifdef OSVR_HAVE_STRUCT_TIMEVAL
99 
100 void osvrTimeValueGetNow(OSVR_INOUT_PTR OSVR_TimeValue *dest) {
101  timeval tv;
102  vrpn_gettimeofday(&tv, nullptr);
103  osvrStructTimevalToTimeValue(dest, &tv);
104 }
105 
106 void osvrTimeValueToStructTimeval(OSVR_OUT timeval *dest,
107  OSVR_IN_PTR const OSVR_TimeValue *src) {
108  if (!dest || !src) {
109  return;
110  }
111  dest->tv_sec = tv_seconds_type(src->seconds);
112  dest->tv_usec = tv_microseconds_type(src->microseconds);
113 }
114 
115 void osvrStructTimevalToTimeValue(OSVR_OUT OSVR_TimeValue *dest,
116  OSVR_IN_PTR const timeval *src) {
117  if (!dest || !src) {
118  return;
119  }
120  dest->seconds = src->tv_sec;
121  dest->microseconds = src->tv_usec;
123 }
124 
125 #endif
void osvrTimeValueGetNow(OSVR_TimeValue *dest)
Gets the current time in the TimeValue. Parallel to gettimeofday.
Definition: TimeValueC.cpp:100
int32_t OSVR_TimeValue_Microseconds
The signed integer type storing the microseconds in a struct OSVR_TimeValue.
Definition: TimeValueC.h:69
void osvrTimeValueToStructTimeval(timeval *dest, const OSVR_TimeValue *src)
Converts from a TimeValue struct to your system's struct timeval.
Definition: TimeValueC.cpp:106
void osvrTimeValueDifference(OSVR_TimeValue *tvA, const OSVR_TimeValue *tvB)
Computes the difference between two time values, replacing the first with the result.
Definition: TimeValueC.cpp:75
void osvrStructTimevalToTimeValue(OSVR_TimeValue *dest, const struct timeval *src)
Converts from a TimeValue struct to your system's struct timeval.
Header defining a dependency-free, cross-platform substitute for struct timeval.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
void osvrTimeValueSum(OSVR_TimeValue *tvA, const OSVR_TimeValue *tvB)
Sums two time values, replacing the first with the result.
Definition: TimeValueC.cpp:65
int osvrTimeValueCmp(const OSVR_TimeValue *tvA, const OSVR_TimeValue *tvB)
Compares two time values (assumed to be normalized), returning the same values as strcmp...
Definition: TimeValueC.cpp:89
void osvrTimeValueNormalize(OSVR_TimeValue *tv)
"Normalizes" a time value so that the absolute number of microseconds is less than 1...
Definition: TimeValueC.cpp:46