Clock.cpp
Go to the documentation of this file.
1 #include "Clock.h"
2 
3 #include <unistd.h>
4 #include <limits.h>
5 
6 #include <chrono>
7 
11 
12 
14 {
15 
16  Clock::Clock(ClockType clockType) : _clockType{clockType}
17  {
18  ;
19  }
20 
21 
22  DateTime
23  Clock::now() const
24  {
26 
27  std::chrono::microseconds timestamp_usec;
28 
29  switch (_clockType)
30  {
31  case ClockType::Virtual:
32  {
33  if (timeServer)
34  {
35  timestamp_usec = std::chrono::milliseconds{timeServer->getTime()};
36  break;
37  }
38  else
39  {
40  // If no time server is set, use realtime instead.
41  [[fallthrough]];
42  }
43  }
45  {
46  timestamp_usec = std::chrono::time_point_cast<std::chrono::microseconds>(
47  std::chrono::system_clock::now())
48  .time_since_epoch();
49  break;
50  }
52  {
53  timestamp_usec = std::chrono::time_point_cast<std::chrono::microseconds>(
54  std::chrono::steady_clock::now())
55  .time_since_epoch();
56  break;
57  }
58  case ClockType::Unknown:
59  {
60  throw std::runtime_error{"Cannot asses current time from unknown clock."};
61  }
62  }
63 
64  const std::string hostname = []{
65  char hostname[HOST_NAME_MAX];
66  gethostname(hostname, HOST_NAME_MAX);
67  return std::string(hostname);
68  }();
69 
70  return DateTime(Duration::MicroSeconds(timestamp_usec.count()), _clockType, hostname);
71  }
72 
73 
74  void
75  Clock::waitFor(const Duration& duration) const
76  {
78  }
79 
80 
81  Duration
82  Clock::waitUntil(const DateTime& time) const
83  {
84  const Duration difference = time - now();
85  if (difference.isPositive())
86  {
87  waitFor(difference);
88  }
89  return difference;
90  }
91 
92 
93  Clock Clock::_virtualClock = Clock(ClockType::Virtual);
94 
95 
96  DateTime
98  {
99  return Clock::_virtualClock.now();
100  }
101 
102 
103  void
104  Clock::WaitFor(const Duration& duration)
105  {
106  Clock::_virtualClock.waitFor(duration);
107  }
108 
109 
110  Duration
111  Clock::WaitUntil(const DateTime& dateTime)
112  {
113  return Clock::_virtualClock.waitUntil(dateTime);
114  }
115 
116 } // namespace armarx::core::time
armarx::TimeUtil::SleepUS
static void SleepUS(float microseconds)
Definition: TimeUtil.h:199
armarx::core::time::Clock::WaitFor
static void WaitFor(const Duration &duration)
Wait for a certain duration on the virtual clock.
Definition: Clock.cpp:104
armarx::core::time
Definition: Clock.cpp:13
armarx::core::time::Clock::waitUntil
Duration waitUntil(const DateTime &dateTime) const
Wait and block until the given date/time is surpassed.
Definition: Clock.cpp:82
ice_conversions.h
armarx::core::time::Clock
Clock to get date/time from a specific clock type or wait for certain durations or until certain date...
Definition: Clock.h:26
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:195
Clock.h
armarx::TimeUtil::GetTimeServer
static LocalTimeServerPtr GetTimeServer()
Definition: TimeUtil.cpp:109
IceInternal::Handle
Definition: forward_declarations.h:8
LocalTimeServer.h
armarx::core::time::ClockType
ClockType
Describes the type of clock.
Definition: ClockType.h:10
armarx::core::time::ClockType::Unknown
@ Unknown
Unknown source of time.
armarx::core::time::ClockType::Realtime
@ Realtime
Normalized time as reported by the operating system.
armarx::core::time::Clock::Clock
Clock(ClockType clockType=ClockType::Virtual)
Constructs a new clock of given clock type (virtual by default).
Definition: Clock.cpp:16
armarx::core::time::Clock::waitFor
void waitFor(const Duration &duration) const
Wait for a certain duration.
Definition: Clock.cpp:75
armarx::core::time::ClockType::Virtual
@ Virtual
Time given by time server if configured, realtime otherwise.
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
TimeUtil.h
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
armarx::core::time::Clock::WaitUntil
static Duration WaitUntil(const DateTime &dateTime)
Wait and block until the given date/time is surpassed on the virtual clock.
Definition: Clock.cpp:111
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:97
armarx::core::time::Duration::toMicroSeconds
std::int64_t toMicroSeconds() const
Returns the amount of microseconds.
Definition: Duration.cpp:41
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
armarx::core::time::ClockType::Monotonic
@ Monotonic
Monotonic/steady clock of the operating system.
stopwatch::Clock
std::chrono::system_clock Clock
Definition: Stopwatch.h:10
armarx::core::time::Clock::now
DateTime now() const
Current date/time of the clock.
Definition: Clock.cpp:23