Back to home page

EIC code displayed by LXR

 
 

    


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

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