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_ZONED_TIME_H
0013 #define _LIBCPP___CXX03___CHRONO_ZONED_TIME_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/sys_info.h>
0022 #  include <__cxx03/__chrono/system_clock.h>
0023 #  include <__cxx03/__chrono/time_zone.h>
0024 #  include <__cxx03/__chrono/tzdb_list.h>
0025 #  include <__cxx03/__config>
0026 #  include <__cxx03/__fwd/string_view.h>
0027 #  include <__cxx03/__type_traits/common_type.h>
0028 #  include <__cxx03/__type_traits/conditional.h>
0029 #  include <__cxx03/__type_traits/remove_cvref.h>
0030 #  include <__cxx03/__utility/move.h>
0031 
0032 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0033 #    pragma GCC system_header
0034 #  endif
0035 
0036 _LIBCPP_PUSH_MACROS
0037 #  include <__cxx03/__undef_macros>
0038 
0039 _LIBCPP_BEGIN_NAMESPACE_STD
0040 
0041 #  if _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) &&   \
0042       !defined(_LIBCPP_HAS_NO_LOCALIZATION)
0043 
0044 namespace chrono {
0045 
0046 template <class>
0047 struct zoned_traits {};
0048 
0049 template <>
0050 struct zoned_traits<const time_zone*> {
0051   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* default_zone() { return chrono::locate_zone("UTC"); }
0052   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* locate_zone(string_view __name) {
0053     return chrono::locate_zone(__name);
0054   }
0055 };
0056 
0057 template <class _Duration, class _TimeZonePtr = const time_zone*>
0058 class zoned_time {
0059   // [time.zone.zonedtime.ctor]/2
0060   static_assert(__is_duration<_Duration>::value,
0061                 "the program is ill-formed since _Duration is not a specialization of std::chrono::duration");
0062 
0063   // The wording uses the constraints like
0064   //   constructible_from<zoned_time, decltype(__traits::locate_zone(string_view{}))>
0065   // Using these constraints in the code causes the compiler to give an
0066   // error that the constraint depends on itself. To avoid that issue use
0067   // the fact it is possible to create this object from a _TimeZonePtr.
0068   using __traits = zoned_traits<_TimeZonePtr>;
0069 
0070 public:
0071   using duration = common_type_t<_Duration, seconds>;
0072 
0073   _LIBCPP_HIDE_FROM_ABI zoned_time()
0074     requires requires { __traits::default_zone(); }
0075       : __zone_{__traits::default_zone()}, __tp_{} {}
0076 
0077   _LIBCPP_HIDE_FROM_ABI zoned_time(const zoned_time&)            = default;
0078   _LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const zoned_time&) = default;
0079 
0080   _LIBCPP_HIDE_FROM_ABI zoned_time(const sys_time<_Duration>& __tp)
0081     requires requires { __traits::default_zone(); }
0082       : __zone_{__traits::default_zone()}, __tp_{__tp} {}
0083 
0084   _LIBCPP_HIDE_FROM_ABI explicit zoned_time(_TimeZonePtr __zone) : __zone_{std::move(__zone)}, __tp_{} {}
0085 
0086   _LIBCPP_HIDE_FROM_ABI explicit zoned_time(string_view __name)
0087     requires(requires { __traits::locate_zone(string_view{}); } &&
0088              constructible_from<_TimeZonePtr, decltype(__traits::locate_zone(string_view{}))>)
0089       : __zone_{__traits::locate_zone(__name)}, __tp_{} {}
0090 
0091   template <class _Duration2>
0092   _LIBCPP_HIDE_FROM_ABI zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
0093     requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
0094       : __zone_{__zt.get_time_zone()}, __tp_{__zt.get_sys_time()} {}
0095 
0096   _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const sys_time<_Duration>& __tp)
0097       : __zone_{std::move(__zone)}, __tp_{__tp} {}
0098 
0099   _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const sys_time<_Duration>& __tp)
0100     requires requires { _TimeZonePtr{__traits::locate_zone(string_view{})}; }
0101       : zoned_time{__traits::locate_zone(__name), __tp} {}
0102 
0103   _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const local_time<_Duration>& __tp)
0104     requires(is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{})),
0105                               sys_time<duration>>)
0106       : __zone_{std::move(__zone)}, __tp_{__zone_->to_sys(__tp)} {}
0107 
0108   _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const local_time<_Duration>& __tp)
0109     requires(requires {
0110       _TimeZonePtr{__traits::locate_zone(string_view{})};
0111     } && is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{})),
0112                           sys_time<duration>>)
0113       : zoned_time{__traits::locate_zone(__name), __tp} {}
0114 
0115   _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const local_time<_Duration>& __tp, choose __c)
0116     requires(is_convertible_v<
0117                 decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{}, choose::earliest)),
0118                 sys_time<duration>>)
0119       : __zone_{std::move(__zone)}, __tp_{__zone_->to_sys(__tp, __c)} {}
0120 
0121   _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const local_time<_Duration>& __tp, choose __c)
0122     requires(requires {
0123       _TimeZonePtr{__traits::locate_zone(string_view{})};
0124     } && is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{}, choose::earliest)),
0125                           sys_time<duration>>)
0126       : zoned_time{__traits::locate_zone(__name), __tp, __c} {}
0127 
0128   template <class _Duration2, class _TimeZonePtr2>
0129   _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
0130     requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
0131       : __zone_{std::move(__zone)}, __tp_{__zt.get_sys_time()} {}
0132 
0133   // per wording choose has no effect
0134   template <class _Duration2, class _TimeZonePtr2>
0135   _LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const zoned_time<_Duration2, _TimeZonePtr2>& __zt, choose)
0136     requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
0137       : __zone_{std::move(__zone)}, __tp_{__zt.get_sys_time()} {}
0138 
0139   template <class _Duration2, class _TimeZonePtr2>
0140   _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
0141     requires(requires {
0142       _TimeZonePtr{__traits::locate_zone(string_view{})};
0143     } && is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>)
0144       : zoned_time{__traits::locate_zone(__name), __zt} {}
0145 
0146   template <class _Duration2, class _TimeZonePtr2>
0147   _LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const zoned_time<_Duration2, _TimeZonePtr2>& __zt, choose __c)
0148     requires(requires {
0149       _TimeZonePtr{__traits::locate_zone(string_view{})};
0150     } && is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>)
0151       : zoned_time{__traits::locate_zone(__name), __zt, __c} {}
0152 
0153   _LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const sys_time<_Duration>& __tp) {
0154     __tp_ = __tp;
0155     return *this;
0156   }
0157 
0158   _LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const local_time<_Duration>& __tp) {
0159     // TODO TZDB This seems wrong.
0160     // Assigning a non-existent or ambiguous time will throw and not satisfy
0161     // the post condition. This seems quite odd; I constructed an object with
0162     // choose::earliest and that choice is not respected.
0163     // what did LEWG do with this.
0164     // MSVC STL and libstdc++ behave the same
0165     __tp_ = __zone_->to_sys(__tp);
0166     return *this;
0167   }
0168 
0169   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI operator sys_time<duration>() const { return get_sys_time(); }
0170   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit operator local_time<duration>() const { return get_local_time(); }
0171 
0172   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _TimeZonePtr get_time_zone() const { return __zone_; }
0173   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI local_time<duration> get_local_time() const { return __zone_->to_local(__tp_); }
0174   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<duration> get_sys_time() const { return __tp_; }
0175   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_info get_info() const { return __zone_->get_info(__tp_); }
0176 
0177 private:
0178   _TimeZonePtr __zone_;
0179   sys_time<duration> __tp_;
0180 };
0181 
0182 zoned_time() -> zoned_time<seconds>;
0183 
0184 template <class _Duration>
0185 zoned_time(sys_time<_Duration>) -> zoned_time<common_type_t<_Duration, seconds>>;
0186 
0187 template <class _TimeZonePtrOrName>
0188 using __time_zone_representation =
0189     conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
0190                   const time_zone*,
0191                   remove_cvref_t<_TimeZonePtrOrName>>;
0192 
0193 template <class _TimeZonePtrOrName>
0194 zoned_time(_TimeZonePtrOrName&&) -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
0195 
0196 template <class _TimeZonePtrOrName, class _Duration>
0197 zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
0198     -> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
0199 
0200 template <class _TimeZonePtrOrName, class _Duration>
0201 zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>, choose = choose::earliest)
0202     -> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
0203 
0204 template <class _Duration, class _TimeZonePtrOrName, class TimeZonePtr2>
0205 zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, TimeZonePtr2>, choose = choose::earliest)
0206     -> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
0207 
0208 using zoned_seconds = zoned_time<seconds>;
0209 
0210 template <class _Duration1, class _Duration2, class _TimeZonePtr>
0211 _LIBCPP_HIDE_FROM_ABI bool
0212 operator==(const zoned_time<_Duration1, _TimeZonePtr>& __lhs, const zoned_time<_Duration2, _TimeZonePtr>& __rhs) {
0213   return __lhs.get_time_zone() == __rhs.get_time_zone() && __lhs.get_sys_time() == __rhs.get_sys_time();
0214 }
0215 
0216 } // namespace chrono
0217 
0218 #  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
0219          // && !defined(_LIBCPP_HAS_NO_LOCALIZATION)
0220 
0221 _LIBCPP_END_NAMESPACE_STD
0222 
0223 _LIBCPP_POP_MACROS
0224 
0225 #endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB)
0226 
0227 #endif // _LIBCPP___CXX03___CHRONO_ZONED_TIME_H