YARP
Yet Another Robot Platform
PlatformTime.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
11 
12 #include <yarp/os/Time.h>
13 #include <chrono>
14 #include <cmath>
15 #include <thread>
16 
17 //time helper functions
19  if (Time::isSystemClock()) {
20 #ifdef YARP_HAS_ACE
21  now = ACE_OS::gettimeofday();
22 #else
23  struct timezone *tz = nullptr;
24  gettimeofday(&now, tz);
25 #endif
26  } else {
27 #ifdef YARP_HAS_ACE
28  now.set(Time::now());
29 #else
30  double t = Time::now();
31  now.tv_sec = lround(t);
32  now.tv_usec = lround((t-now.tv_sec)*1e6);
33 #endif
34  }
35 }
36 
37 
39  if (Time::isSystemClock()) {
40  std::this_thread::sleep_for(std::chrono::duration<double>(toDouble(sleep_period)));
41  } else {
42  Time::delay(toDouble(sleep_period));
43  }
44 }
45 
46 
48 #ifdef YARP_HAS_ACE
49  val += add;
50 #else
51  val.tv_usec += add.tv_usec;
52  int over = val.tv_usec % 1000000;
53  if (over != val.tv_usec) {
54  val.tv_usec = over;
55  val.tv_sec++;
56  }
57  val.tv_sec += add.tv_sec;
58 #endif
59 }
60 
61 
63 #ifdef YARP_HAS_ACE
64  val -= subtract;
65 #else
66  if (val.tv_usec > subtract.tv_usec) {
67  val.tv_usec -= subtract.tv_usec;
68  val.tv_sec -= subtract.tv_sec;
69  return;
70  }
71  int over = 1000000 + val.tv_usec - subtract.tv_usec;
72  val.tv_usec = over;
73  val.tv_sec--;
74  val.tv_sec -= subtract.tv_sec;
75 #endif
76 }
77 
78 
80 #ifdef YARP_HAS_ACE
81  return double(v.sec()) + v.usec() * 1e-6;
82 #else
83  return double(v.tv_sec) + v.tv_usec * 1e-6;
84 #endif
85 }
86 
87 
88 void yarp::os::impl::fromDouble(YARP_timeval &v, double x, int unit) {
89 #ifdef YARP_HAS_ACE
90  v.msec(lround(x*1000/unit+0.5));
91 #else
92  v.tv_usec = lround(x*1000000/unit+0.5) % 1000000;
93  v.tv_sec = lround(x/unit);
94 #endif
95 }
yarp::os::impl::getTime
void getTime(YARP_timeval &now)
Definition: PlatformTime.cpp:18
t
float t
Definition: FfmpegWriter.cpp:74
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::os::impl::subtractTime
void subtractTime(YARP_timeval &val, const YARP_timeval &subtract)
Definition: PlatformTime.cpp:62
yarp::os::impl::fromDouble
void fromDouble(YARP_timeval &v, double x, int unit=1)
Definition: PlatformTime.cpp:88
yarp::os::impl::addTime
void addTime(YARP_timeval &val, const YARP_timeval &add)
Definition: PlatformTime.cpp:47
yarp::os::impl::toDouble
double toDouble(const YARP_timeval &v)
Definition: PlatformTime.cpp:79
yarp::os::impl::YARP_timeval
struct timeval YARP_timeval
Definition: PlatformTime.h:35
yarp::os::Time::isSystemClock
bool isSystemClock()
Check if YARP is providing system time.
Definition: Time.cpp:265
Time.h
PlatformTime.h
yarp::os::Time::delay
void delay(double seconds)
Wait for a certain number of seconds.
Definition: Time.cpp:114
yarp::os::impl::sleepThread
void sleepThread(YARP_timeval &sleep_period)
Definition: PlatformTime.cpp:38