Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:15

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CHRONO_WEEKDAY_H
0011 #define _LIBCPP___CHRONO_WEEKDAY_H
0012 
0013 #include <__chrono/calendar.h>
0014 #include <__chrono/duration.h>
0015 #include <__chrono/system_clock.h>
0016 #include <__chrono/time_point.h>
0017 #include <__config>
0018 
0019 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0020 #  pragma GCC system_header
0021 #endif
0022 
0023 #if _LIBCPP_STD_VER >= 20
0024 
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026 
0027 namespace chrono {
0028 
0029 class weekday_indexed;
0030 class weekday_last;
0031 
0032 class weekday {
0033 private:
0034   unsigned char __wd_;
0035   _LIBCPP_HIDE_FROM_ABI static constexpr unsigned char __weekday_from_days(int __days) noexcept;
0036 
0037 public:
0038   weekday() = default;
0039   _LIBCPP_HIDE_FROM_ABI inline explicit constexpr weekday(unsigned __val) noexcept
0040       : __wd_(static_cast<unsigned char>(__val == 7 ? 0 : __val)) {}
0041   _LIBCPP_HIDE_FROM_ABI inline constexpr weekday(const sys_days& __sysd) noexcept
0042       : __wd_(__weekday_from_days(__sysd.time_since_epoch().count())) {}
0043   _LIBCPP_HIDE_FROM_ABI inline explicit constexpr weekday(const local_days& __locd) noexcept
0044       : __wd_(__weekday_from_days(__locd.time_since_epoch().count())) {}
0045 
0046   _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& operator++() noexcept {
0047     __wd_ = (__wd_ == 6 ? 0 : __wd_ + 1);
0048     return *this;
0049   }
0050   _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator++(int) noexcept {
0051     weekday __tmp = *this;
0052     ++(*this);
0053     return __tmp;
0054   }
0055   _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& operator--() noexcept {
0056     __wd_ = (__wd_ == 0 ? 6 : __wd_ - 1);
0057     return *this;
0058   }
0059   _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator--(int) noexcept {
0060     weekday __tmp = *this;
0061     --(*this);
0062     return __tmp;
0063   }
0064   _LIBCPP_HIDE_FROM_ABI constexpr weekday& operator+=(const days& __dd) noexcept;
0065   _LIBCPP_HIDE_FROM_ABI constexpr weekday& operator-=(const days& __dd) noexcept;
0066   _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned c_encoding() const noexcept { return __wd_; }
0067   _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned iso_encoding() const noexcept { return __wd_ == 0u ? 7 : __wd_; }
0068   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __wd_ <= 6; }
0069   _LIBCPP_HIDE_FROM_ABI constexpr weekday_indexed operator[](unsigned __index) const noexcept;
0070   _LIBCPP_HIDE_FROM_ABI constexpr weekday_last operator[](last_spec) const noexcept;
0071 };
0072 
0073 // https://howardhinnant.github.io/date_algorithms.html#weekday_from_days
0074 _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned char weekday::__weekday_from_days(int __days) noexcept {
0075   return static_cast<unsigned char>(static_cast<unsigned>(__days >= -4 ? (__days + 4) % 7 : (__days + 5) % 7 + 6));
0076 }
0077 
0078 _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const weekday& __lhs, const weekday& __rhs) noexcept {
0079   return __lhs.c_encoding() == __rhs.c_encoding();
0080 }
0081 
0082 _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator+(const weekday& __lhs, const days& __rhs) noexcept {
0083   auto const __mu = static_cast<long long>(__lhs.c_encoding()) + __rhs.count();
0084   auto const __yr = (__mu >= 0 ? __mu : __mu - 6) / 7;
0085   return weekday{static_cast<unsigned>(__mu - __yr * 7)};
0086 }
0087 
0088 _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator+(const days& __lhs, const weekday& __rhs) noexcept {
0089   return __rhs + __lhs;
0090 }
0091 
0092 _LIBCPP_HIDE_FROM_ABI inline constexpr weekday operator-(const weekday& __lhs, const days& __rhs) noexcept {
0093   return __lhs + -__rhs;
0094 }
0095 
0096 _LIBCPP_HIDE_FROM_ABI inline constexpr days operator-(const weekday& __lhs, const weekday& __rhs) noexcept {
0097   const int __wdu = __lhs.c_encoding() - __rhs.c_encoding();
0098   const int __wk  = (__wdu >= 0 ? __wdu : __wdu - 6) / 7;
0099   return days{__wdu - __wk * 7};
0100 }
0101 
0102 _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& weekday::operator+=(const days& __dd) noexcept {
0103   *this = *this + __dd;
0104   return *this;
0105 }
0106 
0107 _LIBCPP_HIDE_FROM_ABI inline constexpr weekday& weekday::operator-=(const days& __dd) noexcept {
0108   *this = *this - __dd;
0109   return *this;
0110 }
0111 
0112 class weekday_indexed {
0113 private:
0114   chrono::weekday __wd_;
0115   unsigned char __idx_;
0116 
0117 public:
0118   weekday_indexed() = default;
0119   _LIBCPP_HIDE_FROM_ABI inline constexpr weekday_indexed(const chrono::weekday& __wdval, unsigned __idxval) noexcept
0120       : __wd_{__wdval}, __idx_(__idxval) {}
0121   _LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept { return __wd_; }
0122   _LIBCPP_HIDE_FROM_ABI inline constexpr unsigned index() const noexcept { return __idx_; }
0123   _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __wd_.ok() && __idx_ >= 1 && __idx_ <= 5; }
0124 };
0125 
0126 _LIBCPP_HIDE_FROM_ABI inline constexpr bool
0127 operator==(const weekday_indexed& __lhs, const weekday_indexed& __rhs) noexcept {
0128   return __lhs.weekday() == __rhs.weekday() && __lhs.index() == __rhs.index();
0129 }
0130 
0131 class weekday_last {
0132 private:
0133   chrono::weekday __wd_;
0134 
0135 public:
0136   _LIBCPP_HIDE_FROM_ABI explicit constexpr weekday_last(const chrono::weekday& __val) noexcept : __wd_{__val} {}
0137   _LIBCPP_HIDE_FROM_ABI constexpr chrono::weekday weekday() const noexcept { return __wd_; }
0138   _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept { return __wd_.ok(); }
0139 };
0140 
0141 _LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const weekday_last& __lhs, const weekday_last& __rhs) noexcept {
0142   return __lhs.weekday() == __rhs.weekday();
0143 }
0144 
0145 _LIBCPP_HIDE_FROM_ABI inline constexpr weekday_indexed weekday::operator[](unsigned __index) const noexcept {
0146   return weekday_indexed{*this, __index};
0147 }
0148 
0149 _LIBCPP_HIDE_FROM_ABI inline constexpr weekday_last weekday::operator[](last_spec) const noexcept {
0150   return weekday_last{*this};
0151 }
0152 
0153 inline constexpr weekday Sunday{0};
0154 inline constexpr weekday Monday{1};
0155 inline constexpr weekday Tuesday{2};
0156 inline constexpr weekday Wednesday{3};
0157 inline constexpr weekday Thursday{4};
0158 inline constexpr weekday Friday{5};
0159 inline constexpr weekday Saturday{6};
0160 
0161 } // namespace chrono
0162 
0163 _LIBCPP_END_NAMESPACE_STD
0164 
0165 #endif // _LIBCPP_STD_VER >= 20
0166 
0167 #endif // _LIBCPP___CHRONO_WEEKDAY_H