OSVR-Core
TimeValue.h
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 #ifndef INCLUDED_TimeValue_h_GUID_AD9F3D81_382D_4394_433B_A8026BE803B6
26 #define INCLUDED_TimeValue_h_GUID_AD9F3D81_382D_4394_433B_A8026BE803B6
27 
28 // Internal Includes
29 #include <osvr/Util/TimeValueC.h>
31 
32 // Library/third-party includes
33 // - none
34 
35 // Standard includes
36 #include <iomanip>
37 #include <sstream>
38 
39 namespace osvr {
40 namespace util {
46  namespace time {
49 
51  inline void getNow(TimeValue &tv) { osvrTimeValueGetNow(&tv); }
52 
54  inline TimeValue getNow() {
55  TimeValue tv;
56  getNow(tv);
57  return tv;
58  }
59 
62  inline double duration(TimeValue const &a, TimeValue const &b) {
63  return osvrTimeValueDurationSeconds(&a, &b);
64  }
65 
67  inline std::string toDecimalString(TimeValue tv) {
68  std::ostringstream os;
70  // Ensure we have only non-negative components, pulling out the
71  // negatives. (Normalize ensures we have 0 or the same sign between
72  // components)
73  bool negative = false;
74  if (tv.seconds < 0) {
75  negative = true;
76  tv.seconds *= -1;
77  }
78  if (tv.microseconds < 0) {
79  negative = true;
80  tv.microseconds *= -1;
81  }
82  // Output sign
83  if (negative) {
84  os << "-";
85  }
86  // Output seconds
87  os << tv.seconds << ".";
88  // set up decimal padding
89  os << std::setw(6) << std::setfill('0');
90  // Output microseconds
91  os << tv.microseconds;
92 
93  return os.str();
94  }
95 
96 #ifdef OSVR_HAVE_STRUCT_TIMEVAL
97  inline void toStructTimeval(struct timeval &dest,
99  TimeValue const &src) {
100  osvrTimeValueToStructTimeval(&dest, &src);
101  }
102 
104  inline void fromStructTimeval(TimeValue &dest,
105  struct timeval const &src) {
106  osvrStructTimevalToTimeValue(&dest, &src);
107  }
108 
110  inline TimeValue toTimeValue(struct timeval const &src) {
111  TimeValue dest;
112  osvrStructTimevalToTimeValue(&dest, &src);
113  return dest;
114  }
116  inline TimeValue fromStructTimeval(struct timeval const &src) {
117  return toTimeValue(src);
118  }
119 
120 #ifdef OSVR_STRUCT_TIMEVAL_INCLUDED
121  inline struct timeval toStructTimeval(TimeValue const &src) {
123  struct timeval dest;
124  osvrTimeValueToStructTimeval(&dest, &src);
125  return dest;
126  }
127 #endif // OSVR_STRUCT_TIMEVAL_INCLUDED
128 
129 #endif // OSVR_HAVE_STRUCT_TIMEVAL
130 
131  } // namespace time
132 } // namespace util
133 } // namespace osvr
134 
135 #endif // INCLUDED_TimeValue_h_GUID_AD9F3D81_382D_4394_433B_A8026BE803B6
Definition: RunLoopManager.h:42
void getNow(TimeValue &tv)
Set the given TimeValue to the current time.
Definition: TimeValue.h:51
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT void OSVR_UTIL_EXPORT int OSVR_EXTERN_C_END OSVR_INLINE double osvrTimeValueDurationSeconds(OSVR_IN_PTR const OSVR_TimeValue *tvA, OSVR_IN_PTR const OSVR_TimeValue *tvB)
Compute the difference between the two time values, returning the duration as a double-precision floa...
Definition: TimeValueC.h:185
::OSVR_TimeValue TimeValue
C++-friendly typedef for the OSVR_TimeValue structure.
Definition: TimeValue.h:48
double duration(TimeValue const &a, TimeValue const &b)
Get a double containing seconds between the time points.
Definition: TimeValue.h:62
OSVR_UTIL_EXPORT void osvrTimeValueNormalize(OSVR_INOUT_PTR OSVR_TimeValue *tv) OSVR_FUNC_NONNULL((1))
"Normalizes" a time value so that the absolute number of microseconds is less than 1...
Definition: TimeValueC.cpp:46
OSVR_TimeValue_Microseconds microseconds
Microseconds portion of the time value.
Definition: TimeValueC.h:85
std::string toDecimalString(TimeValue tv)
Converts to a precise decimal string.
Definition: TimeValue.h:67
Header defining a dependency-free, cross-platform substitute for struct timeval.
Header forward-declaring TimeValue C++ API.
struct OSVR_TimeValue OSVR_TimeValue
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
OSVR_TimeValue_Seconds seconds
Seconds portion of the time value.
Definition: TimeValueC.h:83