xref: /openbmc/telemetry/tests/src/fakes/clock_fake.hpp (revision 7e098e93ef0974739459d296f99ddfab54722c23)
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     void reset(void) noexcept
31     {
32         set(Milliseconds(0));
33     }
34 
35     static uint64_t toTimestamp(Milliseconds time)
36     {
37         return time.count();
38     }
39 
40     static uint64_t toTimestamp(time_point tp)
41     {
42         return std::chrono::time_point_cast<Milliseconds>(tp)
43             .time_since_epoch()
44             .count();
45     }
46 
47   private:
48     time_point timePoint = std::chrono::steady_clock::now();
49 };
50