day.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef _LIBCPP___CHRONO_DAY_H
  10. #define _LIBCPP___CHRONO_DAY_H
  11. #include <__chrono/duration.h>
  12. #include <__config>
  13. #include <compare>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. #if _LIBCPP_STD_VER > 17
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. namespace chrono
  20. {
  21. class day {
  22. private:
  23. unsigned char __d_;
  24. public:
  25. _LIBCPP_HIDE_FROM_ABI day() = default;
  26. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr day(unsigned __val) noexcept : __d_(static_cast<unsigned char>(__val)) {}
  27. _LIBCPP_HIDE_FROM_ABI inline constexpr day& operator++() noexcept { ++__d_; return *this; }
  28. _LIBCPP_HIDE_FROM_ABI inline constexpr day operator++(int) noexcept { day __tmp = *this; ++(*this); return __tmp; }
  29. _LIBCPP_HIDE_FROM_ABI inline constexpr day& operator--() noexcept { --__d_; return *this; }
  30. _LIBCPP_HIDE_FROM_ABI inline constexpr day operator--(int) noexcept { day __tmp = *this; --(*this); return __tmp; }
  31. _LIBCPP_HIDE_FROM_ABI constexpr day& operator+=(const days& __dd) noexcept;
  32. _LIBCPP_HIDE_FROM_ABI constexpr day& operator-=(const days& __dd) noexcept;
  33. _LIBCPP_HIDE_FROM_ABI explicit inline constexpr operator unsigned() const noexcept { return __d_; }
  34. _LIBCPP_HIDE_FROM_ABI inline constexpr bool ok() const noexcept { return __d_ >= 1 && __d_ <= 31; }
  35. };
  36. _LIBCPP_HIDE_FROM_ABI inline constexpr
  37. bool operator==(const day& __lhs, const day& __rhs) noexcept
  38. { return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs); }
  39. _LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const day& __lhs, const day& __rhs) noexcept {
  40. return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
  41. }
  42. _LIBCPP_HIDE_FROM_ABI inline constexpr
  43. day operator+ (const day& __lhs, const days& __rhs) noexcept
  44. { return day(static_cast<unsigned>(__lhs) + __rhs.count()); }
  45. _LIBCPP_HIDE_FROM_ABI inline constexpr
  46. day operator+ (const days& __lhs, const day& __rhs) noexcept
  47. { return __rhs + __lhs; }
  48. _LIBCPP_HIDE_FROM_ABI inline constexpr
  49. day operator- (const day& __lhs, const days& __rhs) noexcept
  50. { return __lhs + -__rhs; }
  51. _LIBCPP_HIDE_FROM_ABI inline constexpr
  52. days operator-(const day& __lhs, const day& __rhs) noexcept
  53. { return days(static_cast<int>(static_cast<unsigned>(__lhs)) -
  54. static_cast<int>(static_cast<unsigned>(__rhs))); }
  55. _LIBCPP_HIDE_FROM_ABI inline constexpr
  56. day& day::operator+=(const days& __dd) noexcept
  57. { *this = *this + __dd; return *this; }
  58. _LIBCPP_HIDE_FROM_ABI inline constexpr
  59. day& day::operator-=(const days& __dd) noexcept
  60. { *this = *this - __dd; return *this; }
  61. } // namespace chrono
  62. _LIBCPP_END_NAMESPACE_STD
  63. #endif // _LIBCPP_STD_VER > 17
  64. #endif // _LIBCPP___CHRONO_DAY_H