Merge branch 'feature/#757-Add-time-date-classes' of https://github.com/ETLCPP/etl-21 into dual-licence/feature/#757-Add-time-date-classes

This commit is contained in:
John Wellbelove 2024-09-07 19:34:51 +01:00
commit c0d3447818
18 changed files with 3496 additions and 2 deletions

52
include/etl/chrono.h Normal file
View File

@ -0,0 +1,52 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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_CHRONO_INCLUDED
#define ETL_CHRONO_INCLUDED
#define ETL_IN_CHRONO_H
#include "platform.h"
#include "hash.h"
#include <stdint.h>
#include "private/chrono/last_spec.h"
#include "private/chrono/duration.h"
#include "private/chrono/day.h"
#include "private/chrono/weekday.h"
#include "private/chrono/weekday_indexed.h"
#include "private/chrono/weekday_last.h"
#include "private/chrono/month.h"
#include "private/chrono/year.h"
#undef ETL_IN_CHRONO_H
#endif

View File

@ -159,8 +159,8 @@ namespace etl
/// Specialisation for signed char.
///\ingroup hash
//***************************************************************************
template<> struct
hash<signed char>
template <>
struct hash<signed char>
{
ETL_STATIC_ASSERT(sizeof(size_t) >= sizeof(signed char), "size_t smaller than type");

View File

@ -271,6 +271,38 @@ SOFTWARE.
#define ETL_HAS_VIRTUAL_MESSAGES 1
#endif
//*************************************
// Indicate if etl::literals::chrono_literals has weekdays (_day)
#if defined(ETL_DISABLE_CHRONO_LITERALS_WEEKDAY)
#define ETL_HAS_CHRONO_LITERALS_DAY 0
#else
#define ETL_HAS_CHRONO_LITERALS_DAY 1
#endif
//*************************************
// Indicate if etl::literals::chrono_literals has weekdays (_weekday)
#if defined(ETL_DISABLE_CHRONO_LITERALS_WEEKDAY)
#define ETL_HAS_CHRONO_LITERALS_WEEKDAY 0
#else
#define ETL_HAS_CHRONO_LITERALS_WEEKDAY 1
#endif
//*************************************
// Indicate if etl::literals::chrono_literals has month (_month)
#if defined(ETL_DISABLE_CHRONO_LITERALS_MONTH)
#define ETL_HAS_CHRONO_LITERALS_MONTH 0
#else
#define ETL_HAS_CHRONO_LITERALS_MONTH 1
#endif
//*************************************
// Indicate if etl::literals::chrono_literals has year (_year)
#if defined(ETL_DISABLE_CHRONO_LITERALS_YEAR)
#define ETL_HAS_CHRONO_LITERALS_YEAR 0
#else
#define ETL_HAS_CHRONO_LITERALS_YEAR 1
#endif
//*************************************
// The macros below are dependent on the profile.
// C++11
@ -512,6 +544,10 @@ namespace etl
static ETL_CONSTANT bool has_mutable_array_view = (ETL_HAS_MUTABLE_ARRAY_VIEW == 1);
static ETL_CONSTANT bool has_ideque_repair = (ETL_HAS_IDEQUE_REPAIR == 1);
static ETL_CONSTANT bool has_virtual_messages = (ETL_HAS_VIRTUAL_MESSAGES == 1);
static ETL_CONSTANT bool has_chrono_literals_day = (ETL_HAS_CHRONO_LITERALS_DAY == 1);
static ETL_CONSTANT bool has_chrono_literals_weekday = (ETL_HAS_CHRONO_LITERALS_WEEKDAY == 1);
static ETL_CONSTANT bool has_chrono_literals_month = (ETL_HAS_CHRONO_LITERALS_MONTH == 1);
static ETL_CONSTANT bool has_chrono_literals_year = (ETL_HAS_CHRONO_LITERALS_YEAR == 1);
// Is...
static ETL_CONSTANT bool is_debug_build = (ETL_IS_DEBUG_BUILD == 1);

View File

@ -0,0 +1,332 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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
{
//***********************************************************************
/// day
//***********************************************************************
class day
{
public:
//***********************************************************************
/// Default constructor
//***********************************************************************
ETL_CONSTEXPR day() ETL_NOEXCEPT
: value(0)
{
}
//***********************************************************************
/// Construct from unsigned
//***********************************************************************
ETL_CONSTEXPR explicit day(unsigned value_) ETL_NOEXCEPT
: value(static_cast<unsigned char>(value_))
{
}
//***********************************************************************
/// Copy constructor
//***********************************************************************
ETL_CONSTEXPR day(const etl::chrono::day& other) ETL_NOEXCEPT
: value(other.value)
{
}
//***********************************************************************
/// Assignment operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day& operator =(const etl::chrono::day& rhs) ETL_NOEXCEPT
{
value = rhs.value;
return *this;
}
//***********************************************************************
/// Pre-increment operator
template <typename TToDuration, typename TValue2, typename TPeriod2>
ETL_CONSTEXPR etl::chrono::day& operator =(const etl::chrono::duration<TValue2, TPeriod2>& rhs)
{
value = etl::chrono::duration_cast<TToDuration, TValue2, TPeriod2>(rhs);
return *this;
}
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day& operator ++() ETL_NOEXCEPT
{
++value;
return *this;
}
//***********************************************************************
/// Post-increment operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::day operator ++(int) ETL_NOEXCEPT
{
const etl::chrono::day temp = *this;
++value;
return temp;
}
//***********************************************************************
/// Pre-decrement operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day& operator --() ETL_NOEXCEPT
{
--value;
return *this;
}
//***********************************************************************
/// Post-decrement operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::day operator --(int) ETL_NOEXCEPT
{
const etl::chrono::day temp = *this;
--value;
return temp;
}
//***********************************************************************
/// Plus-equals operator adding etl::chrono::days
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day& operator +=(const etl::chrono::days& ds) ETL_NOEXCEPT
{
value += static_cast<unsigned char>(ds.count());
return *this;
}
//***********************************************************************
/// Minus-equals operator subtracting etl::chrono::days
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day& operator -=(const etl::chrono::days& ds) ETL_NOEXCEPT
{
value -= static_cast<unsigned char>(ds.count());
return *this;
}
//***********************************************************************
/// Returns <b>true</b> if the day is within the valid 1 to 31 range
//***********************************************************************
ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
{
return (value >= 1U) && (value <= 31U);
}
//***********************************************************************
/// Conversion operator to unsigned int
//***********************************************************************
ETL_CONSTEXPR operator unsigned() const ETL_NOEXCEPT
{
return static_cast<unsigned>(value);
}
//***********************************************************************
/// The minimum day value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::day min() ETL_NOEXCEPT
{
return etl::chrono::day(1);
}
//***********************************************************************
/// The maximum day value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::day max() ETL_NOEXCEPT
{
return etl::chrono::day(31);
}
private:
unsigned char value;
};
//***********************************************************************
/// Equality operator
//***********************************************************************
ETL_CONSTEXPR bool operator ==(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) == static_cast<unsigned>(d2));
}
//***********************************************************************
/// Inequality operator
//***********************************************************************
ETL_CONSTEXPR bool operator !=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return !(d1 == d2);
}
//***********************************************************************
/// Less-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator <(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) < static_cast<unsigned>(d2));
}
//***********************************************************************
/// Less-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator <=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) <= static_cast<unsigned>(d2));
}
//***********************************************************************
/// Greater-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator >(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) > static_cast<unsigned>(d2));
}
//***********************************************************************
/// Greater-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator >=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) >= static_cast<unsigned>(d2));
}
//***********************************************************************
/// Spaceship operator
//***********************************************************************
#if ETL_USING_CPP20
[[nodiscard]] constexpr auto operator <=>(const etl::chrono::day& d1, const etl::chrono::day& d2) noexcept
{
return (static_cast<unsigned>(d1) <=> static_cast<unsigned>(d2));
}
#endif
//***********************************************************************
/// Add etl::chrono::days to etl::chrono::day
///\return etl::chrono::day
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
{
etl::chrono::day result(d);
result += ds;
return result;
}
//***********************************************************************
/// Add etl::chrono::day to etl::chrono::days
///\return etl::chrono::day
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::days& ds, const etl::chrono::day& d) ETL_NOEXCEPT
{
etl::chrono::day result(d);
result += ds;
return result;
}
//***********************************************************************
/// Subtract etl::chrono::days from etl::chrono::day
///\return etl::chrono::day
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day operator -(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
{
etl::chrono::day result(d);
result -= ds;
return result;
}
//***********************************************************************
/// Subtract etl::chrono::day from etl::chrono::day
///\return etl::chrono::days
//***********************************************************************
ETL_CONSTEXPR etl::chrono::days operator -(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return etl::chrono::days(static_cast<int>(static_cast<unsigned>(d1)) -
static_cast<int>(static_cast<unsigned>(d2)));
}
}
//*************************************************************************
/// Hash function for etl::chrono::day
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::chrono::day>
{
size_t operator()(const etl::chrono::day& d) const
{
unsigned value = d;
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
return etl::private_hash::generic_hash<size_t>(p, p + sizeof(unsigned));
}
};
#endif
}
#if ETL_HAS_CHRONO_LITERALS_DAY
#if ETL_USING_CPP11
namespace etl
{
namespace literals
{
namespace chrono_literals
{
//***********************************************************************
/// Literal for days
//***********************************************************************
constexpr etl::chrono::day operator ""_day(unsigned long long d) noexcept
{
return etl::chrono::day(static_cast<unsigned>(d));
}
}
}
}
#endif
#endif

View File

@ -0,0 +1,146 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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 "../../ratio.h"
#include "../../static_assert.h"
#include "../../limits.h"
namespace etl
{
namespace chrono
{
//***********************************************************************
/// duration
//***********************************************************************
template <typename TValue, typename TPeriod = etl::ratio<1> >
class duration
{
public:
//***********************************************************************
ETL_CONSTEXPR duration() ETL_NOEXCEPT
: value(0)
{
}
//***********************************************************************
ETL_CONSTEXPR duration(const etl::chrono::duration<TValue, TPeriod>& other) ETL_NOEXCEPT
: value(other.value)
{
}
//***********************************************************************
template< typename TValue2 >
ETL_CONSTEXPR explicit duration(const TValue2& value_) ETL_NOEXCEPT
: value(static_cast<TValue>(value_))
{
}
//***********************************************************************
template< typename TValue2, typename TPeriod2 >
ETL_CONSTEXPR duration(const etl::chrono::duration<TValue2, TPeriod2>& other) ETL_NOEXCEPT
: value(static_cast<TValue>(other.value))
{
ETL_STATIC_ASSERT(!(etl::is_integral<TValue>::value && etl::is_floating_point<TValue2>::value), "Cannot convert duration from floating point to integral");
}
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::duration< TValue, TPeriod> zero() ETL_NOEXCEPT
{
return etl::chrono::duration{ 0, TPeriod()};
}
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::duration<TValue, TPeriod> min() ETL_NOEXCEPT
{
return etl::chrono::duration<TValue, TPeriod> { etl::numeric_limits<TValue>::min() };
}
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::duration<TValue, TPeriod> max() ETL_NOEXCEPT
{
return etl::chrono::duration<TValue, TPeriod>{ etl::numeric_limits<TValue>::max() };
}
//***********************************************************************
ETL_CONSTEXPR TValue count() const ETL_NOEXCEPT
{
return value;
}
private:
TValue value;
};
//***********************************************************************
/// Duration types
//***********************************************************************
#if (ETL_USING_64BIT_TYPES)
typedef etl::chrono::duration<int64_t, etl::nano> nanoseconds;
typedef etl::chrono::duration<int64_t, etl::micro> microseconds;
typedef etl::chrono::duration<int64_t, etl::milli> milliseconds;
typedef etl::chrono::duration<int64_t, etl::ratio<1U> > seconds;
#else
typedef etl::chrono::duration<int32_t, etl::nano> nanoseconds;
typedef etl::chrono::duration<int32_t, etl::micro> microseconds;
typedef etl::chrono::duration<int32_t, etl::milli> milliseconds;
typedef etl::chrono::duration<int32_t, etl::ratio<1U> > seconds;
#endif
typedef etl::chrono::duration<int32_t, etl::ratio<60U> > minutes;
typedef etl::chrono::duration<int32_t, etl::ratio<3600U> > hours;
typedef etl::chrono::duration<int32_t, etl::ratio<86400U> > days;
typedef etl::chrono::duration<int32_t, etl::ratio<604800U> > weeks;
typedef etl::chrono::duration<int32_t, etl::ratio<2629746U> > months;
typedef etl::chrono::duration<int32_t, etl::ratio<31556952U> > years;
//***********************************************************************
/// duration_cast
//***********************************************************************
template <typename TToDuration, typename TValue, typename TPeriod>
ETL_CONSTEXPR TToDuration duration_cast(const etl::chrono::duration<TValue, TPeriod>& d) ETL_NOEXCEPT
{
using to_value_type = typename TToDuration::value_type;
using to_period = typename TToDuration::period;
// Calculate the conversion factor between the periods
ETL_CONSTEXPR auto conversion_factor = (static_cast<double>(TPeriod::num) / TPeriod::den) * (to_period::den / to_period::num);
// Convert the value
to_value_type converted_value = static_cast<to_value_type>(d.count() * conversion_factor);
return TToDuration(converted_value);
}
}
}

View File

@ -0,0 +1,52 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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
{
struct last_spec
{
ETL_CONSTEXPR explicit last_spec()
{
}
};
#if ETL_USING_CPP17
inline constexpr last_spec last{};
#else
static ETL_CONSTANT last_spec last{};
#endif
}
}

View File

@ -0,0 +1,383 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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
{
class month;
ETL_CONSTEXPR etl::chrono::month operator +(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT;
ETL_CONSTEXPR etl::chrono::month operator +(const etl::chrono::months& ms, const etl::chrono::month& m) ETL_NOEXCEPT;
ETL_CONSTEXPR etl::chrono::month operator -(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT;
//***********************************************************************
/// month
//***********************************************************************
class month
{
public:
//***********************************************************************
/// Default constructor
//***********************************************************************
ETL_CONSTEXPR month() ETL_NOEXCEPT
: value(0)
{
}
//***********************************************************************
/// Construct from unsigned
//***********************************************************************
ETL_CONSTEXPR explicit month(unsigned value_) ETL_NOEXCEPT
: value(value_)
{
}
//***********************************************************************
/// Copy constructor
//***********************************************************************
ETL_CONSTEXPR month(const etl::chrono::month& other) ETL_NOEXCEPT
: value(other.value)
{
}
//***********************************************************************
/// Assignment operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator =(const etl::chrono::month& rhs) ETL_NOEXCEPT
{
value = rhs.value;
return *this;
}
//***********************************************************************
/// Pre-increment operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator ++() ETL_NOEXCEPT
{
*this += etl::chrono::months(1);
return *this;
}
//***********************************************************************
/// Post-increment operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::month operator ++(int) ETL_NOEXCEPT
{
const etl::chrono::month temp = *this;
*this += etl::chrono::months(1);
return temp;
}
//***********************************************************************
/// Pre-decrement operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator --() ETL_NOEXCEPT
{
*this -= etl::chrono::months(1);
return *this;
}
//***********************************************************************
/// Post-decrement operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::month operator --(int) ETL_NOEXCEPT
{
etl::chrono::month temp = *this;
*this -= etl::chrono::months(1);
return temp;
}
//***********************************************************************
/// Plus-equals operator adding etl::chrono::months
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator +=(const etl::chrono::months& ms) ETL_NOEXCEPT
{
*this = *this + ms;
return *this;
}
//***********************************************************************
/// Minus-equals operator subtracting etl::chrono::months
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator -=(const etl::chrono::months& ms) ETL_NOEXCEPT
{
*this = *this - ms;
return *this;
}
//***********************************************************************
/// Returns <b>true</b> if the month is within the valid 1 to 31 range
//***********************************************************************
ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
{
return (value >= 1U) && (value <= 12U);
}
//***********************************************************************
/// The minimum month value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::month min() ETL_NOEXCEPT
{
return etl::chrono::month(1);
}
//***********************************************************************
/// The maximum month value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::month max() ETL_NOEXCEPT
{
return etl::chrono::month(12);
}
//***********************************************************************
/// Conversion operator to unsigned int
//***********************************************************************
ETL_CONSTEXPR operator unsigned() const ETL_NOEXCEPT
{
return static_cast<unsigned>(value);
}
private:
//***********************************************************************
/// Normalise to a in-range month
//***********************************************************************
ETL_CONSTEXPR void normalise() ETL_NOEXCEPT
{
value = ((value % 12U) == 0U) ? 12U : value;
}
unsigned char value;
};
//***********************************************************************
/// Equality operator
//***********************************************************************
ETL_CONSTEXPR bool operator ==(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) == static_cast<unsigned>(d2));
}
//***********************************************************************
/// Inequality operator
//***********************************************************************
ETL_CONSTEXPR bool operator !=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
{
return !(d1 == d2);
}
//***********************************************************************
/// Less-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator <(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) < static_cast<unsigned>(d2));
}
//***********************************************************************
/// Less-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator <=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) <= static_cast<unsigned>(d2));
}
//***********************************************************************
/// Greater-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator >(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) > static_cast<unsigned>(d2));
}
//***********************************************************************
/// Greater-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator >=(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(d1) >= static_cast<unsigned>(d2));
}
//***********************************************************************
/// Spaceship operator
//***********************************************************************
#if ETL_USING_CPP20
[[nodiscard]] constexpr auto operator <=>(const etl::chrono::month& d1, const etl::chrono::month& d2) noexcept
{
return (static_cast<unsigned>(d1) <=> static_cast<unsigned>(d2));
}
#endif
//***********************************************************************
/// Add etl::chrono::months to etl::chrono::month
///\return etl::chrono::month
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::month operator +(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT
{
unsigned int value = static_cast<unsigned int>(m);
value = value % 12U;
if (value == 0U)
{
value = 12U;
}
int delta = ms.count() % 12;
// Adjust to allow a limited +-11 month delta
value += 11U;
value += delta;
value %= 12U;
++value;
return etl::chrono::month(value);
}
//***********************************************************************
/// Add etl::chrono::month to etl::chrono::months
///\return etl::chrono::month
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month operator +(const etl::chrono::months& ms, const etl::chrono::month& m) ETL_NOEXCEPT
{
return m + ms;
}
//***********************************************************************
/// Subtract etl::chrono::months from etl::chrono::month
///\return etl::chrono::month
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month operator -(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT
{
return m + etl::chrono::months(-ms.count());
}
//***********************************************************************
/// Subtract etl::chrono::month from etl::chrono::month
///\return etl::chrono::months
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::months operator -(const etl::chrono::month& m1, const etl::chrono::month& m2) ETL_NOEXCEPT
{
if (m1.ok() && m2.ok())
{
etl::chrono::months ms(static_cast<signed>(static_cast<unsigned>(m1)) -
static_cast<signed>(static_cast<unsigned>(m2)) % 12);
if (m1 == (m2 + ms))
{
return ms;
}
}
return etl::chrono::months();
}
#if ETL_USING_CPP17
inline constexpr etl::chrono::month January{ 1 };
inline constexpr etl::chrono::month February{ 2 };
inline constexpr etl::chrono::month March{ 3 };
inline constexpr etl::chrono::month April{ 4 };
inline constexpr etl::chrono::month May{ 5 };
inline constexpr etl::chrono::month June{ 6 };
inline constexpr etl::chrono::month July{ 7 };
inline constexpr etl::chrono::month August{ 8 };
inline constexpr etl::chrono::month September{ 9 };
inline constexpr etl::chrono::month October{ 10 };
inline constexpr etl::chrono::month November{ 11 };
inline constexpr etl::chrono::month December{ 12 };
#else
static ETL_CONSTANT etl::chrono::month January{ 1 };
static ETL_CONSTANT etl::chrono::month February{ 2 };
static ETL_CONSTANT etl::chrono::month March{ 3 };
static ETL_CONSTANT etl::chrono::month April{ 4 };
static ETL_CONSTANT etl::chrono::month May{ 5 };
static ETL_CONSTANT etl::chrono::month June{ 6 };
static ETL_CONSTANT etl::chrono::month July{ 7 };
static ETL_CONSTANT etl::chrono::month August{ 8 };
static ETL_CONSTANT etl::chrono::month September{ 9 };
static ETL_CONSTANT etl::chrono::month October{ 10 };
static ETL_CONSTANT etl::chrono::month November{ 11 };
static ETL_CONSTANT etl::chrono::month December{ 12 };
#endif
}
//*************************************************************************
/// Hash function for etl::chrono::month
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::chrono::month>
{
size_t operator()(const etl::chrono::month& m) const
{
unsigned value = m;
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
return etl::private_hash::generic_hash<size_t>(p, p + sizeof(unsigned));
}
};
#endif
}
#if ETL_HAS_CHRONO_LITERALS_MONTH
#if ETL_USING_CPP11
namespace etl
{
namespace literals
{
namespace chrono_literals
{
//***********************************************************************
/// Literal for months
//***********************************************************************
constexpr etl::chrono::month operator ""_month(unsigned long long m) noexcept
{
return etl::chrono::month(static_cast<unsigned char>(m));
}
}
}
}
#endif
#endif

View File

@ -0,0 +1,391 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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
{
class weekday;
class weekday_indexed;
class weekday_last;
struct last_spec;
ETL_CONSTEXPR etl::chrono::weekday operator +(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT;
ETL_CONSTEXPR etl::chrono::weekday operator +(const etl::chrono::days& ds, const etl::chrono::weekday& m) ETL_NOEXCEPT;
ETL_CONSTEXPR etl::chrono::weekday operator -(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT;
//***********************************************************************
/// weekday
//***********************************************************************
class weekday
{
public:
//***********************************************************************
/// Default constructor
//***********************************************************************
ETL_CONSTEXPR weekday() ETL_NOEXCEPT
: value(255U)
{
}
//***********************************************************************
/// Construct from unsigned
//***********************************************************************
ETL_CONSTEXPR explicit weekday(unsigned value_) ETL_NOEXCEPT
: value(value_ == 7U ? 0U :value_)
{
}
//***********************************************************************
/// Copy constructor
//***********************************************************************
ETL_CONSTEXPR weekday(const etl::chrono::weekday& other) ETL_NOEXCEPT
: value(other.value)
{
}
//***********************************************************************
/// Assignment operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday& operator =(const etl::chrono::weekday& rhs) ETL_NOEXCEPT
{
value = rhs.value;
return *this;
}
//***********************************************************************
/// Pre-increment operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday& operator ++() ETL_NOEXCEPT
{
*this += etl::chrono::days(1);
return *this;
}
//***********************************************************************
/// Post-increment operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::weekday operator ++(int) ETL_NOEXCEPT
{
const etl::chrono::weekday temp = *this;
*this += etl::chrono::days(1);
return temp;
}
//***********************************************************************
/// Pre-decrement operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday& operator --() ETL_NOEXCEPT
{
*this -= etl::chrono::days(1);
return *this;
}
//***********************************************************************
/// Post-decrement operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::weekday operator --(int) ETL_NOEXCEPT
{
etl::chrono::weekday temp = *this;
*this -= etl::chrono::days(1);
return temp;
}
//***********************************************************************
/// Plus-equals operator adding etl::chrono::days
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday& operator +=(const etl::chrono::days& ds) ETL_NOEXCEPT
{
*this = *this + ds;
return *this;
}
//***********************************************************************
/// Minus-equals operator subtracting etl::chrono::days
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday& operator -=(const etl::chrono::days& ds) ETL_NOEXCEPT
{
*this = *this - ds;
return *this;
}
//***********************************************************************
/// Returns <b>true</b> if the weekday is within the valid 1 to 31 range
//***********************************************************************
ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
{
return (c_encoding() <= 6U);
}
//***********************************************************************
/// The minimum weekday value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::weekday min() ETL_NOEXCEPT
{
return etl::chrono::weekday(0);
}
//***********************************************************************
/// The maximum weekday value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::weekday max() ETL_NOEXCEPT
{
return etl::chrono::weekday(6);
}
//***********************************************************************
/// Get the C encoding of the weekday
//***********************************************************************
ETL_CONSTEXPR unsigned c_encoding() const ETL_NOEXCEPT
{
return value;
}
//***********************************************************************
/// Get the ISO encoding of the weekday
//***********************************************************************
ETL_CONSTEXPR unsigned iso_encoding() const ETL_NOEXCEPT
{
return (value == 0U) ? 7U : value;
}
//***********************************************************************
/// Index operator from index
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday_indexed operator[](unsigned index) const ETL_NOEXCEPT;
//***********************************************************************
/// Index operator from etl::chrono::last_spec
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday_last operator[](etl::chrono::last_spec) const ETL_NOEXCEPT;
//***********************************************************************
/// Returns <b>true</b> if the day is a weekend.
//***********************************************************************
ETL_CONSTEXPR14 bool is_weekend() ETL_NOEXCEPT
{
return (c_encoding() == 0U) || (c_encoding() == 6U);
}
private:
//***********************************************************************
/// Normalise to a in-range weekday
//***********************************************************************
ETL_CONSTEXPR void normalise() ETL_NOEXCEPT
{
value %= 7U;
}
unsigned char value;
};
//***********************************************************************
/// Equality operator
//***********************************************************************
ETL_CONSTEXPR bool operator ==(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
{
return (wd1.c_encoding() == wd2.c_encoding());
}
//***********************************************************************
/// Inequality operator
//***********************************************************************
ETL_CONSTEXPR bool operator !=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
{
return !(wd1 == wd2);
}
//***********************************************************************
/// Less-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator <(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
{
return (wd1.c_encoding() < wd2.c_encoding());
}
//***********************************************************************
/// Less-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator <=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
{
return (wd1.c_encoding() <= wd2.c_encoding());
}
//***********************************************************************
/// Greater-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator >(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
{
return (wd1.c_encoding() > wd2.c_encoding());
}
//***********************************************************************
/// Greater-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator >=(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
{
return (wd1.c_encoding() >= wd2.c_encoding());
}
//***********************************************************************
/// Spaceship operator
//***********************************************************************
#if ETL_USING_CPP20
[[nodiscard]] constexpr auto operator <=>(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) noexcept
{
return (wd1.c_encoding() <=> wd2.c_encoding());
}
#endif
//***********************************************************************
/// Add etl::chrono::days to etl::chrono::weekday
///\return etl::chrono::weekday
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::weekday operator +(const etl::chrono::weekday& wd, const etl::chrono::days& ds) ETL_NOEXCEPT
{
int delta = ds.count() % 7;
unsigned int value = wd.c_encoding();
// Adjust to allow a limited +-7 weekday delta
value %= 7U;
value += 7U;
value += delta;
value %= 7U;
return etl::chrono::weekday(value);
}
//***********************************************************************
/// Add etl::chrono::weekday to etl::chrono::days
///\return etl::chrono::weekday
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday operator +(const etl::chrono::days& ds, const etl::chrono::weekday& wd) ETL_NOEXCEPT
{
return wd + ds;
}
//***********************************************************************
/// Subtract etl::chrono::days from etl::chrono::weekday
///\return etl::chrono::weekday
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday operator -(const etl::chrono::weekday& m, const etl::chrono::days& ds) ETL_NOEXCEPT
{
return m + etl::chrono::days(-ds.count());
}
//***********************************************************************
/// Subtract etl::chrono::weekday from etl::chrono::weekday
///\return etl::chrono::days
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::days operator -(const etl::chrono::weekday& wd1, const etl::chrono::weekday& wd2) ETL_NOEXCEPT
{
if (wd1.ok() && wd2.ok())
{
int diff = static_cast<int>(wd1.c_encoding()) - static_cast<int>(wd2.c_encoding());
return etl::chrono::days((diff + 7) % 7);
}
return etl::chrono::days(0);
}
#if ETL_USING_CPP17
inline constexpr etl::chrono::weekday Sunday{ 0U };
inline constexpr etl::chrono::weekday Monday{ 1U };
inline constexpr etl::chrono::weekday Tuesday{ 2U };
inline constexpr etl::chrono::weekday Wednesday{ 3U };
inline constexpr etl::chrono::weekday Thursday{ 4U };
inline constexpr etl::chrono::weekday Friday{ 5U };
inline constexpr etl::chrono::weekday Saturday{ 6U };
#else
static ETL_CONSTANT etl::chrono::weekday Sunday{ 0U };
static ETL_CONSTANT etl::chrono::weekday Monday{ 1U };
static ETL_CONSTANT etl::chrono::weekday Tuesday{ 2U };
static ETL_CONSTANT etl::chrono::weekday Wednesday{ 3U };
static ETL_CONSTANT etl::chrono::weekday Thursday{ 4U };
static ETL_CONSTANT etl::chrono::weekday Friday{ 5U };
static ETL_CONSTANT etl::chrono::weekday Saturday{ 6U };
#endif
}
//*************************************************************************
/// Hash function for etl::chrono::weekday
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::chrono::weekday>
{
size_t operator()(const etl::chrono::weekday& wd) const
{
unsigned value = wd.c_encoding();
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
return etl::private_hash::generic_hash<size_t>(p, p + sizeof(unsigned));
}
};
#endif
}
#if ETL_HAS_CHRONO_LITERALS_WEEKDAY
#if ETL_USING_CPP11
namespace etl
{
namespace literals
{
namespace chrono_literals
{
//***********************************************************************
/// Literal for weekdays
//***********************************************************************
constexpr etl::chrono::weekday operator ""_weekday(unsigned long long m) noexcept
{
return etl::chrono::weekday(static_cast<unsigned char>(m));
}
}
}
}
#endif
#endif

View File

@ -0,0 +1,151 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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
{
//***********************************************************************
/// weekday_indexed
//***********************************************************************
class weekday_indexed
{
public:
//***********************************************************************
/// Default constructor
//***********************************************************************
ETL_CONSTEXPR weekday_indexed() ETL_NOEXCEPT
{
}
//***********************************************************************
/// Construct from unsigned
//***********************************************************************
ETL_CONSTEXPR weekday_indexed(const etl::chrono::weekday& wd_, unsigned index_) ETL_NOEXCEPT
: wd(wd_)
, i(index_)
{
}
//***********************************************************************
/// Copy constructor
//***********************************************************************
ETL_CONSTEXPR weekday_indexed(const etl::chrono::weekday_indexed& other) ETL_NOEXCEPT
: wd(other.wd)
, i(other.i)
{
}
//***********************************************************************
/// Assignment operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::weekday_indexed& operator =(const etl::chrono::weekday_indexed& rhs) ETL_NOEXCEPT
{
wd = rhs.wd;
i = rhs.i;
return *this;
}
//***********************************************************************
/// Get weekday
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR etl::chrono::weekday weekday() const ETL_NOEXCEPT
{
return wd;
}
//***********************************************************************
/// Get index
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR unsigned index() const ETL_NOEXCEPT
{
return i;
}
//***********************************************************************
/// Returns <b>true</b> if the weekday and index are valid
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
{
return wd.ok() && (i >= 1U) && (i <= 5);
}
private:
etl::chrono::weekday wd;
unsigned i;
};
//***********************************************************************
/// Equality operator
//***********************************************************************
ETL_CONSTEXPR bool operator ==(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT
{
return (wd1.weekday() == wd2.weekday()) &&
(wd1.index() == wd2.index());
}
//***********************************************************************
/// Inequality operator
//***********************************************************************
ETL_CONSTEXPR bool operator !=(const etl::chrono::weekday_indexed& wd1, const etl::chrono::weekday_indexed& wd2) ETL_NOEXCEPT
{
return !(wd1 == wd2);
}
//***********************************************************************
/// weekday index operator from index
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday_indexed etl::chrono::weekday::operator[](unsigned index) const ETL_NOEXCEPT
{
return etl::chrono::weekday_indexed(*this, index);
}
}
//*************************************************************************
/// Hash function for etl::chrono::weekday_indexed
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::chrono::weekday_indexed>
{
size_t operator()(const etl::chrono::weekday_indexed& wdi) const
{
return etl::hash<etl::chrono::weekday>()(wdi.weekday()) ^ etl::hash<unsigned>()(wdi.index());
}
};
#endif
}

View File

@ -0,0 +1,138 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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
{
//***********************************************************************
/// weekday_last
//***********************************************************************
class weekday_last
{
public:
//***********************************************************************
/// Default constructor
//***********************************************************************
ETL_CONSTEXPR weekday_last() ETL_NOEXCEPT
{
}
//***********************************************************************
/// Construct from unsigned
//***********************************************************************
ETL_CONSTEXPR weekday_last(const etl::chrono::weekday& wd_) ETL_NOEXCEPT
: wd(wd_)
{
}
//***********************************************************************
/// Copy constructor
//***********************************************************************
ETL_CONSTEXPR weekday_last(const etl::chrono::weekday_last& other) ETL_NOEXCEPT
: wd(other.wd)
{
}
//***********************************************************************
/// Assignment operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::weekday_last& operator =(const etl::chrono::weekday_last& rhs) ETL_NOEXCEPT
{
wd = rhs.wd;
return *this;
}
//***********************************************************************
/// Get weekday
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR etl::chrono::weekday weekday() const ETL_NOEXCEPT
{
return wd;
}
//***********************************************************************
/// Returns <b>true</b> if the weekday is valid
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
{
return wd.ok();
}
private:
etl::chrono::weekday wd;
};
//***********************************************************************
/// Equality operator
//***********************************************************************
ETL_CONSTEXPR bool operator ==(const etl::chrono::weekday_last& wd1, const etl::chrono::weekday_last& wd2) ETL_NOEXCEPT
{
return (wd1.weekday() == wd2.weekday());
}
//***********************************************************************
/// Inequality operator
//***********************************************************************
ETL_CONSTEXPR bool operator !=(const etl::chrono::weekday_last& wd1, const etl::chrono::weekday_last& wd2) ETL_NOEXCEPT
{
return !(wd1 == wd2);
}
//***********************************************************************
/// weekday index operator from index
//***********************************************************************
ETL_CONSTEXPR etl::chrono::weekday_last etl::chrono::weekday::operator[](etl::chrono::last_spec) const ETL_NOEXCEPT
{
return etl::chrono::weekday_last(*this);
}
}
//*************************************************************************
/// Hash function for etl::chrono::weekday_last
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::chrono::weekday_last>
{
size_t operator()(const etl::chrono::weekday_last& wdl) const
{
return etl::hash<etl::chrono::weekday>()(wdl.weekday());
}
};
#endif
}

View File

@ -0,0 +1,332 @@
///\file
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2023 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
{
//***********************************************************************
/// year
//***********************************************************************
class year
{
public:
//***********************************************************************
/// Default constructor
//***********************************************************************
ETL_CONSTEXPR year() ETL_NOEXCEPT
: value(0)
{
}
//***********************************************************************
/// Construct from unsigned
//***********************************************************************
ETL_CONSTEXPR explicit year(unsigned value_) ETL_NOEXCEPT
: value(value_)
{
}
//***********************************************************************
/// Copy constructor
//***********************************************************************
ETL_CONSTEXPR year(const etl::chrono::year& other) ETL_NOEXCEPT
: value(other.value)
{
}
//***********************************************************************
/// Assignment operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year& operator =(const etl::chrono::year& rhs) ETL_NOEXCEPT
{
value = rhs.value;
return *this;
}
//***********************************************************************
/// Pre-increment operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year& operator ++() ETL_NOEXCEPT
{
++value;
return *this;
}
//***********************************************************************
/// Post-increment operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year operator ++(int) ETL_NOEXCEPT
{
const etl::chrono::year temp = *this;
++value;
return temp;
}
//***********************************************************************
/// Pre-decrement operator
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year& operator --() ETL_NOEXCEPT
{
--value;
return *this;
}
//***********************************************************************
/// Post-decrement operator
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year operator --(int) ETL_NOEXCEPT
{
const etl::chrono::year temp = *this;
--value;
return temp;
}
//***********************************************************************
/// Plus-equals operator adding etl::chrono::years
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year& operator +=(const etl::chrono::years& ys) ETL_NOEXCEPT
{
value += static_cast<unsigned char>(ys.count());
return *this;
}
//***********************************************************************
/// Minus-equals operator subtracting etl::chrono::years
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year& operator -=(const etl::chrono::years& ys) ETL_NOEXCEPT
{
value -= static_cast<unsigned char>(ys.count());
return *this;
}
//***********************************************************************
/// Returns <b>true</b> if the year is within the valid -32767 to 32767 range
//***********************************************************************
ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
{
return (value != -32768);
}
//***********************************************************************
/// The minimum year value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::year min() ETL_NOEXCEPT
{
return etl::chrono::year(-32767);
}
//***********************************************************************
/// The maximum year value for which ok() will return <b>true</b>
//***********************************************************************
static ETL_CONSTEXPR etl::chrono::year max() ETL_NOEXCEPT
{
return etl::chrono::year(32767);
}
//***********************************************************************
/// Returns <b>true</b> if the year is a leap year
//***********************************************************************
ETL_CONSTEXPR bool is_leap() const ETL_NOEXCEPT
{
return ((value % 4) == 0) && !((value % 400) == 0);
}
//***********************************************************************
/// Conversion operator to unsigned int
//***********************************************************************
ETL_CONSTEXPR operator int() const ETL_NOEXCEPT
{
return static_cast<int>(value);
}
private:
int16_t value;
};
//***********************************************************************
/// Equality operator
//***********************************************************************
ETL_CONSTEXPR bool operator ==(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(y1) == static_cast<unsigned>(y2));
}
//***********************************************************************
/// Inequality operator
//***********************************************************************
ETL_CONSTEXPR bool operator !=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return !(y1 == y2);
}
//***********************************************************************
/// Less-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator <(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(y1) < static_cast<unsigned>(y2));
}
//***********************************************************************
/// Less-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator <=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(y1) <= static_cast<unsigned>(y2));
}
//***********************************************************************
/// Greater-than operator
//***********************************************************************
ETL_CONSTEXPR bool operator >(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(y1) > static_cast<unsigned>(y2));
}
//***********************************************************************
/// Greater-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator >=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<unsigned>(y1) >= static_cast<unsigned>(y2));
}
//***********************************************************************
/// Spaceship operator
//***********************************************************************
#if ETL_USING_CPP20
[[nodiscard]] constexpr auto operator <=>(const etl::chrono::year& y1, const etl::chrono::year& y2) noexcept
{
return (static_cast<unsigned>(y1) <=> static_cast<unsigned>(y2));
}
#endif
//***********************************************************************
/// Add etl::chrono::years to etl::chrono::year
///\return etl::chrono::year
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year operator +(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT
{
etl::chrono::year result(y);
result += ys;
return result;
}
//***********************************************************************
/// Add etl::chrono::year to etl::chrono::years
///\return etl::chrono::year
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year operator +(const etl::chrono::years& ys, const etl::chrono::year& y) ETL_NOEXCEPT
{
etl::chrono::year result(y);
result += ys;
return result;
}
//***********************************************************************
/// Subtract etl::chrono::years from etl::chrono::year
///\return etl::chrono::year
//***********************************************************************
ETL_CONSTEXPR etl::chrono::year operator -(const etl::chrono::year& y, const etl::chrono::years& ys) ETL_NOEXCEPT
{
etl::chrono::year result(y);
result -= ys;
return result;
}
//***********************************************************************
/// Subtract etl::chrono::year from etl::chrono::year
///\return etl::chrono::years
//***********************************************************************
ETL_CONSTEXPR etl::chrono::years operator -(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return etl::chrono::years(static_cast<int>(static_cast<unsigned>(y1)) -
static_cast<int>(static_cast<unsigned>(y2)));
}
}
//*************************************************************************
/// Hash function for etl::chrono::year
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::chrono::year>
{
size_t operator()(const etl::chrono::year& y) const
{
int value = y;
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
return etl::private_hash::generic_hash<size_t>(p, p + sizeof(int));
}
};
#endif
}
#if ETL_HAS_CHRONO_LITERALS_YEAR
#if ETL_USING_CPP11
namespace etl
{
namespace literals
{
namespace chrono_literals
{
//***********************************************************************
/// Literal for years
//***********************************************************************
constexpr etl::chrono::year operator ""_year(unsigned long long y) noexcept
{
return etl::chrono::year(static_cast<int16_t>(y));
}
}
}
}
#endif
#endif

View File

@ -58,6 +58,12 @@ add_executable(etl_tests
test_callback_timer_locked.cpp
test_char_traits.cpp
test_checksum.cpp
test_chrono_day.cpp
test_chrono_weekday.cpp
test_chrono_month.cpp
test_chrono_year.cpp
test_circular_buffer.cpp
test_circular_buffer_external_buffer.cpp
test_circular_iterator.cpp

333
test/test_chrono_day.cpp Normal file
View File

@ -0,0 +1,333 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Documentation:
Copyright(c) 2023 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.
******************************************************************************/
#include "etl/platform.h"
#if ETL_USING_CPP20
#include "unit_test_framework.h"
#include "etl/chrono.h"
#include <chrono>
#include <array>
#include <algorithm>
namespace
{
SUITE(test_chrono_day)
{
//*************************************************************************
TEST(test_default_constructor)
{
std::chrono::day std_day;
etl::chrono::day day;
CHECK_EQUAL(std_day.ok(), day.ok());
}
//*************************************************************************
TEST(test_constructor_in_range)
{
for (unsigned i = 0U; i < 256; ++i)
{
std::chrono::day std_day(i);
etl::chrono::day day(i);
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
//*************************************************************************
TEST(test_pre_increment)
{
std::chrono::day std_day(0);
etl::chrono::day day(0);
for (int i = 0; i < 255; ++i)
{
++std_day;
++day;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
//*************************************************************************
TEST(test_post_increment)
{
std::chrono::day std_day(0);
etl::chrono::day day(0);
for (int i = 0; i < 255; ++i)
{
std::chrono::day std_last_day = std_day++;
etl::chrono::day last_day = day++;
CHECK_EQUAL(std_last_day.ok(), last_day.ok());
CHECK_EQUAL(unsigned(std_last_day), unsigned(last_day));
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
//*************************************************************************
TEST(test_pre_decrement)
{
std::chrono::day std_day(256);
etl::chrono::day day(256);
for (int i = 0; i < 255; ++i)
{
--std_day;
--day;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
//*************************************************************************
TEST(test_post_decrement)
{
std::chrono::day std_day(256);
etl::chrono::day day(256);
for (int i = 0; i < 255; ++i)
{
std::chrono::day std_last_day = std_day--;
etl::chrono::day last_day = day--;
CHECK_EQUAL(std_last_day.ok(), last_day.ok());
CHECK_EQUAL(unsigned(std_last_day), unsigned(last_day));
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
//*************************************************************************
TEST(test_plus_equal_days)
{
std::chrono::day std_day(0);
etl::chrono::day day(0);
std::chrono::days std_days(2);
etl::chrono::days days(2);
for (int i = 0; i < 128; ++i)
{
std_day += std_days;
day += days;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
//*************************************************************************
TEST(test_day_plus_days)
{
for (int d = 0; d < 256; ++d)
{
for (int ds = 0; ds < 256; ++ds)
{
std::chrono::day std_day(d);
etl::chrono::day day(d);
std::chrono::days std_days(ds);
etl::chrono::days days(ds);
std_day = std_day + std_days;
day = day + days;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
}
//*************************************************************************
TEST(test_days_plus_day)
{
for (int d = 0; d < 256; ++d)
{
for (int ds = 0; ds < 256; ++ds)
{
std::chrono::day std_day(d);
etl::chrono::day day(d);
std::chrono::days std_days(ds);
etl::chrono::days days(ds);
std_day = std_days + std_day;
day = days + day;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
}
//*************************************************************************
TEST(test_minus_equal_days)
{
for (int d = 0; d <= 256; ++d)
{
for (int ds = 0; ds <= 256; ++ds)
{
std::chrono::day std_day(d);
etl::chrono::day day(d);
std::chrono::days std_days(ds);
etl::chrono::days days(ds);
std_day -= std_days;
day -= days;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
}
//*************************************************************************
TEST(test_day_minus_days)
{
for (int d = 0; d <= 256; ++d)
{
for (int ds = 0; ds <= 256; ++ds)
{
std::chrono::day std_day(d);
etl::chrono::day day(d);
std::chrono::days std_days(ds);
etl::chrono::days days(ds);
std_day = std_day - std_days;
day = day - days;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
}
}
//*************************************************************************
TEST(test_day_minus_day)
{
for (int d = 0; d < 256; ++d)
{
std::chrono::day std_day1(d);
std::chrono::day std_day2(255 - d);
std::chrono::day day1(d);
std::chrono::day day2(255 - d);
auto std_days12 = std_day1 - std_day2;
auto std_days21 = std_day2 - std_day1;
auto days12 = day1 - day2;
auto days21 = day2 - day1;
CHECK_EQUAL(std_days12.count(), days12.count());
CHECK_EQUAL(std_days21.count(), days21.count());
}
}
//*************************************************************************
TEST(test_min_max_day)
{
CHECK_EQUAL(1U, etl::chrono::day::min());
CHECK_EQUAL(31U, etl::chrono::day::max());
}
//*************************************************************************
TEST(test_literal_day)
{
using namespace std::literals::chrono_literals;
using namespace etl::literals::chrono_literals;
std::chrono::day std_day = 25d;
etl::chrono::day day = 25_day;
CHECK_EQUAL(std_day.ok(), day.ok());
CHECK_EQUAL(unsigned(std_day), unsigned(day));
}
//*************************************************************************
TEST(test_day_comparison_operators)
{
etl::chrono::day day10(10);
etl::chrono::day day20(20);
CHECK_TRUE(day10 == day10);
CHECK_FALSE(day10 != day10);
CHECK_TRUE(day10 < day20);
CHECK_FALSE(day10 < day10);
CHECK_FALSE(day20 < day10);
CHECK_TRUE(day10 <= day20);
CHECK_TRUE(day10 <= day10);
CHECK_FALSE(day20 <= day10);
CHECK_FALSE(day10 > day20);
CHECK_FALSE(day10 > day10);
CHECK_TRUE(day20 > day10);
CHECK_FALSE(day10 >= day20);
CHECK_TRUE(day10 >= day10);
CHECK_TRUE(day20 >= day10);
#if ETL_USING_CPP20
CHECK_TRUE((day10 <=> day10) == 0);
CHECK_TRUE((day10 <=> day20) < 0);
CHECK_TRUE((day20 <=> day10) > 0);
#endif
}
//*************************************************************************
TEST(test_day_hashes_are_unique)
{
std::vector<size_t> hashes;
for (int i = 0; i < 256; ++i)
{
hashes.push_back(etl::hash<etl::chrono::day>()(etl::chrono::day(i)));
}
std::sort(hashes.begin(), hashes.end());
(void)std::unique(hashes.begin(), hashes.end());
CHECK_EQUAL(256U, hashes.size());
}
};
}
#endif

385
test/test_chrono_month.cpp Normal file
View File

@ -0,0 +1,385 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Documentation:
Copyright(c) 2023 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.
******************************************************************************/
#include "etl/platform.h"
#if ETL_USING_CPP20
#include "unit_test_framework.h"
#include "etl/chrono.h"
#include <chrono>
#include <array>
#include <algorithm>
namespace
{
SUITE(test_chrono_month)
{
//*************************************************************************
TEST(test_default_constructor)
{
std::chrono::month std_month;
etl::chrono::month month;
CHECK_EQUAL(std_month.ok(), month.ok());
}
//*************************************************************************
TEST(test_constructor_in_range)
{
for (unsigned i = 0U; i < 256; ++i)
{
std::chrono::month std_month(i);
etl::chrono::month month(i);
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
//*************************************************************************
TEST(test_pre_increment)
{
std::chrono::month std_month(0);
etl::chrono::month month(0);
for (int i = 0; i < 255; ++i)
{
++std_month;
++month;
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
//*************************************************************************
TEST(test_post_increment)
{
std::chrono::month std_month(0);
etl::chrono::month month(0);
for (int i = 0; i < 256; ++i)
{
std::chrono::month std_last_month = std_month++;
etl::chrono::month last_month = month++;
CHECK_EQUAL(std_last_month.ok(), last_month.ok());
CHECK_EQUAL(unsigned(std_last_month), unsigned(last_month));
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
//*************************************************************************
TEST(test_pre_decrement)
{
std::chrono::month std_month(255);
etl::chrono::month month(255);
for (int i = 0; i < 256; ++i)
{
--std_month;
--month;
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
//*************************************************************************
TEST(test_post_decrement)
{
std::chrono::month std_month(255);
etl::chrono::month month(255);
for (int i = 0; i < 256; ++i)
{
std::chrono::month std_last_month = std_month--;
etl::chrono::month last_month = month--;
CHECK_EQUAL(std_last_month.ok(), last_month.ok());
CHECK_EQUAL(unsigned(std_last_month), unsigned(last_month));
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
//*************************************************************************
TEST(test_plus_equal_months)
{
for (int m = 0; m <= 12; ++m)
{
for (int ms = 0; ms <= 24; ++ms)
{
std::chrono::month std_month(m);
etl::chrono::month month(m);
std::chrono::months std_months(ms);
etl::chrono::months months(ms);
std_month += std_months;
month += months;
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
}
//*************************************************************************
TEST(test_month_plus_months)
{
for (int m = 0; m <= 12; ++m)
{
for (int ms = 0; ms <= 24; ++ms)
{
std::chrono::month std_month(m);
etl::chrono::month month(m);
std::chrono::months std_months(ms);
etl::chrono::months months(ms);
std_month = std_month + std_months;
month = month + months;
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
}
//*************************************************************************
TEST(test_months_plus_month)
{
for (int m = 0; m <= 12; ++m)
{
for (int ms = 0; ms <= 24; ++ms)
{
std::chrono::month std_month(m);
etl::chrono::month month(m);
std::chrono::months std_months(ms);
etl::chrono::months months(ms);
std_month = std_months + std_month;
month = months + month;
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
}
//*************************************************************************
TEST(test_minus_equal_months)
{
for (int m = 0; m <= 12; ++m)
{
for (int ms = 0; ms <= 24; ++ms)
{
std::chrono::month std_month(m);
etl::chrono::month month(m);
std::chrono::months std_months(ms);
etl::chrono::months months(ms);
std_month -= std_months;
month -= months;
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
}
//*************************************************************************
TEST(test_month_minus_months)
{
for (int m = 0; m <= 12; ++m)
{
for (int ms = 0; ms <= 24; ++ms)
{
std::chrono::month std_month(m);
etl::chrono::month month(m);
std::chrono::months std_months(ms);
etl::chrono::months months(ms);
std_month = std_month - std_months;
month = month - months;
CHECK_EQUAL(std_month.ok(), month.ok());
CHECK_EQUAL(unsigned(std_month), unsigned(month));
}
}
}
//*************************************************************************
TEST(test_month_minus_month)
{
for (int m = 0; m < 256; ++m)
{
std::chrono::month std_month1(m);
std::chrono::month std_month2(255 - m);
std::chrono::month month1(m);
std::chrono::month month2(255 - m);
auto std_months12 = std_month1 - std_month2;
auto std_months21 = std_month2 - std_month1;
auto months12 = month1 - month2;
auto months21 = month2 - month1;
CHECK_EQUAL(std_months12.count(), months12.count());
CHECK_EQUAL(std_months21.count(), months21.count());
}
}
//*************************************************************************
TEST(test_min_max_month)
{
CHECK_EQUAL(1U, etl::chrono::month::min());
CHECK_EQUAL(12U, etl::chrono::month::max());
}
//*************************************************************************
TEST(test_literal_month)
{
using namespace etl::literals::chrono_literals;
etl::chrono::month month1 = 1_month;
etl::chrono::month month2 = 2_month;
etl::chrono::month month3 = 3_month;
etl::chrono::month month4 = 4_month;
etl::chrono::month month5 = 5_month;
etl::chrono::month month6 = 6_month;
etl::chrono::month month7 = 7_month;
etl::chrono::month month8 = 8_month;
etl::chrono::month month9 = 9_month;
etl::chrono::month month10 = 10_month;
etl::chrono::month month11 = 11_month;
etl::chrono::month month12 = 12_month;
CHECK_TRUE(month1.ok());
CHECK_TRUE(month2.ok());
CHECK_TRUE(month3.ok());
CHECK_TRUE(month4.ok());
CHECK_TRUE(month5.ok());
CHECK_TRUE(month6.ok());
CHECK_TRUE(month7.ok());
CHECK_TRUE(month8.ok());
CHECK_TRUE(month9.ok());
CHECK_TRUE(month10.ok());
CHECK_TRUE(month11.ok());
CHECK_TRUE(month12.ok());
CHECK_EQUAL(1U, unsigned(month1));
CHECK_EQUAL(2U, unsigned(month2));
CHECK_EQUAL(3U, unsigned(month3));
CHECK_EQUAL(4U, unsigned(month4));
CHECK_EQUAL(5U, unsigned(month5));
CHECK_EQUAL(6U, unsigned(month6));
CHECK_EQUAL(7U, unsigned(month7));
CHECK_EQUAL(8U, unsigned(month8));
CHECK_EQUAL(9U, unsigned(month9));
CHECK_EQUAL(10U, unsigned(month10));
CHECK_EQUAL(11U, unsigned(month11));
CHECK_EQUAL(12U, unsigned(month12));
}
//*************************************************************************
TEST(test_month_comparison_operators)
{
etl::chrono::month month1(1);
etl::chrono::month month2(2);
CHECK_TRUE(month1 == month1);
CHECK_FALSE(month1 != month1);
CHECK_TRUE(month1 < month2);
CHECK_FALSE(month1 < month1);
CHECK_FALSE(month2 < month1);
CHECK_TRUE(month1 <= month2);
CHECK_TRUE(month1 <= month1);
CHECK_FALSE(month2 <= month1);
CHECK_FALSE(month1 > month2);
CHECK_FALSE(month1 > month1);
CHECK_TRUE(month2 > month1);
CHECK_FALSE(month1 >= month2);
CHECK_TRUE(month1 >= month1);
CHECK_TRUE(month2 >= month1);
#if ETL_USING_CPP20
CHECK_TRUE((month1 <=> month1) == 0);
CHECK_TRUE((month1 <=> month2) < 0);
CHECK_TRUE((month2 <=> month1) > 0);
#endif
}
//*************************************************************************
TEST(test_month_hashes_are_unique)
{
std::vector<size_t> hashes;
for (int i = 0; i < 256; ++i)
{
hashes.push_back(etl::hash<etl::chrono::month>()(etl::chrono::month(i)));
}
std::sort(hashes.begin(), hashes.end());
(void)std::unique(hashes.begin(), hashes.end());
CHECK_EQUAL(256U, hashes.size());
}
//*************************************************************************
TEST(test_month_types)
{
CHECK_EQUAL(static_cast<unsigned>(std::chrono::January), static_cast<unsigned>(etl::chrono::January));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::February), static_cast<unsigned>(etl::chrono::February));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::March), static_cast<unsigned>(etl::chrono::March));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::April), static_cast<unsigned>(etl::chrono::April));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::May), static_cast<unsigned>(etl::chrono::May));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::June), static_cast<unsigned>(etl::chrono::June));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::July), static_cast<unsigned>(etl::chrono::July));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::August), static_cast<unsigned>(etl::chrono::August));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::September), static_cast<unsigned>(etl::chrono::September));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::October), static_cast<unsigned>(etl::chrono::October));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::November), static_cast<unsigned>(etl::chrono::November));
CHECK_EQUAL(static_cast<unsigned>(std::chrono::December), static_cast<unsigned>(etl::chrono::December));
}
};
}
#endif

View File

@ -0,0 +1,379 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Documentation:
Copyright(c) 2023 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.
******************************************************************************/
#include "etl/platform.h"
#include "unit_test_framework.h"
#include "etl/chrono.h"
#include <chrono>
#include <array>
#include <algorithm>
namespace
{
SUITE(test_chrono_weekday)
{
//*************************************************************************
TEST(test_default_constructor)
{
etl::chrono::weekday weekday;
CHECK_FALSE(weekday.ok());
}
//*************************************************************************
TEST(test_constructor_in_range)
{
for (unsigned i = 0U; i < 7U; ++i)
{
etl::chrono::weekday weekday(i);
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(i, weekday.c_encoding());
CHECK_EQUAL((i == 0U) ? 7U : i, weekday.iso_encoding());
}
}
//*************************************************************************
TEST(test_constructor_out_of_range)
{
for (unsigned i = 8U; i < 256U; ++i)
{
etl::chrono::weekday weekday(i);
CHECK_FALSE(weekday.ok());
CHECK_EQUAL(i, weekday.c_encoding());
CHECK_EQUAL((i == 0U) ? 7U : i, weekday.iso_encoding());
}
}
//*************************************************************************
TEST(test_pre_increment)
{
etl::chrono::weekday weekday(0);
unsigned count = 0;
for (int i = 0; i < 255; ++i)
{
++weekday;
++count;
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(count % 7, weekday.c_encoding());
CHECK_EQUAL((count % 7 == 0U) ? 7U : count % 7, weekday.iso_encoding());
}
}
//*************************************************************************
TEST(test_post_increment)
{
etl::chrono::weekday weekday(0);
unsigned count = 0;
for (int i = 0; i < 255; ++i)
{
etl::chrono::weekday last_weekday = weekday++;
unsigned last_count = count++;
CHECK_TRUE(last_weekday.ok());
CHECK_EQUAL(last_count % 7, last_weekday.c_encoding());
CHECK_EQUAL((last_count % 7 == 0U) ? 7U : last_count % 7, last_weekday.iso_encoding());
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(count % 7, weekday.c_encoding());
CHECK_EQUAL((count % 7 == 0U) ? 7U : count % 7, weekday.iso_encoding());
}
}
//*************************************************************************
TEST(test_pre_decrement)
{
etl::chrono::weekday weekday(255U);
unsigned count = 255U;
for (int i = 0; i < 255; ++i)
{
--weekday;
--count;
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(count % 7, weekday.c_encoding());
CHECK_EQUAL((count % 7 == 0U) ? 7U : count % 7, weekday.iso_encoding());
}
}
//*************************************************************************
TEST(test_post_decrement)
{
etl::chrono::weekday weekday(255U);
unsigned count = 255U;
for (int i = 0; i < 255; ++i)
{
etl::chrono::weekday last_weekday = weekday--;
unsigned last_count = count--;
if (last_count == 255U)
{
CHECK_FALSE(last_weekday.ok());
CHECK_EQUAL(255U, last_weekday.c_encoding());
CHECK_EQUAL(255U, last_weekday.iso_encoding());
}
else
{
CHECK_TRUE(last_weekday.ok());
CHECK_EQUAL(last_count % 7, last_weekday.c_encoding());
CHECK_EQUAL((last_count % 7 == 0U) ? 7U : last_count % 7, last_weekday.iso_encoding());
}
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(count % 7, weekday.c_encoding());
CHECK_EQUAL((count % 7 == 0U) ? 7U : count % 7, weekday.iso_encoding());
}
}
//*************************************************************************
TEST(test_plus_equal_days)
{
for (unsigned wd = 0; wd <= 6; ++wd)
{
for (unsigned ds = 0; ds <= 14; ++ds)
{
etl::chrono::weekday weekday(wd);
etl::chrono::days days(ds);
weekday += days;
unsigned expected = (wd + ds) % 7;
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(expected, weekday.c_encoding());
CHECK_EQUAL((expected == 0U) ? 7U : expected, weekday.iso_encoding());
}
}
}
//*************************************************************************
TEST(test_weekday_plus_days)
{
for (unsigned wd = 0; wd <= 6; ++wd)
{
for (unsigned ds = 0; ds <= 14; ++ds)
{
etl::chrono::weekday weekday(wd);
etl::chrono::days days(ds);
weekday = weekday + days;
unsigned expected = (wd + ds) % 7;
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(expected, weekday.c_encoding());
CHECK_EQUAL((expected == 0U) ? 7U : expected, weekday.iso_encoding());
}
}
}
//*************************************************************************
TEST(test_days_plus_weekday)
{
for (unsigned wd = 0; wd <= 6; ++wd)
{
for (unsigned ds = 0; ds <= 14; ++ds)
{
etl::chrono::weekday weekday(wd);
etl::chrono::days days(ds);
weekday = weekday + days;
unsigned expected = (ds + wd) % 7;
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(expected, weekday.c_encoding());
CHECK_EQUAL((expected == 0U) ? 7U : expected, weekday.iso_encoding());
}
}
}
//*************************************************************************
TEST(test_minus_equal_days)
{
for (unsigned wd = 0; wd <= 6; ++wd)
{
for (unsigned ds = 0; ds <= 14; ++ds)
{
etl::chrono::weekday weekday(wd);
etl::chrono::days days(ds);
weekday -= days;
unsigned expected = ((wd + 7U) - (ds % 7U)) % 7U;
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(expected, weekday.c_encoding());
CHECK_EQUAL((expected == 0U) ? 7U : expected, weekday.iso_encoding());
}
}
}
//*************************************************************************
TEST(test_weekday_minus_days)
{
for (unsigned wd = 0; wd <= 6; ++wd)
{
for (unsigned ds = 0; ds <= 14; ++ds)
{
etl::chrono::weekday weekday(wd);
etl::chrono::days days(ds);
weekday = weekday - days;
unsigned expected = ((wd + 7U) - (ds % 7U)) % 7U;
CHECK_TRUE(weekday.ok());
CHECK_EQUAL(expected, weekday.c_encoding());
CHECK_EQUAL((expected == 0U) ? 7U : expected, weekday.iso_encoding());
}
}
}
//*************************************************************************
TEST(test_weekday_minus_weekday)
{
for (int m = 0; m < 7; ++m)
{
etl::chrono::weekday weekday1(m);
etl::chrono::weekday weekday2(7 - m);
std::chrono::weekday std_weekday1(m);
std::chrono::weekday std_weekday2(7 - m);
auto days12 = weekday1 - weekday2;
auto days21 = weekday2 - weekday1;
auto std_days12 = std_weekday1 - std_weekday2;
auto std_days21 = std_weekday2 - std_weekday1;
CHECK_EQUAL(std_days12.count(), days12.count());
CHECK_EQUAL(std_days21.count(), days21.count());
}
}
//*************************************************************************
TEST(test_min_max_weekday)
{
CHECK_EQUAL(etl::chrono::Sunday.c_encoding(), etl::chrono::weekday::min().c_encoding());
CHECK_EQUAL(etl::chrono::Saturday.c_encoding(), etl::chrono::weekday::max().c_encoding());
}
//*************************************************************************
TEST(test_weekday_constants)
{
CHECK_EQUAL(0U, etl::chrono::Sunday.c_encoding());
CHECK_EQUAL(1U, etl::chrono::Monday.c_encoding());
CHECK_EQUAL(2U, etl::chrono::Tuesday.c_encoding());
CHECK_EQUAL(3U, etl::chrono::Wednesday.c_encoding());
CHECK_EQUAL(4U, etl::chrono::Thursday.c_encoding());
CHECK_EQUAL(5U, etl::chrono::Friday.c_encoding());
CHECK_EQUAL(6U, etl::chrono::Saturday.c_encoding());
}
//*************************************************************************
TEST(test_weekday_literals)
{
using namespace etl::literals::chrono_literals;
etl::chrono::weekday weekday0 = 0_weekday;
etl::chrono::weekday weekday1 = 1_weekday;
etl::chrono::weekday weekday2 = 2_weekday;
etl::chrono::weekday weekday3 = 3_weekday;
etl::chrono::weekday weekday4 = 4_weekday;
etl::chrono::weekday weekday5 = 5_weekday;
etl::chrono::weekday weekday6 = 6_weekday;
CHECK_TRUE(weekday0.ok());
CHECK_TRUE(weekday1.ok());
CHECK_TRUE(weekday2.ok());
CHECK_TRUE(weekday3.ok());
CHECK_TRUE(weekday4.ok());
CHECK_TRUE(weekday5.ok());
CHECK_TRUE(weekday6.ok());
CHECK_EQUAL(0U, weekday0.c_encoding());
CHECK_EQUAL(1U, weekday1.c_encoding());
CHECK_EQUAL(2U, weekday2.c_encoding());
CHECK_EQUAL(3U, weekday3.c_encoding());
CHECK_EQUAL(4U, weekday4.c_encoding());
CHECK_EQUAL(5U, weekday5.c_encoding());
CHECK_EQUAL(6U, weekday6.c_encoding());
}
//*************************************************************************
TEST(test_weekday_comparison_operators)
{
etl::chrono::weekday weekday1(1);
etl::chrono::weekday weekday2(2);
CHECK_TRUE(weekday1 == weekday1);
CHECK_FALSE(weekday1 != weekday1);
CHECK_TRUE(weekday1 < weekday2);
CHECK_FALSE(weekday1 < weekday1);
CHECK_FALSE(weekday2 < weekday1);
CHECK_TRUE(weekday1 <= weekday2);
CHECK_TRUE(weekday1 <= weekday1);
CHECK_FALSE(weekday2 <= weekday1);
CHECK_FALSE(weekday1 > weekday2);
CHECK_FALSE(weekday1 > weekday1);
CHECK_TRUE(weekday2 > weekday1);
CHECK_FALSE(weekday1 >= weekday2);
CHECK_TRUE(weekday1 >= weekday1);
CHECK_TRUE(weekday2 >= weekday1);
#if ETL_USING_CPP20
CHECK_TRUE((weekday1 <=> weekday1) == 0);
CHECK_TRUE((weekday1 <=> weekday2) < 0);
CHECK_TRUE((weekday2 <=> weekday1) > 0);
#endif
}
//*************************************************************************
TEST(test_weekday_hashes_are_unique)
{
std::vector<size_t> hashes;
for (int i = 0; i < 256; ++i)
{
hashes.push_back(etl::hash<etl::chrono::weekday>()(etl::chrono::weekday(i)));
}
std::sort(hashes.begin(), hashes.end());
(void)std::unique(hashes.begin(), hashes.end());
CHECK_EQUAL(256U, hashes.size());
}
};
}

317
test/test_chrono_year.cpp Normal file
View File

@ -0,0 +1,317 @@
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Documentation:
Copyright(c) 2023 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.
******************************************************************************/
#include "etl/platform.h"
#if ETL_USING_CPP20
#include "unit_test_framework.h"
#include "etl/chrono.h"
#include <chrono>
#include <algorithm>
namespace
{
SUITE(test_chrono_year)
{
//*************************************************************************
TEST(test_default_constructor)
{
std::chrono::year std_year;
etl::chrono::year year;
CHECK_EQUAL(std_year.ok(), year.ok());
}
//*************************************************************************
TEST(test_constructor_in_range)
{
for (int32_t i = -32767; i <= 32767; ++i)
{
std::chrono::year std_year(i);
etl::chrono::year year(i);
CHECK_TRUE(year.ok());
CHECK_EQUAL(i, int(year));
}
}
//*************************************************************************
TEST(test_pre_increment)
{
etl::chrono::year year(-32767);
int count = int(year);
for (int32_t i = 0; i < 65534; ++i)
{
++count;
etl::chrono::year this_year = ++year;
CHECK_TRUE(year.ok());
CHECK_EQUAL(count, year);
CHECK_EQUAL(this_year, year);
}
}
//*************************************************************************
TEST(test_post_increment)
{
std::chrono::year std_year(-32767);
etl::chrono::year year(-32767);
for (int32_t i = 0; i < 65536; ++i)
{
std::chrono::year std_last_year = std_year++;
etl::chrono::year last_year = year++;
CHECK_EQUAL(std_last_year.ok(), last_year.ok());
CHECK_EQUAL(int(std_last_year), int(last_year));
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_pre_decrement)
{
std::chrono::year std_year(256);
etl::chrono::year year(256);
for (int i = 0; i < 255; ++i)
{
--std_year;
--year;
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_post_decrement)
{
std::chrono::year std_year(256);
etl::chrono::year year(256);
for (int i = 0; i < 255; ++i)
{
std::chrono::year std_last_year = std_year--;
etl::chrono::year last_year = year--;
CHECK_EQUAL(std_last_year.ok(), last_year.ok());
CHECK_EQUAL(int(std_last_year), int(last_year));
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_plus_equal_years)
{
std::chrono::year std_year(0);
etl::chrono::year year(0);
std::chrono::years std_years(2);
etl::chrono::years years(2);
for (int i = 0; i < 128; ++i)
{
std_year += std_years;
year += years;
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_year_plus_years)
{
std::chrono::year std_year(0);
etl::chrono::year year(0);
std::chrono::years std_years(2);
etl::chrono::years years(2);
for (int i = 0; i < 128; ++i)
{
std_year = std_year + std_years;
year = year + years;
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_years_plus_year)
{
std::chrono::year std_year(0);
etl::chrono::year year(0);
std::chrono::years std_years(2);
etl::chrono::years years(2);
for (int i = 0; i < 128; ++i)
{
std_year = std_years + std_year;
year = years + year;
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_minus_equal_years)
{
std::chrono::year std_year(256);
etl::chrono::year year(256);
std::chrono::years std_years(2);
etl::chrono::years years(2);
for (int i = 0; i < 128; ++i)
{
std_year -= std_years;
year -= years;
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_year_minus_years)
{
std::chrono::year std_year(0);
etl::chrono::year year(0);
std::chrono::years std_years(2);
etl::chrono::years years(2);
for (int i = 0; i < 128; ++i)
{
std_year = std_year - std_years;
year = year - years;
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
}
//*************************************************************************
TEST(test_year_minus_year)
{
for (int i = 1; i < 31; ++i)
{
std::chrono::year std_year1(i);
std::chrono::year std_year2(31 - i);
etl::chrono::year year1(i);
etl::chrono::year year2(31 - i);
std::chrono::years std_years = std_year1 - std_year2;
etl::chrono::years years = year1 - year2;
CHECK_EQUAL(std_years.count(), years.count());
}
}
//*************************************************************************
TEST(test_min_max_year)
{
CHECK_EQUAL(-32767, etl::chrono::year::min());
CHECK_EQUAL(32767, etl::chrono::year::max());
}
//*************************************************************************
TEST(test_literal_year)
{
using namespace std::literals::chrono_literals;
using namespace etl::literals::chrono_literals;
std::chrono::year std_year = 25y;
etl::chrono::year year = 25_year;
CHECK_EQUAL(std_year.ok(), year.ok());
CHECK_EQUAL(int(std_year), int(year));
}
//*************************************************************************
TEST(test_year_comparison_operators)
{
etl::chrono::year year10(10);
etl::chrono::year year20(20);
CHECK_TRUE(year10 == year10);
CHECK_FALSE(year10 != year10);
CHECK_TRUE(year10 < year20);
CHECK_FALSE(year10 < year10);
CHECK_FALSE(year20 < year10);
CHECK_TRUE(year10 <= year20);
CHECK_TRUE(year10 <= year10);
CHECK_FALSE(year20 <= year10);
CHECK_FALSE(year10 > year20);
CHECK_FALSE(year10 > year10);
CHECK_TRUE(year20 > year10);
CHECK_FALSE(year10 >= year20);
CHECK_TRUE(year10 >= year10);
CHECK_TRUE(year20 >= year10);
#if ETL_USING_CPP20
CHECK_TRUE((year10 <=> year10) == 0);
CHECK_TRUE((year10 <=> year20) < 0);
CHECK_TRUE((year20 <=> year10) > 0);
#endif
}
//*************************************************************************
TEST(test_year_hashes_are_unique)
{
std::vector<size_t> hashes;
for (int32_t i = -32767; i < 32768; ++i)
{
hashes.push_back(etl::hash<etl::chrono::year>()(etl::chrono::year(i)));
}
std::sort(hashes.begin(), hashes.end());
(void)std::unique(hashes.begin(), hashes.end());
CHECK_EQUAL(65535U, hashes.size());
}
};
}
#endif

View File

@ -3027,6 +3027,7 @@
<ClInclude Include="..\..\include\etl\callback_timer_atomic.h" />
<ClInclude Include="..\..\include\etl\callback_timer_interrupt.h" />
<ClInclude Include="..\..\include\etl\callback_timer_locked.h" />
<ClInclude Include="..\..\include\etl\chrono.h" />
<ClInclude Include="..\..\include\etl\circular_buffer.h" />
<ClInclude Include="..\..\include\etl\circular_iterator.h" />
<ClInclude Include="..\..\include\etl\combinations.h" />
@ -3091,6 +3092,14 @@
<ClInclude Include="..\..\include\etl\poly_span.h" />
<ClInclude Include="..\..\include\etl\private\bitset_legacy.h" />
<ClInclude Include="..\..\include\etl\private\bitset_new.h" />
<ClInclude Include="..\..\include\etl\private\chrono\day.h" />
<ClInclude Include="..\..\include\etl\private\chrono\duration.h" />
<ClInclude Include="..\..\include\etl\private\chrono\last_spec.h" />
<ClInclude Include="..\..\include\etl\private\chrono\month.h" />
<ClInclude Include="..\..\include\etl\private\chrono\weekday.h" />
<ClInclude Include="..\..\include\etl\private\chrono\weekday_indexed.h" />
<ClInclude Include="..\..\include\etl\private\chrono\weekday_last.h" />
<ClInclude Include="..\..\include\etl\private\chrono\year.h" />
<ClInclude Include="..\..\include\etl\private\diagnostic_array_bounds_push.h" />
<ClInclude Include="..\..\include\etl\private\diagnostic_cxx_20_compat_push.h" />
<ClInclude Include="..\..\include\etl\private\diagnostic_deprecated_push.h" />
@ -7947,6 +7956,10 @@
<ClCompile Include="..\test_callback_timer_interrupt.cpp" />
<ClCompile Include="..\test_callback_timer_locked.cpp" />
<ClCompile Include="..\test_char_traits.cpp" />
<ClCompile Include="..\test_chrono_day.cpp" />
<ClCompile Include="..\test_chrono_month.cpp" />
<ClCompile Include="..\test_chrono_weekday.cpp" />
<ClCompile Include="..\test_chrono_year.cpp" />
<ClCompile Include="..\test_circular_buffer.cpp" />
<ClCompile Include="..\test_circular_buffer_external_buffer.cpp" />
<ClCompile Include="..\test_circular_iterator.cpp" />

View File

@ -220,6 +220,12 @@
<Filter Include="Tests\Messaging">
<UniqueIdentifier>{c75cedd3-8b6c-4662-b965-aecbe7fd5d1c}</UniqueIdentifier>
</Filter>
<Filter Include="ETL\Private\chrono">
<UniqueIdentifier>{46e23f29-ce01-418f-8331-9c739774414c}</UniqueIdentifier>
</Filter>
<Filter Include="Tests\Chrono">
<UniqueIdentifier>{1b1afa65-108a-4665-9e74-3c67a27ff917}</UniqueIdentifier>
</Filter>
<Filter Include="ETL\Codecs">
<UniqueIdentifier>{6bbca8f0-f707-45ee-9dad-6d41d401bbaf}</UniqueIdentifier>
</Filter>
@ -1371,6 +1377,36 @@
<ClInclude Include="..\..\include\etl\stringify.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\chrono.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\duration.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\day.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\month.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\year.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\weekday.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\weekday_indexed.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\weekday_last.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\last_spec.h">
<Filter>ETL\Private\chrono</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\private\chrono\day.h">
<Filter>ETL\Utilities</Filter>
</ClInclude>
<ClInclude Include="..\..\include\etl\base64_encoder.h">
<Filter>ETL\Codecs</Filter>
</ClInclude>
@ -2486,6 +2522,18 @@
<ClCompile Include="..\test_macros.cpp">
<Filter>Tests\Misc</Filter>
</ClCompile>
<ClCompile Include="..\test_chrono_day.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\test_chrono_month.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\test_chrono_year.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\test_chrono_weekday.cpp">
<Filter>Tests\Chrono</Filter>
</ClCompile>
<ClCompile Include="..\syntax_check\absolute.h.t.cpp">
<Filter>Tests\Syntax Checks\Source</Filter>
</ClCompile>