Loading...
Searching...
No Matches
time_point.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2025 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_IN_CHRONO_H
#error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H
#endif
namespace etl
{
namespace chrono
{
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration = typename TClock::duration>
class time_point
{
public:
using clock = TClock;
using duration = TDuration;
using rep = typename TDuration::rep;
using period = typename TDuration::period;
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR time_point() ETL_NOEXCEPT
: dur(duration::zero())
{
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 explicit time_point(const duration& dur_) ETL_NOEXCEPT
: dur(dur_)
{
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point(const time_point& rhs) ETL_NOEXCEPT
: dur(rhs.dur)
{
}
//***************************************************************************
//***************************************************************************
template <typename TDuration2>
ETL_CONSTEXPR14 explicit time_point(const time_point<clock, TDuration2>& rhs) ETL_NOEXCEPT
: dur(rhs.time_since_epoch())
{
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point& operator=(const time_point& rhs) ETL_NOEXCEPT
{
dur = rhs.dur;
return *this;
}
//***************************************************************************
//***************************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
{
return dur;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point& operator+=(const duration& rhs) ETL_NOEXCEPT
{
dur += rhs;
return *this;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point& operator-=(const duration& rhs) ETL_NOEXCEPT
{
dur -= rhs;
return *this;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point& operator++() ETL_NOEXCEPT
{
++dur;
return *this;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point operator++(int) ETL_NOEXCEPT
{
time_point temp(*this);
++dur;
return temp;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point& operator--() ETL_NOEXCEPT
{
--dur;
return *this;
}
//***************************************************************************
//***************************************************************************
ETL_CONSTEXPR14 time_point operator--(int) ETL_NOEXCEPT
{
time_point temp(*this);
--dur;
return temp;
}
//***************************************************************************
//***************************************************************************
ETL_NODISCARD
static ETL_CONSTEXPR14 time_point min() ETL_NOEXCEPT
{
return time_point(duration::min());
}
//***************************************************************************
//***************************************************************************
ETL_NODISCARD
static ETL_CONSTEXPR14 time_point max() ETL_NOEXCEPT
{
return time_point(duration::max());
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const time_point& other) const ETL_NOEXCEPT
{
if (dur < other.dur)
return -1;
if (dur > other.dur)
return 1;
return 0;
}
private:
duration dur;
};
//***********************************************************************
//***********************************************************************
template <typename TToDuration, typename TClock, typename TDuration>
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point<TClock, TToDuration> floor(const etl::chrono::time_point<TClock, TDuration>& tp)
ETL_NOEXCEPT
{
return etl::chrono::time_point<TClock, TToDuration>(floor<TToDuration>(tp.time_since_epoch()));
}
//***********************************************************************
//***********************************************************************
template <typename TToDuration, typename TClock, typename TDuration>
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point<TClock, TToDuration> ceil(const etl::chrono::time_point<TClock, TDuration>& tp) ETL_NOEXCEPT
{
return etl::chrono::time_point<TClock, TToDuration>(ceil<TToDuration>(tp.time_since_epoch()));
}
//***********************************************************************
//***********************************************************************
template <typename TToDuration, typename TClock, typename TDuration>
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point<TClock, TToDuration> round(const etl::chrono::time_point<TClock, TDuration>& tp)
ETL_NOEXCEPT
{
return etl::chrono::time_point<TClock, TToDuration>(round<TToDuration>(tp.time_since_epoch()));
}
template <typename TToDuration, typename TClock, typename TDuration>
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point<TClock, TToDuration> time_point_cast(const etl::chrono::time_point<TClock, TDuration>& tp)
ETL_NOEXCEPT
{
TToDuration dur = etl::chrono::duration_cast<TToDuration>(tp.time_since_epoch());
return etl::chrono::time_point<TClock, TToDuration>(dur);
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TRep2, typename TPeriod2>
ETL_CONSTEXPR14 etl::chrono::time_point<TClock, typename etl::common_type<TDuration1, etl::chrono::duration<TRep2, TPeriod2> >::type>
operator+(const time_point<TClock, TDuration1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
{
using common_duration = typename etl::common_type<TDuration1, etl::chrono::duration<TRep2, TPeriod2> >::type;
using result_time_point = etl::chrono::time_point<TClock, common_duration>;
return result_time_point(lhs.time_since_epoch() + rhs);
}
//***************************************************************************
//***************************************************************************
template <typename TRep1, typename TPeriod1, typename TClock, typename TDuration2>
ETL_CONSTEXPR14 etl::chrono::time_point<TClock, typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, TDuration2>::type>
operator+(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, TDuration2>::type;
using result_time_point = etl::chrono::time_point<TClock, common_duration>;
return result_time_point(lhs + rhs.time_since_epoch());
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TRep2, typename TPeriod2>
ETL_CONSTEXPR14 etl::chrono::time_point<TClock, typename etl::common_type<TDuration1, etl::chrono::duration<TRep2, TPeriod2> >::type>
operator-(const time_point<TClock, TDuration1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
{
using common_duration = typename etl::common_type<TDuration1, etl::chrono::duration<TRep2, TPeriod2> >::type;
using result_time_point = etl::chrono::time_point<TClock, common_duration>;
return result_time_point(lhs.time_since_epoch() - rhs);
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
ETL_CONSTEXPR14 typename etl::common_type<TDuration1, TDuration2>::type operator-(const time_point<TClock, TDuration1>& lhs,
const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
return lhs.time_since_epoch() - rhs.time_since_epoch();
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
ETL_CONSTEXPR14 bool operator==(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
return lhs.time_since_epoch() == rhs.time_since_epoch();
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
ETL_CONSTEXPR14 bool operator!=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
return !(lhs == rhs);
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
ETL_CONSTEXPR14 bool operator<(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
return lhs.time_since_epoch() < rhs.time_since_epoch();
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
ETL_CONSTEXPR14 bool operator<=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
return !(rhs < lhs);
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
ETL_CONSTEXPR14 bool operator>(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
return rhs < lhs;
}
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
ETL_CONSTEXPR14 bool operator>=(const time_point<TClock, TDuration1>& lhs, const time_point<TClock, TDuration2>& rhs) ETL_NOEXCEPT
{
return !(lhs < rhs);
}
} // namespace chrono
//***********************************************************************
//***********************************************************************
#if ETL_USING_CPP20
template <typename TClock, typename TDuration1, typename TDuration2>
[[nodiscard]]
constexpr auto operator<=>(const etl::chrono::time_point<TClock, TDuration1>& lhs, const etl::chrono::time_point<TClock, TDuration2>& rhs)
ETL_NOEXCEPT
{
return (lhs.time_since_epoch() <=> rhs.time_since_epoch());
}
#endif
//***************************************************************************
//***************************************************************************
template <typename TClock, typename TDuration1, typename TDuration2>
struct common_type<etl::chrono::time_point<TClock, TDuration1>, etl::chrono::time_point<TClock, TDuration2>>
{
using type = etl::chrono::time_point< TClock, typename etl::common_type<TDuration1, TDuration2>::type>;
};
} // namespace etl
etl::chrono::duration
duration
Definition duration.h:108
etl::chrono::time_point
Definition time_point.h:45
etl::chrono::time_point::time_since_epoch
ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
Definition time_point.h:100
etl::chrono::time_point::time_point
ETL_CONSTEXPR14 time_point(const duration &dur_) ETL_NOEXCEPT
Construct from a duration.
Definition time_point.h:64
etl::chrono::time_point::operator=
ETL_CONSTEXPR14 time_point & operator=(const time_point &rhs) ETL_NOEXCEPT
Assignment operator.
Definition time_point.h:89
etl::chrono::time_point::time_point
ETL_CONSTEXPR time_point() ETL_NOEXCEPT
Default constructor.
Definition time_point.h:56
etl::chrono::time_point::operator++
ETL_CONSTEXPR14 time_point & operator++() ETL_NOEXCEPT
Pre-increments the stored duration by one tick.
Definition time_point.h:128
etl::chrono::time_point::operator--
ETL_CONSTEXPR14 time_point operator--(int) ETL_NOEXCEPT
Post-decrements the stored duration by one tick.
Definition time_point.h:159
etl::chrono::time_point::min
static ETL_NODISCARD ETL_CONSTEXPR14 time_point min() ETL_NOEXCEPT
Returns a time_point with the smallest possible duration.
Definition time_point.h:171
etl::chrono::time_point::compare
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const time_point &other) const ETL_NOEXCEPT
Definition time_point.h:191
etl::chrono::time_point::max
static ETL_NODISCARD ETL_CONSTEXPR14 time_point max() ETL_NOEXCEPT
Returns a time_point with the largest possible duration.
Definition time_point.h:180
etl::chrono::time_point::time_point
ETL_CONSTEXPR14 time_point(const time_point< clock, TDuration2 > &rhs) ETL_NOEXCEPT
Copy construct from another time_point with a different duration type.
Definition time_point.h:81
etl::chrono::time_point::time_point
ETL_CONSTEXPR14 time_point(const time_point &rhs) ETL_NOEXCEPT
Copy constructor.
Definition time_point.h:72
etl::chrono::time_point::operator--
ETL_CONSTEXPR14 time_point & operator--() ETL_NOEXCEPT
Pre-decrements the stored duration by one tick.
Definition time_point.h:149
etl::chrono::time_point::operator-=
ETL_CONSTEXPR14 time_point & operator-=(const duration &rhs) ETL_NOEXCEPT
Subtracts a duration.
Definition time_point.h:118
etl::chrono::time_point::operator+=
ETL_CONSTEXPR14 time_point & operator+=(const duration &rhs) ETL_NOEXCEPT
Adds a duration.
Definition time_point.h:108
etl::chrono::time_point::operator++
ETL_CONSTEXPR14 time_point operator++(int) ETL_NOEXCEPT
Post-increments the stored duration by one tick.
Definition time_point.h:138
etl::chrono::operator+
ETL_CONSTEXPR14 etl::chrono::day operator+(const etl::chrono::day &d, const etl::chrono::days &ds) ETL_NOEXCEPT
Spaceship operator.
Definition day.h:226
etl::chrono::operator-
ETL_CONSTEXPR14 etl::chrono::day operator-(const etl::chrono::day &d, const etl::chrono::days &ds) ETL_NOEXCEPT
Definition day.h:252
etl::chrono::operator<
ETL_CONSTEXPR14 bool operator<(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Less-than operator.
Definition day.h:182
etl::chrono::operator>=
ETL_CONSTEXPR14 bool operator>=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Greater-than-or-equal operator.
Definition day.h:206
etl::chrono::operator==
ETL_CONSTEXPR14 bool operator==(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Equality operator.
Definition day.h:166
etl::chrono::operator<=
ETL_CONSTEXPR14 bool operator<=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Less-than-or-equal operator.
Definition day.h:190
etl::chrono::operator!=
ETL_CONSTEXPR14 bool operator!=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Inequality operator.
Definition day.h:174
etl::chrono::operator>
ETL_CONSTEXPR14 bool operator>(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Greater-than operator.
Definition day.h:198
etl::chrono::duration_cast
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:343
type
etl
Definition absolute.h:40
etl::type
etl::chrono::round
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point< TClock, TToDuration > round(const etl::chrono::time_point< TClock, TDuration > &tp) ETL_NOEXCEPT
Definition time_point.h:230
etl::chrono::floor
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point< TClock, TToDuration > floor(const etl::chrono::time_point< TClock, TDuration > &tp) ETL_NOEXCEPT
Rounds down a duration to the nearest lower precision.
Definition time_point.h:210
etl::chrono::ceil
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::time_point< TClock, TToDuration > ceil(const etl::chrono::time_point< TClock, TDuration > &tp) ETL_NOEXCEPT
Rounds up a duration to the nearest higher precision.
Definition time_point.h:220