Loading...
Searching...
No Matches
year.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 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 year
{
public:
using rep = int16_t;
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR year() ETL_NOEXCEPT
: value(0)
{
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR explicit year(int value_) ETL_NOEXCEPT
: value(value_)
{
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year& operator++() ETL_NOEXCEPT
{
++value;
return *this;
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year operator++(int) ETL_NOEXCEPT
{
etl::chrono::year temp = *this;
++value;
return temp;
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year& operator--() ETL_NOEXCEPT
{
--value;
return *this;
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year operator--(int) ETL_NOEXCEPT
{
etl::chrono::year temp = *this;
--value;
return temp;
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year& operator+=(const etl::chrono::years& ys) ETL_NOEXCEPT
{
value += ys.count();
return *this;
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 etl::chrono::year& operator-=(const etl::chrono::years& ys) ETL_NOEXCEPT
{
value -= ys.count();
return *this;
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
{
return (value != etl::integral_limits<int16_t>::min);
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD
static ETL_CONSTEXPR14 etl::chrono::year min() ETL_NOEXCEPT
{
return etl::chrono::year(-32767);
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD
static ETL_CONSTEXPR14 etl::chrono::year max() ETL_NOEXCEPT
{
return etl::chrono::year(32767);
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 bool is_leap() const ETL_NOEXCEPT
{
return ((value % 4) == 0) && // Divisible by 4
(((value % 100) != 0) || // but not divisible by 100
((value % 400) == 0)); // unless divisible by 400
}
//***********************************************************************
//***********************************************************************
ETL_CONSTEXPR14 /*explicit*/ operator int() const ETL_NOEXCEPT
{
return value;
}
//***********************************************************************
//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const year& other) const ETL_NOEXCEPT
{
if (value < other.value)
return -1;
if (value > other.value)
return 1;
return 0;
}
private:
rep value;
};
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 bool operator==(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<int>(y1) == static_cast<int>(y2));
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 bool operator!=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return !(y1 == y2);
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 bool operator<(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<int>(y1) < static_cast<int>(y2));
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 bool operator<=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<int>(y1) <= static_cast<int>(y2));
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 bool operator>(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<int>(y1) > static_cast<int>(y2));
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 bool operator>=(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<int>(y1) >= static_cast<int>(y2));
}
//***********************************************************************
//***********************************************************************
#if ETL_USING_CPP20
[[nodiscard]]
inline constexpr auto operator<=>(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return (static_cast<int>(y1) <=> static_cast<int>(y2));
}
#endif
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 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;
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 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;
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 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;
}
//***********************************************************************
//***********************************************************************
inline ETL_CONSTEXPR14 etl::chrono::years operator-(const etl::chrono::year& y1, const etl::chrono::year& y2) ETL_NOEXCEPT
{
return etl::chrono::years(static_cast<int>(y1) - static_cast<int>(y2));
}
} // namespace chrono
//*************************************************************************
//*************************************************************************
#if ETL_USING_8BIT_TYPES
template <>
struct hash<etl::chrono::year>
{
size_t operator()(const etl::chrono::year& y) const
{
etl::chrono::year::rep value = static_cast<etl::chrono::year::rep>(static_cast<int>(y));
const uint8_t* p = reinterpret_cast<const uint8_t*>(&value);
return etl::private_hash::generic_hash<size_t>(p, p + sizeof(value));
}
};
#endif
} // namespace etl
#if ETL_HAS_CHRONO_LITERALS_YEAR
namespace etl
{
inline namespace literals
{
inline namespace chrono_literals
{
//***********************************************************************
//***********************************************************************
#if ETL_USING_VERBOSE_CHRONO_LITERALS
inline ETL_CONSTEXPR14 etl::chrono::year operator""_year(unsigned long long y) ETL_NOEXCEPT
#else
inline ETL_CONSTEXPR14 etl::chrono::year operator""_y(unsigned long long y) ETL_NOEXCEPT
#endif
{
return etl::chrono::year(static_cast<int>(y));
}
} // namespace chrono_literals
} // namespace literals
} // namespace etl
#endif
etl::chrono::year
year
Definition year.h:43
etl::chrono::year::operator--
ETL_CONSTEXPR14 etl::chrono::year operator--(int) ETL_NOEXCEPT
Post-decrement operator.
Definition year.h:98
etl::chrono::year::is_leap
ETL_NODISCARD ETL_CONSTEXPR14 bool is_leap() const ETL_NOEXCEPT
Returns true if the year is a leap year.
Definition year.h:156
etl::chrono::year::year
ETL_CONSTEXPR year() ETL_NOEXCEPT
Default constructor.
Definition year.h:51
etl::chrono::year::operator--
ETL_CONSTEXPR14 etl::chrono::year & operator--() ETL_NOEXCEPT
Pre-decrement operator.
Definition year.h:88
etl::chrono::year::year
ETL_CONSTEXPR year(int value_) ETL_NOEXCEPT
Construct from int.
Definition year.h:59
etl::chrono::year::operator++
ETL_CONSTEXPR14 etl::chrono::year & operator++() ETL_NOEXCEPT
Pre-increment operator.
Definition year.h:67
etl::chrono::year::operator-=
ETL_CONSTEXPR14 etl::chrono::year & operator-=(const etl::chrono::years &ys) ETL_NOEXCEPT
Minus-equals operator subtracting etl::chrono::years.
Definition year.h:119
etl::chrono::year::min
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year min() ETL_NOEXCEPT
The minimum year value for which ok() will return true.
Definition year.h:139
etl::chrono::year::operator+=
ETL_CONSTEXPR14 etl::chrono::year & operator+=(const etl::chrono::years &ys) ETL_NOEXCEPT
Plus-equals operator adding etl::chrono::years.
Definition year.h:109
etl::chrono::year::compare
ETL_NODISCARD ETL_CONSTEXPR14 int compare(const year &other) const ETL_NOEXCEPT
Definition year.h:177
etl::chrono::year::ok
ETL_NODISCARD ETL_CONSTEXPR14 bool ok() const ETL_NOEXCEPT
Definition year.h:130
etl::chrono::year::max
static ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::year max() ETL_NOEXCEPT
The maximum year value for which ok() will return true.
Definition year.h:148
etl::chrono::year::operator++
ETL_CONSTEXPR14 etl::chrono::year operator++(int) ETL_NOEXCEPT
Post-increment operator.
Definition year.h:77
etl::chrono::operator+
ETL_CONSTEXPR14 etl::chrono::day operator+(const etl::chrono::day &d, const etl::chrono::days &ds) ETL_NOEXCEPT
Spaceship operator.
Definition day.h:226
etl::chrono::operator-
ETL_CONSTEXPR14 etl::chrono::day operator-(const etl::chrono::day &d, const etl::chrono::days &ds) ETL_NOEXCEPT
Definition day.h:252
etl::chrono::operator<
ETL_CONSTEXPR14 bool operator<(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Less-than operator.
Definition day.h:182
etl::chrono::operator>=
ETL_CONSTEXPR14 bool operator>=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Greater-than-or-equal operator.
Definition day.h:206
etl::chrono::operator==
ETL_CONSTEXPR14 bool operator==(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Equality operator.
Definition day.h:166
etl::chrono::operator<=
ETL_CONSTEXPR14 bool operator<=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Less-than-or-equal operator.
Definition day.h:190
etl::chrono::operator!=
ETL_CONSTEXPR14 bool operator!=(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Inequality operator.
Definition day.h:174
etl::chrono::operator>
ETL_CONSTEXPR14 bool operator>(const etl::chrono::day &d1, const etl::chrono::day &d2) ETL_NOEXCEPT
Greater-than operator.
Definition day.h:198
etl::integral_limits
Definition integral_limits.h:517
etl
Definition absolute.h:40