Skip to content

Commit

Permalink
Merge branch 'feature/#757-Add-time-date-classes' of https://github.c…
Browse files Browse the repository at this point in the history
…om/ETLCPP/etl into feature/#757-Add-time-date-classes
  • Loading branch information
John Wellbelove committed Sep 16, 2023
2 parents f9d27d9 + 479fd09 commit 5c6d32d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion include/etl/private/chrono/day.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day operator ""_d(unsigned long long d) ETL_NOEXCEPT
{
return etl::chrono::day(d);
return etl::chrono::day(static_cast<unsigned>(d));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions include/etl/private/chrono/duration.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ namespace etl

//***********************************************************************
ETL_CONSTEXPR duration() ETL_NOEXCEPT
: value(0)
{
}

Expand Down
83 changes: 44 additions & 39 deletions include/etl/private/chrono/month.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ 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
//***********************************************************************
Expand Down Expand Up @@ -88,12 +94,7 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator ++() ETL_NOEXCEPT
{
value %= 12U;

if (++value == 13U)
{
value = 1U;
}
*this += etl::chrono::months(1);

return *this;
}
Expand All @@ -105,7 +106,7 @@ namespace etl
{
const etl::chrono::month temp = *this;

this->operator++();
*this += etl::chrono::months(1);

return temp;
}
Expand All @@ -115,19 +116,7 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator --() ETL_NOEXCEPT
{
if (value > 0U)
{
value = ((value - 1U) % 12U) + 1U;
}

if (value == 1U)
{
value = 12U;
}
else
{
--value;
}
*this -= etl::chrono::months(1);

return *this;
}
Expand All @@ -139,7 +128,7 @@ namespace etl
{
etl::chrono::month temp = *this;

this->operator--();
*this -= etl::chrono::months(1);

return temp;
}
Expand All @@ -149,7 +138,7 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator +=(const etl::chrono::months& ms) ETL_NOEXCEPT
{
value += static_cast<unsigned char>(ms.count());
*this = *this + ms;

return *this;
}
Expand All @@ -159,7 +148,7 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month& operator -=(const etl::chrono::months& ms) ETL_NOEXCEPT
{
value -= static_cast<unsigned char>(ms.count());
*this = *this - ms;

return *this;
}
Expand Down Expand Up @@ -207,11 +196,24 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month operator +(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT
{
etl::chrono::month result(m);
unsigned int value = static_cast<unsigned int>(m);

value = value % 12U;

if (value == 0U)
{
value = 12U;
}

result += ms;
int delta = ms.count() % 12;

return result;
// Adjust to allow a limited +-11 month delta
value += 11U;
value += delta;
value %= 12U;
++value;

return etl::chrono::month(value);
}

//***********************************************************************
Expand All @@ -220,11 +222,7 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month operator +(const etl::chrono::months& ms, const etl::chrono::month& m) ETL_NOEXCEPT
{
etl::chrono::month result(m);

result += ms;

return result;
return m + ms;
}

//***********************************************************************
Expand All @@ -233,21 +231,28 @@ namespace etl
//***********************************************************************
ETL_CONSTEXPR etl::chrono::month operator -(const etl::chrono::month& m, const etl::chrono::months& ms) ETL_NOEXCEPT
{
etl::chrono::month result(m);

result -= ms;

return result;
return m + etl::chrono::months(-ms.count());
}

//***********************************************************************
/// Subtract etl::chrono::month from etl::chrono::month
///\return etl::chrono::months
//***********************************************************************
ETL_CONSTEXPR etl::chrono::months operator -(const etl::chrono::month& d1, const etl::chrono::month& d2) ETL_NOEXCEPT
ETL_CONSTEXPR etl::chrono::months operator -(const etl::chrono::month& m1, const etl::chrono::month& m2) ETL_NOEXCEPT
{
return etl::chrono::months(static_cast<int>(static_cast<unsigned>(d1)) -
static_cast<int>(static_cast<unsigned>(d2)));
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();
}

//***********************************************************************
Expand Down
2 changes: 1 addition & 1 deletion include/etl/private/chrono/year.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ namespace etl
//***********************************************************************
constexpr etl::chrono::year operator ""_y(unsigned long long y) noexcept
{
return etl::chrono::year(y);
return etl::chrono::year(static_cast<int16_t>(y));
}
}
}
Expand Down

0 comments on commit 5c6d32d

Please sign in to comment.