Skip to content

Commit

Permalink
added comparisson operators for interval
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthos committed Nov 17, 2023
1 parent 02c0275 commit 62284fc
Showing 1 changed file with 35 additions and 44 deletions.
79 changes: 35 additions & 44 deletions src/dtcalendar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,13 @@ int sgn(DType val) noexcept {
return (DType(0) <= val) - (val < DType(0));
}

/** A utility struct to hold possibly negative datetime intervals
*
* This struct is meant to act as an intermediate/buffer state between
* a datetime difference and a datetime interval. A difference could be
* negative, but an interval can only be positive. Hence, this class only
* has the purpose of acting as an intermediate state, so that it can
* 'normalize' the behaviour between a difference and an interval.
*/
// #if __cplusplus >= 202002L
// template <gconcepts::is_sec_dt S>
// #else
// template <class S, typename = std::enable_if_t<S::is_of_sec_type>>
// #endif
// struct RawDatetimeDifference {
// typedef typename S::underlying_type SecIntType;
// typedef modified_julian_day::underlying_type DaysIntType;
//
// /** seconds of day (in interval), always positive */
// SecIntType msecs;
// /** days (in interval),can be negative */
// DaysIntType mdays;
//
// explicit RawDatetimeDifference(DaysIntType days, SecIntType sec) noexcept
// : mdays(days), msecs(sec) {
// normalize();
// }
//
// void normalize() noexcept {
// /* number of whole days in seconds (always positive) */
// DaysIntType more = std::copysign(1, msecs) / S::max_in_day();
// /* add to current days, with the right sign */
// mdays += std::copysign(msecs, more);
// /* leftover seconds (positive) */
// SecIntType s = std::copysign(1, msecs) - more * S::max_in_day();
// /* if initial seconds were negative, adjust */
// mdays -= 1 * (msecs < 0);
// msecs = (S::max_in_day() - s) * (msecs < 0) + s * (msecs >= 0);
// #ifdef DEBUG
// assert(mdays >= 0 && msecs >= 0);
// #endif
// }
// }; /* RawDatetimeInterval */

} /* namespace core*/

enum class DateTimeDifferenceType { FractionalYears, FractionalDays, FractionalSeconds };
enum class DateTimeDifferenceType {
FractionalYears,
FractionalDays,
FractionalSeconds
};

/** @brief A generic, templatized class to hold a datetime period/interval.
*
Expand Down Expand Up @@ -223,6 +184,36 @@ class datetime_interval {
}
}

/** Overload equality operator. */
constexpr bool operator==(const datetime_interval &d) const noexcept {
return m_days == d.m_days && m_secs == d.m_secs;
}

/** Overload in-equality operator. */
constexpr bool operator!=(const datetime_interval &d) const noexcept {
return !(this->operator==(d));
}

/** Overload ">" operator. */
constexpr bool operator>(const datetime_interval &d) const noexcept {
return m_days > d.m_days || (m_days == d.m_days && m_secs > d.m_secs);
}

/** Overload ">=" operator. */
constexpr bool operator>=(const datetime_interval &d) const noexcept {
return m_days > d.m_days || (m_days == d.m_days && m_secs >= d.m_secs);
}

/** Overload "<" operator. */
constexpr bool operator<(const datetime_interval &d) const noexcept {
return m_days < d.m_days || (m_days == d.m_days && m_secs < d.m_secs);
}

/** Overload "<=" operator. */
constexpr bool operator<=(const datetime_interval &d) const noexcept {
return m_days < d.m_days || (m_days == d.m_days && m_secs <= d.m_secs);
}

private:
/** number of whole days in interval */
DaysIntType m_days;
Expand Down

0 comments on commit 62284fc

Please sign in to comment.