master
  1// -*- C++ -*-
  2//===----------------------------------------------------------------------===//
  3//
  4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5// See https://llvm.org/LICENSE.txt for license information.
  6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7//
  8//===----------------------------------------------------------------------===//
  9
 10#ifndef _LIBCPP___CHRONO_YEAR_H
 11#define _LIBCPP___CHRONO_YEAR_H
 12
 13#include <__chrono/duration.h>
 14#include <__compare/ordering.h>
 15#include <__config>
 16#include <limits>
 17
 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 19#  pragma GCC system_header
 20#endif
 21
 22_LIBCPP_PUSH_MACROS
 23#include <__undef_macros>
 24
 25#if _LIBCPP_STD_VER >= 20
 26
 27_LIBCPP_BEGIN_NAMESPACE_STD
 28
 29namespace chrono {
 30
 31class year {
 32private:
 33  short __y_;
 34
 35public:
 36  year() = default;
 37  _LIBCPP_HIDE_FROM_ABI explicit inline constexpr year(int __val) noexcept : __y_(static_cast<short>(__val)) {}
 38
 39  _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator++() noexcept {
 40    ++__y_;
 41    return *this;
 42  }
 43  _LIBCPP_HIDE_FROM_ABI inline constexpr year operator++(int) noexcept {
 44    year __tmp = *this;
 45    ++(*this);
 46    return __tmp;
 47  }
 48  _LIBCPP_HIDE_FROM_ABI inline constexpr year& operator--() noexcept {
 49    --__y_;
 50    return *this;
 51  }
 52  _LIBCPP_HIDE_FROM_ABI inline constexpr year operator--(int) noexcept {
 53    year __tmp = *this;
 54    --(*this);
 55    return __tmp;
 56  }
 57  _LIBCPP_HIDE_FROM_ABI constexpr year& operator+=(const years& __dy) noexcept;
 58  _LIBCPP_HIDE_FROM_ABI constexpr year& operator-=(const years& __dy) noexcept;
 59  _LIBCPP_HIDE_FROM_ABI inline constexpr year operator+() const noexcept { return *this; }
 60  _LIBCPP_HIDE_FROM_ABI inline constexpr year operator-() const noexcept { return year{-__y_}; }
 61
 62  _LIBCPP_HIDE_FROM_ABI inline constexpr bool is_leap() const noexcept {
 63    return __y_ % 4 == 0 && (__y_ % 100 != 0 || __y_ % 400 == 0);
 64  }
 65  _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator int() const noexcept { return __y_; }
 66  _LIBCPP_HIDE_FROM_ABI constexpr bool ok() const noexcept;
 67  _LIBCPP_HIDE_FROM_ABI static inline constexpr year min() noexcept { return year{-32767}; }
 68  _LIBCPP_HIDE_FROM_ABI static inline constexpr year max() noexcept { return year{32767}; }
 69};
 70
 71_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const year& __lhs, const year& __rhs) noexcept {
 72  return static_cast<int>(__lhs) == static_cast<int>(__rhs);
 73}
 74
 75_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const year& __lhs, const year& __rhs) noexcept {
 76  return static_cast<int>(__lhs) <=> static_cast<int>(__rhs);
 77}
 78
 79_LIBCPP_HIDE_FROM_ABI inline constexpr year operator+(const year& __lhs, const years& __rhs) noexcept {
 80  return year(static_cast<int>(__lhs) + __rhs.count());
 81}
 82
 83_LIBCPP_HIDE_FROM_ABI inline constexpr year operator+(const years& __lhs, const year& __rhs) noexcept {
 84  return __rhs + __lhs;
 85}
 86
 87_LIBCPP_HIDE_FROM_ABI inline constexpr year operator-(const year& __lhs, const years& __rhs) noexcept {
 88  return __lhs + -__rhs;
 89}
 90
 91_LIBCPP_HIDE_FROM_ABI inline constexpr years operator-(const year& __lhs, const year& __rhs) noexcept {
 92  return years{static_cast<int>(__lhs) - static_cast<int>(__rhs)};
 93}
 94
 95_LIBCPP_HIDE_FROM_ABI inline constexpr year& year::operator+=(const years& __dy) noexcept {
 96  *this = *this + __dy;
 97  return *this;
 98}
 99
100_LIBCPP_HIDE_FROM_ABI inline constexpr year& year::operator-=(const years& __dy) noexcept {
101  *this = *this - __dy;
102  return *this;
103}
104
105_LIBCPP_HIDE_FROM_ABI constexpr bool year::ok() const noexcept {
106  static_assert(static_cast<int>(std::numeric_limits<decltype(__y_)>::max()) == static_cast<int>(max()));
107  return static_cast<int>(min()) <= __y_;
108}
109
110} // namespace chrono
111
112_LIBCPP_END_NAMESPACE_STD
113
114#endif // _LIBCPP_STD_VER >= 20
115
116_LIBCPP_POP_MACROS
117
118#endif // _LIBCPP___CHRONO_YEAR_H