Back to home page

EIC code displayed by LXR

 
 

    


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

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 // For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
0011 
0012 #ifndef _LIBCPP___CXX03___CHRONO_TIME_ZONE_H
0013 #define _LIBCPP___CXX03___CHRONO_TIME_ZONE_H
0014 
0015 #include <__cxx03/version>
0016 // Enable the contents of the header only when libc++ was built with experimental features enabled.
0017 #if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
0018 
0019 #  include <__cxx03/__chrono/calendar.h>
0020 #  include <__cxx03/__chrono/duration.h>
0021 #  include <__cxx03/__chrono/exception.h>
0022 #  include <__cxx03/__chrono/local_info.h>
0023 #  include <__cxx03/__chrono/sys_info.h>
0024 #  include <__cxx03/__chrono/system_clock.h>
0025 #  include <__cxx03/__compare/strong_order.h>
0026 #  include <__cxx03/__config>
0027 #  include <__cxx03/__memory/unique_ptr.h>
0028 #  include <__cxx03/__type_traits/common_type.h>
0029 #  include <__cxx03/string_view>
0030 
0031 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0032 #    pragma GCC system_header
0033 #  endif
0034 
0035 _LIBCPP_PUSH_MACROS
0036 #  include <__cxx03/__undef_macros>
0037 
0038 _LIBCPP_BEGIN_NAMESPACE_STD
0039 
0040 #  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) &&   \
0041       !defined(_LIBCPP_HAS_NO_LOCALIZATION)
0042 
0043 namespace chrono {
0044 
0045 enum class choose { earliest, latest };
0046 
0047 class _LIBCPP_AVAILABILITY_TZDB time_zone {
0048   _LIBCPP_HIDE_FROM_ABI time_zone() = default;
0049 
0050 public:
0051   class __impl; // public so it can be used by make_unique.
0052 
0053   // The "constructor".
0054   //
0055   // The default constructor is private to avoid the constructor from being
0056   // part of the ABI. Instead use an __ugly_named function as an ABI interface,
0057   // since that gives us the ability to change it in the future.
0058   [[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI static time_zone __create(unique_ptr<__impl>&& __p);
0059 
0060   _LIBCPP_EXPORTED_FROM_ABI ~time_zone();
0061 
0062   _LIBCPP_HIDE_FROM_ABI time_zone(time_zone&&)            = default;
0063   _LIBCPP_HIDE_FROM_ABI time_zone& operator=(time_zone&&) = default;
0064 
0065   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI string_view name() const noexcept { return __name(); }
0066 
0067   template <class _Duration>
0068   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_info get_info(const sys_time<_Duration>& __time) const {
0069     return __get_info(chrono::time_point_cast<seconds>(__time));
0070   }
0071 
0072   template <class _Duration>
0073   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI local_info get_info(const local_time<_Duration>& __time) const {
0074     return __get_info(chrono::time_point_cast<seconds>(__time));
0075   }
0076 
0077   // We don't apply nodiscard here since this function throws on many inputs,
0078   // so it could be used as a validation.
0079   template <class _Duration>
0080   _LIBCPP_HIDE_FROM_ABI sys_time<common_type_t<_Duration, seconds>> to_sys(const local_time<_Duration>& __time) const {
0081     local_info __info = get_info(__time);
0082     switch (__info.result) {
0083     case local_info::unique:
0084       return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.first.offset};
0085 
0086     case local_info::nonexistent:
0087       chrono::__throw_nonexistent_local_time(__time, __info);
0088 
0089     case local_info::ambiguous:
0090       chrono::__throw_ambiguous_local_time(__time, __info);
0091     }
0092 
0093     // TODO TZDB The Standard does not specify anything in these cases.
0094     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0095         __info.result != -1, "cannot convert the local time; it would be before the minimum system clock value");
0096     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0097         __info.result != -2, "cannot convert the local time; it would be after the maximum system clock value");
0098 
0099     return {};
0100   }
0101 
0102   template <class _Duration>
0103   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<common_type_t<_Duration, seconds>>
0104   to_sys(const local_time<_Duration>& __time, choose __z) const {
0105     local_info __info = get_info(__time);
0106     switch (__info.result) {
0107     case local_info::unique:
0108     case local_info::nonexistent: // first and second are the same
0109       return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.first.offset};
0110 
0111     case local_info::ambiguous:
0112       switch (__z) {
0113       case choose::earliest:
0114         return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.first.offset};
0115 
0116       case choose::latest:
0117         return sys_time<common_type_t<_Duration, seconds>>{__time.time_since_epoch() - __info.second.offset};
0118 
0119         // Note a value out of bounds is not specified.
0120       }
0121     }
0122 
0123     // TODO TZDB The standard does not specify anything in these cases.
0124     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0125         __info.result != -1, "cannot convert the local time; it would be before the minimum system clock value");
0126     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0127         __info.result != -2, "cannot convert the local time; it would be after the maximum system clock value");
0128 
0129     return {};
0130   }
0131 
0132   template <class _Duration>
0133   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI local_time<common_type_t<_Duration, seconds>>
0134   to_local(const sys_time<_Duration>& __time) const {
0135     using _Dp = common_type_t<_Duration, seconds>;
0136 
0137     sys_info __info = get_info(__time);
0138 
0139     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0140         __info.offset >= chrono::seconds{0} || __time.time_since_epoch() >= _Dp::min() - __info.offset,
0141         "cannot convert the system time; it would be before the minimum local clock value");
0142 
0143     _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
0144         __info.offset <= chrono::seconds{0} || __time.time_since_epoch() <= _Dp::max() - __info.offset,
0145         "cannot convert the system time; it would be after the maximum local clock value");
0146 
0147     return local_time<_Dp>{__time.time_since_epoch() + __info.offset};
0148   }
0149 
0150   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI const __impl& __implementation() const noexcept { return *__impl_; }
0151 
0152 private:
0153   [[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI string_view __name() const noexcept;
0154 
0155   [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI sys_info __get_info(sys_seconds __time) const;
0156   [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI local_info __get_info(local_seconds __time) const;
0157 
0158   unique_ptr<__impl> __impl_;
0159 };
0160 
0161 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI inline bool
0162 operator==(const time_zone& __x, const time_zone& __y) noexcept {
0163   return __x.name() == __y.name();
0164 }
0165 
0166 [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_HIDE_FROM_ABI inline strong_ordering
0167 operator<=>(const time_zone& __x, const time_zone& __y) noexcept {
0168   return __x.name() <=> __y.name();
0169 }
0170 
0171 } // namespace chrono
0172 
0173 #  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
0174          // && !defined(_LIBCPP_HAS_NO_LOCALIZATION)
0175 
0176 _LIBCPP_END_NAMESPACE_STD
0177 
0178 _LIBCPP_POP_MACROS
0179 
0180 #endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
0181 
0182 #endif // _LIBCPP___CXX03___CHRONO_TIME_ZONE_H