From d0b2016c23229b10f17683d177ecd173b313e7a5 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Wed, 17 Apr 2024 21:00:22 +0200 Subject: [PATCH] [libc++][TZDB] Adds basics of zoned_time class. This implements the class, its non-templated constructors and its getters to verify the construction. Completes - LWG3224 zoned_time constructor from TimeZonePtr does not specify initialization of tp_ Implements parts of: - P0355 Extending chrono to Calendars and Time Zones --- libcxx/docs/Status/Cxx20Issues.csv | 2 +- libcxx/include/__chrono/zoned_time.h | 49 +++++++ libcxx/include/chrono | 4 + libcxx/modules/std/chrono.inc | 2 +- .../diagnostics/chrono.nodiscard.verify.cpp | 6 + .../time.zone.zonedtime/copy.assign.pass.cpp | 36 +++++ .../time.zone.zonedtime/copy.ctor.pass.cpp | 43 ++++++ .../time.zone.zonedtime.ctor/default.pass.cpp | 66 +++++++++ .../string_view.pass.cpp | 77 ++++++++++ .../sys_time.pass.cpp | 138 ++++++++++++++++++ .../time_zone_pointer.pass.cpp | 87 +++++++++++ .../get_sys_time.pass.cpp | 121 +++++++++++++++ .../get_time_zone.pass.cpp | 45 ++++++ .../types.compile.pass.cpp | 59 ++++++++ .../time.zone.zonedtime/types.verify.cpp | 29 ++++ libcxx/test/support/test_offset_time_zone.h | 68 +++++++++ 16 files changed, 830 insertions(+), 2 deletions(-) create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.assign.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.ctor.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/default.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/string_view.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/sys_time.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/time_zone_pointer.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_sys_time.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_time_zone.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/types.compile.pass.cpp create mode 100644 libcxx/test/std/time/time.zone/time.zone.zonedtime/types.verify.cpp create mode 100644 libcxx/test/support/test_offset_time_zone.h diff --git a/libcxx/docs/Status/Cxx20Issues.csv b/libcxx/docs/Status/Cxx20Issues.csv index e748ff6ad749b7..d4db7876cb2ef9 100644 --- a/libcxx/docs/Status/Cxx20Issues.csv +++ b/libcxx/docs/Status/Cxx20Issues.csv @@ -165,7 +165,7 @@ "`3225 `__","``zoned_time``\ converting constructor shall not be ``noexcept``\ ","Belfast","","","|chrono|" "`3190 `__","``std::allocator::allocate``\ sometimes returns too little storage","Belfast","|Complete|","14.0" "`3218 `__","Modifier for ``%d``\ parse flag does not match POSIX and ``format``\ specification","Belfast","","","|chrono| |format|" -"`3224 `__","``zoned_time``\ constructor from ``TimeZonePtr``\ does not specify initialization of ``tp_``\ ","Belfast","","","|chrono|" +"`3224 `__","``zoned_time``\ constructor from ``TimeZonePtr``\ does not specify initialization of ``tp_``\ ","Belfast","|Complete|","19.0","|chrono|" "`3230 `__","Format specifier ``%y/%Y``\ is missing locale alternative versions","Belfast","|Complete|","16.0","|chrono| |format|" "`3232 `__","Inconsistency in ``zoned_time``\ deduction guides","Belfast","","","|chrono|" "`3222 `__","P0574R1 introduced preconditions on non-existent parameters","Belfast","","" diff --git a/libcxx/include/__chrono/zoned_time.h b/libcxx/include/__chrono/zoned_time.h index c6084426ad72b4..f7510c9121a594 100644 --- a/libcxx/include/__chrono/zoned_time.h +++ b/libcxx/include/__chrono/zoned_time.h @@ -16,15 +16,22 @@ // Enable the contents of the header only when libc++ was built with experimental features enabled. #if !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB) +# include <__chrono/duration.h> +# include <__chrono/system_clock.h> # include <__chrono/time_zone.h> # include <__chrono/tzdb_list.h> # include <__config> # include <__fwd/string_view.h> +# include <__type_traits/common_type.h> +# include <__utility/move.h> # 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 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) && \ @@ -43,6 +50,46 @@ struct zoned_traits { } }; +template +class zoned_time { + // [time.zone.zonedtime.ctor]/2 + static_assert(__is_duration<_Duration>::value, + "the program is ill-formed since _Duration is not a specialization of std::chrono::duration"); + + using __traits = 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 + // would create a dependency on itself. Instead depend on the fact + // a constructor taking a _TimeZonePtr exists. + constructible_from<_TimeZonePtr, decltype(__traits::locate_zone(string_view{}))>) + : __zone_{__traits::locate_zone(__name)}, __tp_{} {} + + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI _TimeZonePtr get_time_zone() const { return __zone_; } + [[nodiscard]] _LIBCPP_HIDE_FROM_ABI sys_time get_sys_time() const { return __tp_; } + +private: + _TimeZonePtr __zone_; + sys_time __tp_; +}; + } // namespace chrono # endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM) @@ -50,6 +97,8 @@ struct zoned_traits { _LIBCPP_END_NAMESPACE_STD +_LIBCPP_POP_MACROS + #endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_TZDB) #endif // _LIBCPP___CHRONO_ZONED_TIME_H diff --git a/libcxx/include/chrono b/libcxx/include/chrono index c1a92595ff1f59..026e920342e1ed 100644 --- a/libcxx/include/chrono +++ b/libcxx/include/chrono @@ -789,6 +789,10 @@ strong_ordering operator<=>(const time_zone& x, const time_zone& y) noexcept; // [time.zone.zonedtraits], class template zoned_traits template struct zoned_traits; // C++20 +// [time.zone.zonedtime], class template zoned_time +template // C++20 +class zoned_time; + // [time.zone.leap], leap second support class leap_second { // C++20 public: diff --git a/libcxx/modules/std/chrono.inc b/libcxx/modules/std/chrono.inc index 87e32afbe4bdca..89fe7284eb694d 100644 --- a/libcxx/modules/std/chrono.inc +++ b/libcxx/modules/std/chrono.inc @@ -230,10 +230,10 @@ export namespace std { // [time.zone.zonedtraits], class template zoned_traits using std::chrono::zoned_traits; -# if 0 // [time.zone.zonedtime], class template zoned_time using std::chrono::zoned_time; +# if 0 using std::chrono::zoned_seconds; # endif // if 0 diff --git a/libcxx/test/libcxx/diagnostics/chrono.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/chrono.nodiscard.verify.cpp index 6fed41bdb43edb..e8337cb33822e9 100644 --- a/libcxx/test/libcxx/diagnostics/chrono.nodiscard.verify.cpp +++ b/libcxx/test/libcxx/diagnostics/chrono.nodiscard.verify.cpp @@ -78,4 +78,10 @@ void test() { t::default_zone(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} t::locate_zone(""); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} } + + { + std::chrono::zoned_time zt; + zt.get_time_zone(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} + zt.get_sys_time(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}} + } } diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.assign.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.assign.pass.cpp new file mode 100644 index 00000000000000..b74ae06057b8da --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.assign.pass.cpp @@ -0,0 +1,36 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// zoned_time& operator=(const zoned_time&) = default; + +#include +#include + +int main(int, char**) { + std::chrono::zoned_time zt{std::chrono::sys_seconds{std::chrono::seconds{42}}}; + assert(zt.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); + + std::chrono::zoned_time copy; + copy = zt; + assert(copy.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(copy.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.ctor.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.ctor.pass.cpp new file mode 100644 index 00000000000000..c7a36df2b4b200 --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/copy.ctor.pass.cpp @@ -0,0 +1,43 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// zoned_time(const zoned_time&) = default; + +#include +#include + +int main(int, char**) { + std::chrono::zoned_time zt{std::chrono::sys_seconds{std::chrono::seconds{42}}}; + assert(zt.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); + + { + std::chrono::zoned_time copy{zt}; + assert(copy.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(copy.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); + } + + { + std::chrono::zoned_time copy = zt; + assert(copy.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(copy.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); + } + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/default.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/default.pass.cpp new file mode 100644 index 00000000000000..dd498a9325b317 --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/default.pass.cpp @@ -0,0 +1,66 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// zoned_time(); + +#include +#include +#include + +#include "test_offset_time_zone.h" + +// Verify the results of the default constructed object, +// and whether the constructor's constrains are satisfied. +int main(int, char**) { + { + static_assert(std::default_initializable>); + std::chrono::zoned_time zt; + assert(zt.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + static_assert(!std::default_initializable< + std::chrono::zoned_time>>); + + { + using type = offset_time_zone; + static_assert(std::default_initializable>); + + std::chrono::zoned_time zt; + + assert(zt.get_time_zone().offset() == std::chrono::seconds{0}); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + static_assert( + !std::default_initializable< + std::chrono::zoned_time>>); + + { + using type = offset_time_zone; + static_assert(std::default_initializable>); + + std::chrono::zoned_time zt; + + assert(zt.get_time_zone().offset() == std::chrono::seconds{0}); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/string_view.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/string_view.pass.cpp new file mode 100644 index 00000000000000..8b237442a5c97b --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/string_view.pass.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// explicit zoned_time(string_view name); + +#include +#include + +#include "test_offset_time_zone.h" + +// Verify the results of the constructed object. +int main(int, char**) { + { + using ptr = const std::chrono::time_zone*; + static_assert(std::constructible_from, std::string_view>); + static_assert(!std::convertible_to>); + + std::chrono::zoned_time zt{"UTC"}; + + assert(zt.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + { + using ptr = offset_time_zone; + static_assert(!std::constructible_from, std::string_view>); + static_assert(!std::convertible_to>); + } + + { + using ptr = offset_time_zone; + static_assert(!std::constructible_from, std::string_view>); + static_assert(!std::convertible_to>); + } + + { + using ptr = offset_time_zone; + static_assert(std::constructible_from, std::string_view>); + static_assert(!std::convertible_to>); + + ptr tz; + std::chrono::zoned_time zt{"42"}; + + assert(zt.get_time_zone().offset() == std::chrono::seconds{42}); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + { + using ptr = offset_time_zone; + static_assert(std::constructible_from, std::string_view>); + static_assert(!std::convertible_to>); + + ptr tz; + std::chrono::zoned_time zt{"42"}; + + assert(zt.get_time_zone().offset() == std::chrono::seconds{42}); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/sys_time.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/sys_time.pass.cpp new file mode 100644 index 00000000000000..542024dfa78a5d --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/sys_time.pass.cpp @@ -0,0 +1,138 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// zoned_time(const sys_time& st); + +#include +#include +#include + +#include "test_offset_time_zone.h" + +static void test_construction() { + static_assert(std::constructible_from, std::chrono::sys_seconds>); + std::chrono::zoned_time zt{std::chrono::sys_seconds{std::chrono::seconds{42}}}; + assert(zt.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); +} + +static void test_conversion() { + static_assert(std::convertible_to>); + std::chrono::zoned_time zt = std::chrono::sys_seconds{std::chrono::seconds{42}}; + assert(zt.get_time_zone() == std::chrono::locate_zone("UTC")); + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); +} + +static void test_duration_conversion() { + { + using duration = std::chrono::nanoseconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == time_point{duration{42}}); + } + { + using duration = std::chrono::microseconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == time_point{duration{42}}); + } + { + using duration = std::chrono::milliseconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == time_point{duration{42}}); + } + { + using duration = std::chrono::seconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == time_point{duration{42}}); + } + { + using duration = std::chrono::days; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::days{42}}); + } + { + using duration = std::chrono::weeks; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::weeks{42}}); + } + { + using duration = std::chrono::months; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::months{42}}); + } + { + using duration = std::chrono::years; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::years{42}}); + } +} + +static void test_duration_constraints() { + static_assert(!std::constructible_from< + std::chrono::zoned_time>, + std::chrono::sys_seconds>); + + { + using type = offset_time_zone; + static_assert( + std::constructible_from, std::chrono::sys_seconds>); + + std::chrono::zoned_time zt = std::chrono::sys_seconds{std::chrono::seconds{42}}; + + assert(zt.get_time_zone().offset() == std::chrono::seconds{0}); + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); + } + + static_assert( + !std::constructible_from< + std::chrono::zoned_time>, + std::chrono::sys_seconds>); + + { + using type = offset_time_zone; + static_assert( + std::constructible_from, std::chrono::sys_seconds>); + + std::chrono::zoned_time zt = std::chrono::sys_seconds{std::chrono::seconds{42}}; + + assert(zt.get_time_zone().offset() == std::chrono::seconds{0}); + assert(zt.get_sys_time() == std::chrono::sys_seconds{std::chrono::seconds{42}}); + } +} + +// Verify: +// - the results of the constructed object, +// - conversion construction is possible, +// - Duration is converted to zoned_time<>::duration, and +// - whether the constructor's constraints are satisfied. +int main(int, char**) { + test_construction(); + test_conversion(); + test_duration_conversion(); + test_duration_constraints(); + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/time_zone_pointer.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/time_zone_pointer.pass.cpp new file mode 100644 index 00000000000000..69a44417b6dec1 --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.ctor/time_zone_pointer.pass.cpp @@ -0,0 +1,87 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// explicit zoned_time(TimeZonePtr z); + +#include +#include + +#include "test_offset_time_zone.h" + +// Verify the results of the constructed object. +int main(int, char**) { + { + using ptr = const std::chrono::time_zone*; + static_assert(std::constructible_from, ptr>); + static_assert(!std::convertible_to>); + + ptr tz = std::chrono::locate_zone("UTC"); + std::chrono::zoned_time zt{tz}; + + assert(zt.get_time_zone() == tz); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + { + using ptr = offset_time_zone; + static_assert(std::constructible_from, ptr>); + static_assert(!std::convertible_to>); + + ptr tz; + std::chrono::zoned_time zt{tz}; + + assert(zt.get_time_zone().offset() == tz.offset()); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + { + using ptr = offset_time_zone; + static_assert(std::constructible_from, ptr>); + static_assert(!std::convertible_to>); + + ptr tz; + std::chrono::zoned_time zt{tz}; + + assert(zt.get_time_zone().offset() == tz.offset()); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + { + using ptr = offset_time_zone; + static_assert(std::constructible_from, ptr>); + static_assert(!std::convertible_to>); + + ptr tz; + std::chrono::zoned_time zt{tz}; + + assert(zt.get_time_zone().offset() == tz.offset()); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + { + using ptr = offset_time_zone; + static_assert(std::constructible_from, ptr>); + static_assert(!std::convertible_to>); + + ptr tz; + std::chrono::zoned_time zt{tz}; + + assert(zt.get_time_zone().offset() == tz.offset()); + assert(zt.get_sys_time() == std::chrono::sys_seconds{}); + } + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_sys_time.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_sys_time.pass.cpp new file mode 100644 index 00000000000000..bdad29aae76af6 --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_sys_time.pass.cpp @@ -0,0 +1,121 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// sys_time get_sys_time() const; + +#include +#include + +#include "test_offset_time_zone.h" + +static void test_const_member() { + { + using duration = std::chrono::nanoseconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + { + using duration = std::chrono::nanoseconds; + using time_point = std::chrono::sys_time; + const std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } +} + +static void test_duration_conversion() { + // common_type_t -> duration + { + using duration = std::chrono::nanoseconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + { + using duration = std::chrono::microseconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + { + using duration = std::chrono::milliseconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + // common_type_t -> seconds + { + using duration = std::chrono::seconds; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + // common_type_t -> seconds + { + using duration = std::chrono::days; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + { + using duration = std::chrono::weeks; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + { + using duration = std::chrono::months; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } + { + using duration = std::chrono::years; + using time_point = std::chrono::sys_time; + std::chrono::zoned_time zt{time_point{duration{42}}}; + + std::same_as decltype(auto) time = zt.get_sys_time(); + assert(time == time_point{duration{42}}); + } +} + +int main(int, char**) { + test_const_member(); + test_duration_conversion(); + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_time_zone.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_time_zone.pass.cpp new file mode 100644 index 00000000000000..270db92c273417 --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.members/get_time_zone.pass.cpp @@ -0,0 +1,45 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time; +// +// TimeZonePtr get_time_zone() const; + +#include +#include + +#include "test_offset_time_zone.h" + +int main(int, char**) { + { + const std::chrono::time_zone* tz = std::chrono::locate_zone("UTC"); + std::chrono::zoned_time zt{tz}; + + std::same_as decltype(auto) ptr = zt.get_time_zone(); + assert(ptr = tz); + } + + { + int tz = 0; + const std::chrono::zoned_time zt{&tz}; + + std::same_as decltype(auto) ptr = zt.get_time_zone(); + assert(ptr = &tz); + } + + return 0; +} diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/types.compile.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/types.compile.pass.cpp new file mode 100644 index 00000000000000..f2b198249e73de --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/types.compile.pass.cpp @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// class zoned_time { +// public: +// using duration = common_type_t; +// ... +// zoned_time(const zoned_time&) = default; +// zoned_time& operator=(const zoned_time&) = default; +// +// }; + +#include +#include +#include +#include + +// Test duration +static_assert(std::same_as::duration, std::chrono::nanoseconds>); +static_assert(std::same_as::duration, std::chrono::microseconds>); +static_assert(std::same_as::duration, std::chrono::milliseconds>); +static_assert(std::same_as::duration, std::chrono::seconds>); +static_assert(std::same_as::duration, std::chrono::seconds>); +static_assert(std::same_as::duration, std::chrono::seconds>); +static_assert(std::same_as::duration, std::chrono::seconds>); +static_assert(std::same_as::duration, std::chrono::seconds>); + +// Tests defaulted copy construct/assign and move construct/assign +static_assert(std::is_copy_constructible_v>); +static_assert(std::is_move_constructible_v>); +static_assert(std::is_copy_assignable_v>); +static_assert(std::is_move_assignable_v>); + +// There are no requirements for TimeZonePtr, so test with a non-pointer type. +static_assert(std::is_copy_constructible_v>); +static_assert(std::is_move_constructible_v>); +static_assert(std::is_copy_assignable_v>); +static_assert(std::is_move_assignable_v>); + +// Test with a move only type, since the copy constructor is defined, no move +// constuctor is generated. +static_assert(!std::is_copy_constructible_v>>); +static_assert(!std::is_move_constructible_v>>); +static_assert(!std::is_copy_assignable_v>>); +static_assert(!std::is_move_assignable_v>>); diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/types.verify.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/types.verify.cpp new file mode 100644 index 00000000000000..5d0587055f94ef --- /dev/null +++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/types.verify.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: no-filesystem, no-localization, no-tzdb + +// XFAIL: libcpp-has-no-experimental-tzdb +// XFAIL: availability-tzdb-missing + +// + +// template +// +// If Duration is not a specialization of chrono::duration, the program is ill-formed. + +#include + +int main(int, char**) { + // clang-format off + std::chrono::zoned_time v; // expected-error@*:* {{static assertion failed due to requirement '__is_duration::value': the program is ill-formed since _Duration is not a specialization of std::chrono::duration}} + // clang-format on + + return 0; +} diff --git a/libcxx/test/support/test_offset_time_zone.h b/libcxx/test/support/test_offset_time_zone.h new file mode 100644 index 00000000000000..e1c9f05c2483c0 --- /dev/null +++ b/libcxx/test/support/test_offset_time_zone.h @@ -0,0 +1,68 @@ +// -*- 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 +// +//===----------------------------------------------------------------------===// + +#ifndef SUPPORT_TEST_OFFSET_TIME_ZONE_H +#define SUPPORT_TEST_OFFSET_TIME_ZONE_H + +#include +#include +#include +#include + +enum class offset_time_zone_flags { + none = 0, + has_default_zone = 1, + has_locate_zone = 2, + both = has_default_zone | has_locate_zone +}; + +// The enforcement of the flags is done in the zoned_traits +template +class offset_time_zone { +public: + offset_time_zone() : offset_{std::chrono::seconds{0}} {} + explicit offset_time_zone(std::string_view name) { + int count; + const char* begin = name.data(); + const char* end = begin + name.size(); + std::from_chars_result result = std::from_chars(begin, end, count); + assert(result == std::from_chars_result(end, std::errc{})); + + offset_ = std::chrono::seconds(count); + } + + std::chrono::seconds offset() const { return offset_; } + +private: + std::chrono::seconds offset_; +}; + +template <> +struct std::chrono::zoned_traits> { + using type = offset_time_zone; + + static type default_zone() { return {}; } +}; + +template <> +struct std::chrono::zoned_traits> { + using type = offset_time_zone; + + static type locate_zone(std::string_view name) { return type{name}; } +}; + +template <> +struct std::chrono::zoned_traits> { + using type = offset_time_zone; + + static type default_zone() { return {}; } + static type locate_zone(std::string_view name) { return type{name}; } +}; + +#endif // SUPPORT_TEST_OFFSET_TIME_ZONE_H