Path: blob/main/contrib/llvm-project/libcxx/include/__chrono/year_month_weekday.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H10#define _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H1112#include <__chrono/calendar.h>13#include <__chrono/day.h>14#include <__chrono/duration.h>15#include <__chrono/month.h>16#include <__chrono/month_weekday.h>17#include <__chrono/system_clock.h>18#include <__chrono/time_point.h>19#include <__chrono/weekday.h>20#include <__chrono/year.h>21#include <__chrono/year_month.h>22#include <__chrono/year_month_day.h>23#include <__config>2425#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif2829#if _LIBCPP_STD_VER >= 203031_LIBCPP_BEGIN_NAMESPACE_STD3233namespace chrono {3435class year_month_weekday {36chrono::year __y_;37chrono::month __m_;38chrono::weekday_indexed __wdi_;3940public:41year_month_weekday() = default;42_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday(43const chrono::year& __yval, const chrono::month& __mval, const chrono::weekday_indexed& __wdival) noexcept44: __y_{__yval}, __m_{__mval}, __wdi_{__wdival} {}45_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday(const sys_days& __sysd) noexcept46: year_month_weekday(__from_days(__sysd.time_since_epoch())) {}47_LIBCPP_HIDE_FROM_ABI inline explicit constexpr year_month_weekday(const local_days& __locd) noexcept48: year_month_weekday(__from_days(__locd.time_since_epoch())) {}49_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator+=(const months&) noexcept;50_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator-=(const months&) noexcept;51_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator+=(const years&) noexcept;52_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday& operator-=(const years&) noexcept;5354_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }55_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }56_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept { return __wdi_.weekday(); }57_LIBCPP_HIDE_FROM_ABI inline constexpr unsigned index() const noexcept { return __wdi_.index(); }58_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday_indexed weekday_indexed() const noexcept { return __wdi_; }5960_LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }61_LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {62return local_days{__to_days()};63}64_LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept {65if (!__y_.ok() || !__m_.ok() || !__wdi_.ok())66return false;67if (__wdi_.index() <= 4)68return true;69auto __nth_weekday_day =70__wdi_.weekday() - chrono::weekday{static_cast<sys_days>(__y_ / __m_ / 1)} + days{(__wdi_.index() - 1) * 7 + 1};71return static_cast<unsigned>(__nth_weekday_day.count()) <= static_cast<unsigned>((__y_ / __m_ / last).day());72}7374_LIBCPP_HIDE_FROM_ABI static constexpr year_month_weekday __from_days(days __d) noexcept;75_LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;76};7778_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday year_month_weekday::__from_days(days __d) noexcept {79const sys_days __sysd{__d};80const chrono::weekday __wd = chrono::weekday(__sysd);81const year_month_day __ymd = year_month_day(__sysd);82return year_month_weekday{__ymd.year(), __ymd.month(), __wd[(static_cast<unsigned>(__ymd.day()) - 1) / 7 + 1]};83}8485_LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_weekday::__to_days() const noexcept {86const sys_days __sysd = sys_days(__y_ / __m_ / 1);87return (__sysd + (__wdi_.weekday() - chrono::weekday(__sysd) + days{(__wdi_.index() - 1) * 7})).time_since_epoch();88}8990_LIBCPP_HIDE_FROM_ABI inline constexpr bool91operator==(const year_month_weekday& __lhs, const year_month_weekday& __rhs) noexcept {92return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() &&93__lhs.weekday_indexed() == __rhs.weekday_indexed();94}9596_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday97operator/(const year_month& __lhs, const weekday_indexed& __rhs) noexcept {98return year_month_weekday{__lhs.year(), __lhs.month(), __rhs};99}100101_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday102operator/(const year& __lhs, const month_weekday& __rhs) noexcept {103return year_month_weekday{__lhs, __rhs.month(), __rhs.weekday_indexed()};104}105106_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday operator/(int __lhs, const month_weekday& __rhs) noexcept {107return year(__lhs) / __rhs;108}109110_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday111operator/(const month_weekday& __lhs, const year& __rhs) noexcept {112return __rhs / __lhs;113}114115_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday operator/(const month_weekday& __lhs, int __rhs) noexcept {116return year(__rhs) / __lhs;117}118119_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday120operator+(const year_month_weekday& __lhs, const months& __rhs) noexcept {121return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_indexed();122}123124_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday125operator+(const months& __lhs, const year_month_weekday& __rhs) noexcept {126return __rhs + __lhs;127}128129_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday130operator-(const year_month_weekday& __lhs, const months& __rhs) noexcept {131return __lhs + (-__rhs);132}133134_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday135operator+(const year_month_weekday& __lhs, const years& __rhs) noexcept {136return year_month_weekday{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_indexed()};137}138139_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday140operator+(const years& __lhs, const year_month_weekday& __rhs) noexcept {141return __rhs + __lhs;142}143144_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday145operator-(const year_month_weekday& __lhs, const years& __rhs) noexcept {146return __lhs + (-__rhs);147}148149_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const months& __dm) noexcept {150*this = *this + __dm;151return *this;152}153_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const months& __dm) noexcept {154*this = *this - __dm;155return *this;156}157_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator+=(const years& __dy) noexcept {158*this = *this + __dy;159return *this;160}161_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday& year_month_weekday::operator-=(const years& __dy) noexcept {162*this = *this - __dy;163return *this;164}165166class year_month_weekday_last {167private:168chrono::year __y_;169chrono::month __m_;170chrono::weekday_last __wdl_;171172public:173_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last(174const chrono::year& __yval, const chrono::month& __mval, const chrono::weekday_last& __wdlval) noexcept175: __y_{__yval}, __m_{__mval}, __wdl_{__wdlval} {}176_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const months& __dm) noexcept;177_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const months& __dm) noexcept;178_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator+=(const years& __dy) noexcept;179_LIBCPP_HIDE_FROM_ABI constexpr year_month_weekday_last& operator-=(const years& __dy) noexcept;180181_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::year year() const noexcept { return __y_; }182_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::month month() const noexcept { return __m_; }183_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday weekday() const noexcept { return __wdl_.weekday(); }184_LIBCPP_HIDE_FROM_ABI inline constexpr chrono::weekday_last weekday_last() const noexcept { return __wdl_; }185_LIBCPP_HIDE_FROM_ABI inline constexpr operator sys_days() const noexcept { return sys_days{__to_days()}; }186_LIBCPP_HIDE_FROM_ABI inline explicit constexpr operator local_days() const noexcept {187return local_days{__to_days()};188}189_LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __y_.ok() && __m_.ok() && __wdl_.ok(); }190191_LIBCPP_HIDE_FROM_ABI constexpr days __to_days() const noexcept;192};193194_LIBCPP_HIDE_FROM_ABI inline constexpr days year_month_weekday_last::__to_days() const noexcept {195const sys_days __last = sys_days{__y_ / __m_ / last};196return (__last - (chrono::weekday{__last} - __wdl_.weekday())).time_since_epoch();197}198199_LIBCPP_HIDE_FROM_ABI inline constexpr bool200operator==(const year_month_weekday_last& __lhs, const year_month_weekday_last& __rhs) noexcept {201return __lhs.year() == __rhs.year() && __lhs.month() == __rhs.month() && __lhs.weekday_last() == __rhs.weekday_last();202}203204_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last205operator/(const year_month& __lhs, const weekday_last& __rhs) noexcept {206return year_month_weekday_last{__lhs.year(), __lhs.month(), __rhs};207}208209_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last210operator/(const year& __lhs, const month_weekday_last& __rhs) noexcept {211return year_month_weekday_last{__lhs, __rhs.month(), __rhs.weekday_last()};212}213214_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last215operator/(int __lhs, const month_weekday_last& __rhs) noexcept {216return year(__lhs) / __rhs;217}218219_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last220operator/(const month_weekday_last& __lhs, const year& __rhs) noexcept {221return __rhs / __lhs;222}223224_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last225operator/(const month_weekday_last& __lhs, int __rhs) noexcept {226return year(__rhs) / __lhs;227}228229_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last230operator+(const year_month_weekday_last& __lhs, const months& __rhs) noexcept {231return (__lhs.year() / __lhs.month() + __rhs) / __lhs.weekday_last();232}233234_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last235operator+(const months& __lhs, const year_month_weekday_last& __rhs) noexcept {236return __rhs + __lhs;237}238239_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last240operator-(const year_month_weekday_last& __lhs, const months& __rhs) noexcept {241return __lhs + (-__rhs);242}243244_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last245operator+(const year_month_weekday_last& __lhs, const years& __rhs) noexcept {246return year_month_weekday_last{__lhs.year() + __rhs, __lhs.month(), __lhs.weekday_last()};247}248249_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last250operator+(const years& __lhs, const year_month_weekday_last& __rhs) noexcept {251return __rhs + __lhs;252}253254_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last255operator-(const year_month_weekday_last& __lhs, const years& __rhs) noexcept {256return __lhs + (-__rhs);257}258259_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&260year_month_weekday_last::operator+=(const months& __dm) noexcept {261*this = *this + __dm;262return *this;263}264_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&265year_month_weekday_last::operator-=(const months& __dm) noexcept {266*this = *this - __dm;267return *this;268}269_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&270year_month_weekday_last::operator+=(const years& __dy) noexcept {271*this = *this + __dy;272return *this;273}274_LIBCPP_HIDE_FROM_ABI inline constexpr year_month_weekday_last&275year_month_weekday_last::operator-=(const years& __dy) noexcept {276*this = *this - __dy;277return *this;278}279280} // namespace chrono281282_LIBCPP_END_NAMESPACE_STD283284#endif // _LIBCPP_STD_VER >= 20285286#endif // _LIBCPP___CHRONO_YEAR_MONTH_WEEKDAY_H287288289