1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
#ifndef _LIBCPP___CHRONO_ZONED_TIME_H
#define _LIBCPP___CHRONO_ZONED_TIME_H
#include <version>
// Enable the contents of the header only when libc++ was built with experimental features enabled.
#if _LIBCPP_HAS_EXPERIMENTAL_TZDB
# include <__chrono/calendar.h>
# include <__chrono/duration.h>
# include <__chrono/sys_info.h>
# include <__chrono/system_clock.h>
# include <__chrono/time_zone.h>
# include <__chrono/tzdb_list.h>
# include <__concepts/constructible.h>
# include <__config>
# include <__type_traits/common_type.h>
# include <__type_traits/conditional.h>
# include <__type_traits/remove_cvref.h>
# include <__utility/declval.h>
# include <__utility/move.h>
# include <string_view>
# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
# endif
_LIBCPP_PUSH_MACROS
# include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
# if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
namespace chrono {
template <class>
struct zoned_traits {};
template <>
struct zoned_traits<const time_zone*> {
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* default_zone() { return chrono::locate_zone("UTC"); }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI static const time_zone* locate_zone(string_view __name) {
return chrono::locate_zone(__name);
}
};
template <class _Duration, class _TimeZonePtr = const time_zone*>
class zoned_time {
// [time.zone.zonedtime.ctor]/2
static_assert(__is_duration_v<_Duration>,
"the program is ill-formed since _Duration is not a specialization of std::chrono::duration");
// The wording uses the constraints like
// constructible_from<zoned_time, decltype(__traits::locate_zone(string_view{}))>
// Using these constraints in the code causes the compiler to give an
// error that the constraint depends on itself. To avoid that issue use
// the fact it is possible to create this object from a _TimeZonePtr.
using __traits _LIBCPP_NODEBUG = zoned_traits<_TimeZonePtr>;
public:
using duration = common_type_t<_Duration, seconds>;
_LIBCPP_HIDE_FROM_ABI zoned_time()
requires requires { __traits::default_zone(); }
: __zone_{__traits::default_zone()}, __tp_{} {}
_LIBCPP_HIDE_FROM_ABI zoned_time(const zoned_time&) = default;
_LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const zoned_time&) = default;
_LIBCPP_HIDE_FROM_ABI zoned_time(const sys_time<_Duration>& __tp)
requires requires { __traits::default_zone(); }
: __zone_{__traits::default_zone()}, __tp_{__tp} {}
_LIBCPP_HIDE_FROM_ABI explicit zoned_time(_TimeZonePtr __zone) : __zone_{std::move(__zone)}, __tp_{} {}
_LIBCPP_HIDE_FROM_ABI explicit zoned_time(string_view __name)
requires(requires { __traits::locate_zone(string_view{}); } &&
constructible_from<_TimeZonePtr, decltype(__traits::locate_zone(string_view{}))>)
: __zone_{__traits::locate_zone(__name)}, __tp_{} {}
template <class _Duration2>
_LIBCPP_HIDE_FROM_ABI zoned_time(const zoned_time<_Duration2, _TimeZonePtr>& __zt)
requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
: __zone_{__zt.get_time_zone()}, __tp_{__zt.get_sys_time()} {}
_LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const sys_time<_Duration>& __tp)
: __zone_{std::move(__zone)}, __tp_{__tp} {}
_LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const sys_time<_Duration>& __tp)
requires requires { _TimeZonePtr{__traits::locate_zone(string_view{})}; }
: zoned_time{__traits::locate_zone(__name), __tp} {}
_LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const local_time<_Duration>& __tp)
requires(is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{})),
sys_time<duration>>)
: __zone_{std::move(__zone)}, __tp_{__zone_->to_sys(__tp)} {}
_LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const local_time<_Duration>& __tp)
requires(requires {
_TimeZonePtr{__traits::locate_zone(string_view{})};
} && is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{})),
sys_time<duration>>)
: zoned_time{__traits::locate_zone(__name), __tp} {}
_LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const local_time<_Duration>& __tp, choose __c)
requires(is_convertible_v<
decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{}, choose::earliest)),
sys_time<duration>>)
: __zone_{std::move(__zone)}, __tp_{__zone_->to_sys(__tp, __c)} {}
_LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const local_time<_Duration>& __tp, choose __c)
requires(requires {
_TimeZonePtr{__traits::locate_zone(string_view{})};
} && is_convertible_v<decltype(std::declval<_TimeZonePtr&>() -> to_sys(local_time<_Duration>{}, choose::earliest)),
sys_time<duration>>)
: zoned_time{__traits::locate_zone(__name), __tp, __c} {}
template <class _Duration2, class _TimeZonePtr2>
_LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
: __zone_{std::move(__zone)}, __tp_{__zt.get_sys_time()} {}
// per wording choose has no effect
template <class _Duration2, class _TimeZonePtr2>
_LIBCPP_HIDE_FROM_ABI zoned_time(_TimeZonePtr __zone, const zoned_time<_Duration2, _TimeZonePtr2>& __zt, choose)
requires is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>
: __zone_{std::move(__zone)}, __tp_{__zt.get_sys_time()} {}
template <class _Duration2, class _TimeZonePtr2>
_LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const zoned_time<_Duration2, _TimeZonePtr2>& __zt)
requires(requires {
_TimeZonePtr{__traits::locate_zone(string_view{})};
} && is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>)
: zoned_time{__traits::locate_zone(__name), __zt} {}
template <class _Duration2, class _TimeZonePtr2>
_LIBCPP_HIDE_FROM_ABI zoned_time(string_view __name, const zoned_time<_Duration2, _TimeZonePtr2>& __zt, choose __c)
requires(requires {
_TimeZonePtr{__traits::locate_zone(string_view{})};
} && is_convertible_v<sys_time<_Duration2>, sys_time<_Duration>>)
: zoned_time{__traits::locate_zone(__name), __zt, __c} {}
_LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const sys_time<_Duration>& __tp) {
__tp_ = __tp;
return *this;
}
_LIBCPP_HIDE_FROM_ABI zoned_time& operator=(const local_time<_Duration>& __tp) {
// TODO TZDB This seems wrong.
// Assigning a non-existent or ambiguous time will throw and not satisfy
// the post condition. This seems quite odd; I constructed an object with
// choose::earliest and that choice is not respected.
// what did LEWG do with this.
// MSVC STL and libstdc++ behave the same
__tp_ = __zone_->to_sys(__tp);
return *this;
}
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI operator sys_time<duration>() const { return get_sys_time(); }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit operator local_time<duration>() const { return get_local_time(); }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI _TimeZonePtr get_time_zone() const { return __zone_; }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI local_time<duration> get_local_time() const { return __zone_->to_local(__tp_); }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time<duration> get_sys_time() const { return __tp_; }
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_info get_info() const { return __zone_->get_info(__tp_); }
private:
_TimeZonePtr __zone_;
sys_time<duration> __tp_;
};
zoned_time() -> zoned_time<seconds>;
template <class _Duration>
zoned_time(sys_time<_Duration>) -> zoned_time<common_type_t<_Duration, seconds>>;
template <class _TimeZonePtrOrName>
using __time_zone_representation _LIBCPP_NODEBUG =
conditional_t<is_convertible_v<_TimeZonePtrOrName, string_view>,
const time_zone*,
remove_cvref_t<_TimeZonePtrOrName>>;
template <class _TimeZonePtrOrName>
zoned_time(_TimeZonePtrOrName&&) -> zoned_time<seconds, __time_zone_representation<_TimeZonePtrOrName>>;
template <class _TimeZonePtrOrName, class _Duration>
zoned_time(_TimeZonePtrOrName&&, sys_time<_Duration>)
-> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
template <class _TimeZonePtrOrName, class _Duration>
zoned_time(_TimeZonePtrOrName&&, local_time<_Duration>, choose = choose::earliest)
-> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
template <class _Duration, class _TimeZonePtrOrName, class _TimeZonePtr2>
zoned_time(_TimeZonePtrOrName&&, zoned_time<_Duration, _TimeZonePtr2>, choose = choose::earliest)
-> zoned_time<common_type_t<_Duration, seconds>, __time_zone_representation<_TimeZonePtrOrName>>;
using zoned_seconds = zoned_time<seconds>;
template <class _Duration1, class _Duration2, class _TimeZonePtr>
_LIBCPP_HIDE_FROM_ABI bool
operator==(const zoned_time<_Duration1, _TimeZonePtr>& __lhs, const zoned_time<_Duration2, _TimeZonePtr>& __rhs) {
return __lhs.get_time_zone() == __rhs.get_time_zone() && __lhs.get_sys_time() == __rhs.get_sys_time();
}
} // namespace chrono
# endif // _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_TIME_ZONE_DATABASE && _LIBCPP_HAS_FILESYSTEM &&
// _LIBCPP_HAS_LOCALIZATION
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
#endif // _LIBCPP___CHRONO_ZONED_TIME_H
|