Loading...
Searching...
No Matches
chrono.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Hamburg University of Applied Sciences (HAW)
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
25#ifndef RIOT_CHRONO_HPP
26#define RIOT_CHRONO_HPP
27
28#include <chrono>
29#include <algorithm>
30
31#include "time.h"
32#include "timex.h"
33#include "ztimer64.h"
34
35namespace riot {
36
43
44 public:
48 inline time_point() : m_handle{0, 0} {}
52 explicit inline time_point(timex_t&& tp) : m_handle(tp) {}
56 constexpr time_point(const time_point& tp) = default;
60 constexpr time_point(time_point&& tp) = default;
61
65 inline native_handle_type native_handle() const { return m_handle; }
66
70 template <class Rep, class Period>
71 inline time_point& operator+=(const std::chrono::duration<Rep, Period>& d) {
72 auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
73 auto m = (std::chrono::duration_cast<std::chrono::microseconds>(d) - s);
74 m_handle.seconds += s.count();
75 m_handle.microseconds += m.count();
76 adjust_overhead();
77 return *this;
78 }
79
83 inline uint32_t seconds() const { return m_handle.seconds; }
84
88 inline uint32_t microseconds() const { return m_handle.microseconds; }
89
90 private:
91 timex_t m_handle;
92 void inline adjust_overhead() {
93 auto secs = m_handle.microseconds / US_PER_SEC;
94 m_handle.seconds += secs;
95 m_handle.microseconds -= (secs * US_PER_SEC);
96 }
97};
98
104inline time_point now() {
105 timex_t tp;
107 return time_point(std::move(tp));
108}
109
113inline bool operator<(const time_point& lhs, const time_point& rhs) {
114 return lhs.seconds() < rhs.seconds()
115 || (lhs.seconds() == rhs.seconds() && lhs.microseconds()
116 < rhs.microseconds());
117}
118
122inline bool operator>(const time_point& lhs, const time_point& rhs) {
123 return rhs < lhs;
124}
125
129inline bool operator<=(const time_point& lhs, const time_point& rhs) {
130 return !(rhs < lhs);
131}
132
136inline bool operator>=(const time_point& lhs, const time_point& rhs) {
137 return !(lhs < rhs);
138}
139
140} // namespace riot
141
142#endif // RIOT_CHRONO_HPP
A time point for timed wait, as clocks from the standard are not available on RIOT.
Definition chrono.hpp:41
native_handle_type native_handle() const
Gives access to the native handle that stores the time information.
Definition chrono.hpp:65
constexpr time_point(time_point &&tp)=default
Use default move constructor.
time_point & operator+=(const std::chrono::duration< Rep, Period > &d)
Add a standard chrono::duration to this time point.
Definition chrono.hpp:71
time_point()
Creates a time point with seconds and microseconds set to 0.
Definition chrono.hpp:48
uint32_t microseconds() const
Returns microseconds member as uint32_t.
Definition chrono.hpp:88
uint32_t seconds() const
Returns seconds member as uint32_t.
Definition chrono.hpp:83
time_point(timex_t &&tp)
Create time point from timex_t struct.
Definition chrono.hpp:52
constexpr time_point(const time_point &tp)=default
Use default copy constructor.
#define US_PER_SEC
The number of microseconds per second.
Definition time_units.h:85
static timex_t timex_from_uint64(const uint64_t timestamp)
Converts a 64 bit value of microseconds to a timex timestamp.
Definition timex.h:139
uint64_t ztimer64_now(ztimer64_clock_t *clock)
Get the current time from a clock.
ztimer64_clock_t *const ZTIMER64_USEC
Default ztimer microsecond clock.
RIOT C++ namespace.
Definition chrono.hpp:35
bool operator<=(const time_point &lhs, const time_point &rhs)
Compares two timepoints.
Definition chrono.hpp:129
bool operator>(const time_point &lhs, const time_point &rhs)
Compares two timepoints.
Definition chrono.hpp:122
time_point now()
Returns the current time saved in a time point.
Definition chrono.hpp:104
bool operator<(const time_point &lhs, const time_point &rhs)
Compares two timepoints.
Definition chrono.hpp:113
bool operator>=(const time_point &lhs, const time_point &rhs)
Compare two timepoints.
Definition chrono.hpp:136
A timex timestamp.
Definition timex.h:49
uint32_t seconds
number of seconds
Definition timex.h:50
uint32_t microseconds
number of microseconds
Definition timex.h:51
Utility library for comparing and computing timestamps.
ztimer 64bit API