libgpiod  2.1.2
timestamp.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /* SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl> */
3 
8 #ifndef __LIBGPIOD_CXX_TIMESTAMP_HPP__
9 #define __LIBGPIOD_CXX_TIMESTAMP_HPP__
10 
11 #if !defined(__LIBGPIOD_GPIOD_CXX_INSIDE__)
12 #error "Only gpiod.hpp can be included directly."
13 #endif
14 
15 #include <chrono>
16 #include <cstdint>
17 
18 namespace gpiod {
19 
29 class timestamp final
30 {
31 public:
32 
36  using time_point_monotonic = ::std::chrono::time_point<::std::chrono::steady_clock>;
37 
41  using time_point_realtime = ::std::chrono::time_point<::std::chrono::system_clock,
42  ::std::chrono::nanoseconds>;
43 
48  timestamp(::std::uint64_t ns) : _m_ns(ns) { }
49 
54  timestamp(const timestamp& other) noexcept = default;
55 
60  timestamp(timestamp&& other) noexcept = default;
61 
67  timestamp& operator=(const timestamp& other) noexcept = default;
68 
74  timestamp& operator=(timestamp&& other) noexcept = default;
75 
76  ~timestamp() = default;
77 
81  operator ::std::uint64_t() noexcept
82  {
83  return this->ns();
84  }
85 
90  ::std::uint64_t ns() const noexcept
91  {
92  return this->_m_ns;
93  }
94 
100  {
101  return time_point_monotonic(::std::chrono::nanoseconds(this->ns()));
102  }
103 
109  {
110  return time_point_realtime(::std::chrono::nanoseconds(this->ns()));
111  }
112 
113 private:
114  ::std::uint64_t _m_ns;
115 };
116 
121 } /* namespace gpiod */
122 
123 #endif /* __LIBGPIOD_CXX_TIMESTAMP_HPP__ */
time_point_realtime to_time_point_realtime() const
Convert the timestamp to a real-time time_point.
Definition: timestamp.hpp:108
::std::chrono::time_point<::std::chrono::system_clock,::std::chrono::nanoseconds > time_point_realtime
Real-time time_point.
Definition: timestamp.hpp:42
Stores the edge and info event timestamps as returned by the kernel and allows to convert them to std...
Definition: timestamp.hpp:29
time_point_monotonic to_time_point_monotonic() const
Convert the timestamp to a monotonic time_point.
Definition: timestamp.hpp:99
::std::chrono::time_point<::std::chrono::steady_clock > time_point_monotonic
Monotonic time_point.
Definition: timestamp.hpp:36
timestamp(::std::uint64_t ns)
Constructor with implicit conversion from uint64_t.
Definition: timestamp.hpp:48
::std::uint64_t ns() const noexcept
Get the timestamp in nanoseconds.
Definition: timestamp.hpp:90