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 20, 2023
2 parents 501a73a + bc6da1e commit b6e7506
Show file tree
Hide file tree
Showing 11 changed files with 462 additions and 137 deletions.
11 changes: 10 additions & 1 deletion include/etl/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ 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
46 changes: 19 additions & 27 deletions include/etl/private/chrono/day.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_CHRONO_DAY_INCLUDED
#define ETL_CHRONO_DAY_INCLUDED

#include "../../platform.h"
#include "../../hash.h"

#include "duration.h"

#include <stdint.h>
#ifndef ETL_IN_CHRONO_H
#error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H
#endif

namespace etl
{
Expand All @@ -52,31 +46,31 @@ namespace etl
//***********************************************************************
/// Default constructor
//***********************************************************************
ETL_CONSTEXPR day()
ETL_CONSTEXPR day() ETL_NOEXCEPT
: value(0)
{
}

//***********************************************************************
/// Construct from unsigned
//***********************************************************************
ETL_CONSTEXPR explicit day(unsigned value_)
ETL_CONSTEXPR explicit day(unsigned value_) ETL_NOEXCEPT
: value(value_)
{
}

//***********************************************************************
/// Copy constructor
//***********************************************************************
ETL_CONSTEXPR day(const etl::chrono::day& other)
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_CONSTEXPR etl::chrono::day& operator =(const etl::chrono::day& rhs) ETL_NOEXCEPT
{
value = rhs.value;

Expand Down Expand Up @@ -148,7 +142,7 @@ namespace etl
//***********************************************************************
/// Returns <b>true</b> if the day is within the valid 1 to 31 range
//***********************************************************************
ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
ETL_NODISCARD ETL_CONSTEXPR bool ok() const ETL_NOEXCEPT
{
return (value >= 1U) && (value <= 31U);
}
Expand Down Expand Up @@ -185,15 +179,15 @@ namespace etl
//***********************************************************************
/// Equality operator
//***********************************************************************
ETL_CONSTEXPR bool operator ==(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
ETL_NODISCARD 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
ETL_NODISCARD ETL_CONSTEXPR bool operator !=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
{
return !(d1 == d2);
}
Expand All @@ -209,23 +203,23 @@ namespace etl
//***********************************************************************
/// Less-than-or-equal operator
//***********************************************************************
ETL_CONSTEXPR bool operator <=(const etl::chrono::day& d1, const etl::chrono::day& d2) ETL_NOEXCEPT
ETL_NODISCARD 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
ETL_NODISCARD 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
ETL_NODISCARD 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));
}
Expand All @@ -234,7 +228,7 @@ namespace etl
/// Spaceship operator
//***********************************************************************
#if ETL_USING_CPP20
constexpr auto operator <=>(const etl::chrono::day& d1, const etl::chrono::day& d2) noexcept
[[nodiscard]] constexpr auto operator <=>(const etl::chrono::day& d1, const etl::chrono::day& d2) noexcept
{
return (static_cast<unsigned>(d1) <=> static_cast<unsigned>(d2));
}
Expand All @@ -244,7 +238,7 @@ namespace etl
/// 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_NODISCARD ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
{
etl::chrono::day result(d);

Expand All @@ -257,7 +251,7 @@ namespace etl
/// 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_NODISCARD ETL_CONSTEXPR etl::chrono::day operator +(const etl::chrono::days& ds, const etl::chrono::day& d) ETL_NOEXCEPT
{
etl::chrono::day result(d);

Expand All @@ -270,7 +264,7 @@ namespace etl
/// 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_NODISCARD ETL_CONSTEXPR etl::chrono::day operator -(const etl::chrono::day& d, const etl::chrono::days& ds) ETL_NOEXCEPT
{
etl::chrono::day result(d);

Expand All @@ -283,7 +277,7 @@ namespace etl
/// 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
ETL_NODISCARD 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)));
Expand Down Expand Up @@ -319,7 +313,7 @@ namespace etl
//***********************************************************************
/// Literal for days
//***********************************************************************
ETL_CONSTEXPR etl::chrono::day operator ""_day(unsigned long long d) ETL_NOEXCEPT
constexpr etl::chrono::day operator ""_day(unsigned long long d) noexcept
{
return etl::chrono::day(static_cast<unsigned>(d));
}
Expand All @@ -328,5 +322,3 @@ namespace etl
}
#endif
#endif

#endif
12 changes: 5 additions & 7 deletions include/etl/private/chrono/duration.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_CHRONO_DURATION_INCLUDED
#define ETL_CHRONO_DURATION_INCLUDED
#ifndef ETL_IN_CHRONO_H
#error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H
#endif

#include "../../platform.h"
#include "../../ratio.h"
#include "../../static_assert.h"
#include "../../limits.h"
Expand Down Expand Up @@ -94,7 +94,7 @@ namespace etl
}

//***********************************************************************
ETL_NODISCARD ETL_CONSTEXPR TValue count() const
ETL_NODISCARD ETL_CONSTEXPR TValue count() const ETL_NOEXCEPT
{
return value;
}
Expand Down Expand Up @@ -129,11 +129,9 @@ namespace etl
/// duration_cast
//***********************************************************************
template <typename TToDuration, typename TValue, typename TPeriod>
ETL_CONSTEXPR TToDuration duration_cast(const etl::chrono::duration<TValue, TPeriod>& d)
ETL_CONSTEXPR TToDuration duration_cast(const etl::chrono::duration<TValue, TPeriod>& d) ETL_NOEXCEPT
{
return TToDuration();
}
}
}

#endif
52 changes: 52 additions & 0 deletions include/etl/private/chrono/last_spec.h
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading

0 comments on commit b6e7506

Please sign in to comment.