In libc++, this is the way we find days from weekday y (rhs) to weekday x (lhs) in future direction.
constexpr days operator-(const weekday& __lhs, const weekday& __rhs) noexcept
{
const int __wdu = __lhs.c_encoding() - __rhs.c_encoding();
const int __wk = (__wdu >= 0 ? __wdu : __wdu-6) / 7;
return days{__wdu - __wk * 7};
}
The same as in date.h library
CONSTCD14
inline
days
operator-(const weekday& x, const weekday& y) NOEXCEPT
{
auto const wdu = x.wd_ - y.wd_;
auto const wk = (wdu >= 0 ? wdu : wdu-6) / 7;
return days{wdu - wk * 7};
}
I wonder why we can't do just only this?
return days{x.wd_ - y.wd_ >= 0 ? x.wd_ - y.wd_ : x.wd_ - y.wd_ + 7};
The rationale is that I wanted to offer some support for weekday
s that are !ok()
. For example:
auto x = weekday{13} - weekday{5}; // x == 1d
I.e., for some range greater than [0, 6], the arithmetic is modulo 7. This was an experiment, that I suppose is still in progress. This support did not make it into the C++20 spec.