Loading...
Searching...
No Matches
hh_mm_ss.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
#include "../../absolute.h"
#include "../../power.h"
namespace etl
{
//***********************************************************************
//***********************************************************************
template <typename TDuration>
ETL_NODISCARD ETL_CONSTEXPR14 typename etl::enable_if< etl::is_specialization<TDuration, etl::chrono::duration>::value, TDuration>::type absolute(TDuration dur)
ETL_NOEXCEPT
{
return TDuration((dur.count() < 0) ? -dur.count() : dur.count());
}
namespace chrono
{
//***********************************************************************
//***********************************************************************
template <typename TDuration>
class hh_mm_ss
{
private:
// Helper template to calculate the number of digits in a denominator at
// compile time
template <uintmax_t Den, int Width = 0>
struct fractional_width_helper
{
static constexpr int value = fractional_width_helper<Den / 10, Width + 1>::value;
};
// Base case: when Den == 1, stop recursion
template <int Width>
struct fractional_width_helper<1, Width>
{
static constexpr int value = Width;
};
// Special case: when Den == 0 (invalid denominator), return 0
template <int Width>
struct fractional_width_helper<0, Width>
{
static constexpr int value = 0;
};
// Calculate fractional width for TDuration
template <typename TDur>
struct calculate_fractional_width
{
static constexpr int value = (TDur::period::den == 1) ? 0 : fractional_width_helper<TDur::period::den>::value;
};
public:
ETL_STATIC_ASSERT((etl::is_specialization<TDuration, etl::chrono::duration>::value), "TDuration is not a etl::chrono::duration type");
static constexpr int fractional_width = calculate_fractional_width<TDuration>::value;
//***********************************************************************
//***********************************************************************
using precision = etl::chrono::duration< common_type_t<typename TDuration::rep, etl::chrono::seconds::rep>,
ratio<1, etl::power<10, size_t(fractional_width)>::value>>;
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR hh_mm_ss() ETL_NOEXCEPT
: d(TDuration::zero())
{
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 explicit hh_mm_ss(TDuration d_) ETL_NOEXCEPT
: d(d_)
{
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 bool is_negative() const ETL_NOEXCEPT
{
return d < TDuration::zero();
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::hours hours() const ETL_NOEXCEPT
{
auto dur = etl::absolute(d);
return etl::chrono::duration_cast<etl::chrono::hours>(dur);
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::minutes minutes() const ETL_NOEXCEPT
{
auto dur = etl::absolute(d) - hours();
return etl::chrono::duration_cast<etl::chrono::minutes>(dur);
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::seconds seconds() const ETL_NOEXCEPT
{
auto dur = etl::absolute(d) - hours() - minutes();
return etl::chrono::duration_cast<etl::chrono::seconds>(dur);
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 precision subseconds() const ETL_NOEXCEPT
{
return etl::absolute(d) - etl::chrono::duration_cast<etl::chrono::seconds>(etl::absolute(d));
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 explicit operator precision() const ETL_NOEXCEPT
{
return to_duration();
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 precision to_duration() const ETL_NOEXCEPT
{
return d;
}
private:
TDuration d;
};
template <typename TDuration>
constexpr int etl::chrono::hh_mm_ss<TDuration>::fractional_width;
//***********************************************************************
//***********************************************************************
//***********************************************************************
//***********************************************************************
ETL_NODISCARD
inline ETL_CONSTEXPR14 bool is_am(const etl::chrono::hours& h) ETL_NOEXCEPT
{
return (h >= etl::chrono::hours(0)) && (h <= etl::chrono::hours(11));
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD
inline ETL_CONSTEXPR14 bool is_pm(const etl::chrono::hours& h) ETL_NOEXCEPT
{
return (h >= etl::chrono::hours(12)) && (h <= etl::chrono::hours(23));
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD
inline ETL_CONSTEXPR14 etl::chrono::hours make12(const etl::chrono::hours& h) ETL_NOEXCEPT
{
if (h == etl::chrono::hours(0))
{
return etl::chrono::hours(12);
}
else if (h > etl::chrono::hours(12))
{
return h - etl::chrono::hours(12);
}
return h;
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD
inline ETL_CONSTEXPR14 etl::chrono::hours make24(const etl::chrono::hours& h, bool is_pm) ETL_NOEXCEPT
{
if (is_pm)
{
return (h == etl::chrono::hours(12)) ? h : (h + etl::chrono::hours(12));
}
else
{
return (h == etl::chrono::hours(12)) ? etl::chrono::hours(0) : h;
}
}
} // namespace chrono
} // namespace etl
absolute.h
etl::chrono::duration
duration
Definition duration.h:108
etl::chrono::hh_mm_ss::is_negative
ETL_NODISCARD ETL_CONSTEXPR14 bool is_negative() const ETL_NOEXCEPT
Checks for negative duration.
Definition hh_mm_ss.h:121
etl::chrono::hh_mm_ss::subseconds
ETL_NODISCARD ETL_CONSTEXPR14 precision subseconds() const ETL_NOEXCEPT
Returns the subseconds.
Definition hh_mm_ss.h:159
etl::chrono::hh_mm_ss::precision
etl::chrono::duration< common_type_t< typename TDuration::rep, etl::chrono::seconds::rep >, ratio< 1, etl::power< 10, size_t(fractional_width)>::value > > precision
The return type for to_duration.
Definition hh_mm_ss.h:99
etl::chrono::hh_mm_ss::hh_mm_ss
ETL_CONSTEXPR hh_mm_ss() ETL_NOEXCEPT
Default constructor.
Definition hh_mm_ss.h:105
etl::chrono::hh_mm_ss::to_duration
ETL_NODISCARD ETL_CONSTEXPR14 precision to_duration() const ETL_NOEXCEPT
Returns the duration.
Definition hh_mm_ss.h:175
etl::chrono::hh_mm_ss::hh_mm_ss
ETL_CONSTEXPR14 hh_mm_ss(TDuration d_) ETL_NOEXCEPT
Construct from duration.
Definition hh_mm_ss.h:113
etl::chrono::hh_mm_ss::hours
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::hours hours() const ETL_NOEXCEPT
Returns the hours.
Definition hh_mm_ss.h:129
etl::chrono::hh_mm_ss::minutes
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::minutes minutes() const ETL_NOEXCEPT
Returns the minutes.
Definition hh_mm_ss.h:139
etl::chrono::hh_mm_ss::seconds
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::seconds seconds() const ETL_NOEXCEPT
Returns the seconds.
Definition hh_mm_ss.h:149
etl::chrono::duration_cast
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:343
etl::power
Definition power.h:62
etl::chrono::make24
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::hours make24(const etl::chrono::hours &h, bool is_pm) ETL_NOEXCEPT
Definition hh_mm_ss.h:235
etl::chrono::is_pm
ETL_NODISCARD ETL_CONSTEXPR14 bool is_pm(const etl::chrono::hours &h) ETL_NOEXCEPT
Returns true if h is in the range [12h, 23h] (a pm hour).
Definition hh_mm_ss.h:205
etl::chrono::is_am
ETL_NODISCARD ETL_CONSTEXPR14 bool is_am(const etl::chrono::hours &h) ETL_NOEXCEPT
12/24 hours functions
Definition hh_mm_ss.h:196
etl::chrono::make12
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::hours make12(const etl::chrono::hours &h) ETL_NOEXCEPT
Definition hh_mm_ss.h:215
etl
Definition absolute.h:40
power.h
etl::type
etl::ratio
ratio
Definition ratio.h:52