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