xref: /openbmc/telemetry/tests/src/fakes/clock_fake.hpp (revision 8069771c0db62887b72aa2b8c51cd64eb5f99b2d)
1 #pragma once
2 
3 #include "interfaces/clock.hpp"
4 #include "types/duration_type.hpp"
5 
6 class ClockFake : public interfaces::Clock
7 {
8   public:
9     time_point now() const noexcept override
10     {
11         return timePoint;
12     }
13 
14     uint64_t timestamp() const noexcept override
15     {
16         return toTimestamp(now());
17     }
18 
19     uint64_t advance(Milliseconds delta) noexcept
20     {
21         timePoint += delta;
22         return timestamp();
23     }
24 
25     void set(Milliseconds timeSinceEpoch) noexcept
26     {
27         timePoint = time_point{timeSinceEpoch};
28     }
29 
30     static uint64_t toTimestamp(Milliseconds time)
31     {
32         return time.count();
33     }
34 
35     static uint64_t toTimestamp(time_point tp)
36     {
37         return std::chrono::time_point_cast<Milliseconds>(tp)
38             .time_since_epoch()
39             .count();
40     }
41 
42   private:
43     time_point timePoint = std::chrono::steady_clock::now();
44 };
45