Skip to content

[libc++][TZDB] Adds zoned_time deduction guides. #95139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024

Conversation

mordante
Copy link
Member

Completes

  • LWG3232 Inconsistency in zoned_time deduction guides
  • LWG3294 zoned_time deduction guides misinterprets stringchar*

Implements parts of:

  • P0355 Extending to chrono Calendars and Time Zones

@mordante mordante requested a review from a team as a code owner June 11, 2024 16:31
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jun 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 11, 2024

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

Completes

  • LWG3232 Inconsistency in zoned_time deduction guides
  • LWG3294 zoned_time deduction guides misinterprets stringchar*

Implements parts of:

  • P0355 Extending to chrono Calendars and Time Zones

Full diff: https://github.com/llvm/llvm-project/pull/95139.diff

3 Files Affected:

  • (modified) libcxx/docs/Status/Cxx20Issues.csv (+2-2)
  • (modified) libcxx/include/__chrono/zoned_time.h (+28)
  • (added) libcxx/test/std/time/time.zone/time.zone.zonedtime/deduction.pass.cpp (+285)
diff --git a/libcxx/docs/Status/Cxx20Issues.csv b/libcxx/docs/Status/Cxx20Issues.csv
index 5051a2089c98e..a288d1add33e2 100644
--- a/libcxx/docs/Status/Cxx20Issues.csv
+++ b/libcxx/docs/Status/Cxx20Issues.csv
@@ -167,7 +167,7 @@
 "`3218 <https://wg21.link/LWG3218>`__","Modifier for ``%d``\  parse flag does not match POSIX and ``format``\  specification","Belfast","","","|chrono| |format|"
 "`3224 <https://wg21.link/LWG3224>`__","``zoned_time``\  constructor from ``TimeZonePtr``\  does not specify initialization of ``tp_``\ ","Belfast","|Complete|","19.0","|chrono|"
 "`3230 <https://wg21.link/LWG3230>`__","Format specifier ``%y/%Y``\  is missing locale alternative versions","Belfast","|Complete|","16.0","|chrono| |format|"
-"`3232 <https://wg21.link/LWG3232>`__","Inconsistency in ``zoned_time``\  deduction guides","Belfast","","","|chrono|"
+"`3232 <https://wg21.link/LWG3232>`__","Inconsistency in ``zoned_time``\  deduction guides","Belfast","|Complete|","19.0","|chrono|"
 "`3222 <https://wg21.link/LWG3222>`__","P0574R1 introduced preconditions on non-existent parameters","Belfast","",""
 "`3221 <https://wg21.link/LWG3221>`__","Result of ``year_month``\  arithmetic with ``months``\  is ambiguous","Belfast","|Complete|","8.0"
 "`3235 <https://wg21.link/LWG3235>`__","``parse``\  manipulator without abbreviation is not callable","Belfast","",""
@@ -225,7 +225,7 @@
 "`3286 <https://wg21.link/LWG3286>`__","``ranges::size``\  is not required to be valid after a call to ``ranges::begin``\  on an input range","Prague","|Complete|","15.0","|ranges|"
 "`3291 <https://wg21.link/LWG3291>`__","``iota_view::iterator``\  has the wrong ``iterator_category``\ ","Prague","|Complete|","15.0","|ranges|"
 "`3292 <https://wg21.link/LWG3292>`__","``iota_view``\  is under-constrained","Prague","|Complete|","15.0","|ranges|"
-"`3294 <https://wg21.link/LWG3294>`__","``zoned_time``\  deduction guides misinterprets ``string``\ /``char*``\ ","Prague","","","|chrono|"
+"`3294 <https://wg21.link/LWG3294>`__","``zoned_time``\  deduction guides misinterprets ``string``\ /``char*``\ ","Prague","|Complete|","19.0","|chrono|"
 "`3296 <https://wg21.link/LWG3296>`__","Inconsistent default argument for ``basic_regex<>::assign``\ ","Prague","|Complete|",""
 "`3299 <https://wg21.link/LWG3299>`__","Pointers don't need customized iterator behavior","Prague","|Complete|","15.0","|ranges|"
 "`3300 <https://wg21.link/LWG3300>`__","Non-array ``ssize``\  overload is underconstrained","Prague","|Nothing To Do|",""
diff --git a/libcxx/include/__chrono/zoned_time.h b/libcxx/include/__chrono/zoned_time.h
index 169efb8e9d6f3..33e0ac0603a6e 100644
--- a/libcxx/include/__chrono/zoned_time.h
+++ b/libcxx/include/__chrono/zoned_time.h
@@ -25,6 +25,8 @@
 #  include <__config>
 #  include <__fwd/string_view.h>
 #  include <__type_traits/common_type.h>
+#  include <__type_traits/conditional.h>
+#  include <__type_traits/remove_cvref.h>
 #  include <__utility/move.h>
 
 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -176,6 +178,32 @@ class zoned_time {
   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 =
+    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>>;
+
 } // namespace chrono
 
 #  endif // _LIBCPP_STD_VER >= 20 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/deduction.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/deduction.pass.cpp
new file mode 100644
index 0000000000000..b64dbebd235b1
--- /dev/null
+++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/deduction.pass.cpp
@@ -0,0 +1,285 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <chrono>
+
+// 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 =        // exposition only
+//     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>>;
+
+#include <chrono>
+#include <concepts>
+#include <string>
+
+#include "test_offset_time_zone.h"
+
+// Verify the results of the constructed object.
+int main(int, char**) {
+  {
+    // zoned_time() -> zoned_time<seconds>;
+    std::chrono::zoned_time zt;
+    static_assert(
+        std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+  }
+
+  {
+    // template<class Duration>
+    //   zoned_time(sys_time<Duration>)
+    //     -> zoned_time<common_type_t<Duration, seconds>>;
+    {
+      std::chrono::zoned_time zt{std::chrono::sys_time<std::chrono::nanoseconds>{std::chrono::nanoseconds{0}}};
+      static_assert(
+          std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::nanoseconds, const std::chrono::time_zone*>>);
+    }
+    {
+      std::chrono::zoned_time zt{std::chrono::sys_time<std::chrono::seconds>{std::chrono::seconds{0}}};
+      static_assert(
+          std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+    }
+    {
+      std::chrono::zoned_time zt{std::chrono::sys_time<std::chrono::days>{std::chrono::days{0}}};
+      static_assert(
+          std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+    }
+  }
+
+  {
+    // template<class TimeZonePtrOrName>
+    //   zoned_time(TimeZonePtrOrName&&)
+    //     -> zoned_time<seconds, time-zone-representation<TimeZonePtrOrName>>;
+    { // Name
+      {
+        std::chrono::zoned_time zt{"UTC"};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{std::string{"UTC"}};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{std::string_view{"UTC"}};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+    }
+    { // TimeZonePtr
+      {
+        std::chrono::zoned_time zt{static_cast<const std::chrono::time_zone*>(nullptr)};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{offset_time_zone<offset_time_zone_flags::none>{}};
+        static_assert(std::same_as<
+                      decltype(zt),
+                      std::chrono::zoned_time<std::chrono::seconds, offset_time_zone<offset_time_zone_flags::none>>>);
+      }
+      {
+        std::chrono::zoned_time zt{offset_time_zone<offset_time_zone_flags::has_default_zone>{}};
+        static_assert(
+            std::same_as<decltype(zt),
+                         std::chrono::zoned_time<std::chrono::seconds,
+                                                 offset_time_zone<offset_time_zone_flags::has_default_zone>>>);
+      }
+      {
+        std::chrono::zoned_time zt{offset_time_zone<offset_time_zone_flags::has_locate_zone>{}};
+        static_assert(std::same_as<decltype(zt),
+                                   std::chrono::zoned_time<std::chrono::seconds,
+                                                           offset_time_zone<offset_time_zone_flags::has_locate_zone>>>);
+      }
+      {
+        std::chrono::zoned_time zt{offset_time_zone<offset_time_zone_flags::both>{}};
+        static_assert(std::same_as<
+                      decltype(zt),
+                      std::chrono::zoned_time<std::chrono::seconds, offset_time_zone<offset_time_zone_flags::both>>>);
+      }
+
+      // There are no requirements on the TimeZonePtr type.
+      {
+        std::chrono::zoned_time zt{0};
+        static_assert(std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, int>>);
+      }
+      {
+        std::chrono::zoned_time zt{0.0};
+        static_assert(std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, double>>);
+      }
+      {
+        std::chrono::zoned_time zt{std::chrono::seconds{}};
+        static_assert(std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, std::chrono::seconds>>);
+      }
+    }
+  }
+
+  {
+    // template<class TimeZonePtrOrName, class Duration>
+    //   zoned_time(TimeZonePtrOrName&&, sys_time<Duration>)
+    //     -> zoned_time<common_type_t<Duration, seconds>,
+    //                   time-zone-representation<TimeZonePtrOrName>>;
+    { // Name
+      {
+        std::chrono::zoned_time zt{"UTC", std::chrono::sys_time<std::chrono::nanoseconds>{}};
+        static_assert(std::same_as<decltype(zt),
+                                   std::chrono::zoned_time<std::chrono::nanoseconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{"UTC", std::chrono::sys_time<std::chrono::days>{}};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+    }
+
+    { // TimeZonePtr
+      {
+        std::chrono::zoned_time zt{
+            static_cast<const std::chrono::time_zone*>(nullptr), std::chrono::sys_time<std::chrono::nanoseconds>{}};
+        static_assert(std::same_as<decltype(zt),
+                                   std::chrono::zoned_time<std::chrono::nanoseconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{
+            static_cast<const std::chrono::time_zone*>(nullptr), std::chrono::sys_time<std::chrono::days>{}};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{
+            offset_time_zone<offset_time_zone_flags::none>{}, std::chrono::sys_time<std::chrono::nanoseconds>{}};
+        static_assert(
+            std::same_as<
+                decltype(zt),
+                std::chrono::zoned_time<std::chrono::nanoseconds, offset_time_zone<offset_time_zone_flags::none>>>);
+      }
+      {
+        std::chrono::zoned_time zt{
+            offset_time_zone<offset_time_zone_flags::none>{}, std::chrono::sys_time<std::chrono::days>{}};
+        static_assert(std::same_as<
+                      decltype(zt),
+                      std::chrono::zoned_time<std::chrono::seconds, offset_time_zone<offset_time_zone_flags::none>>>);
+      }
+    }
+  }
+
+  {
+    // 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>>;
+    { // Name
+      {
+        std::chrono::zoned_time zt{"UTC", std::chrono::local_time<std::chrono::nanoseconds>{}};
+        static_assert(std::same_as<decltype(zt),
+                                   std::chrono::zoned_time<std::chrono::nanoseconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{"UTC", std::chrono::local_time<std::chrono::days>{}, std::chrono::choose::earliest};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+    }
+    { // TimeZonePtr
+      {
+        std::chrono::zoned_time zt{
+            std::chrono::locate_zone("UTC"), std::chrono::local_time<std::chrono::nanoseconds>{}};
+        static_assert(std::same_as<decltype(zt),
+                                   std::chrono::zoned_time<std::chrono::nanoseconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{
+            std::chrono::locate_zone("UTC"),
+            std::chrono::local_time<std::chrono::days>{},
+            std::chrono::choose::earliest};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+    }
+  }
+
+  {
+    // 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>>;
+    { // Name
+      {
+        std::chrono::zoned_time zt{
+            "UTC",
+            std::chrono::zoned_time<std::chrono::nanoseconds,
+                                    offset_time_zone<offset_time_zone_flags::has_default_zone>>{}};
+        static_assert(std::same_as<decltype(zt),
+                                   std::chrono::zoned_time<std::chrono::nanoseconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{
+            "UTC",
+            std::chrono::zoned_time<std::chrono::days, offset_time_zone<offset_time_zone_flags::has_default_zone>>{},
+            std::chrono::choose::earliest};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+    }
+    { // TimeZonePtr
+      {
+        std::chrono::zoned_time zt{
+            std::chrono::locate_zone("UTC"),
+            std::chrono::zoned_time<std::chrono::nanoseconds,
+                                    offset_time_zone<offset_time_zone_flags::has_default_zone>>{}};
+        static_assert(std::same_as<decltype(zt),
+                                   std::chrono::zoned_time<std::chrono::nanoseconds, const std::chrono::time_zone*>>);
+      }
+      {
+        std::chrono::zoned_time zt{
+            std::chrono::locate_zone("UTC"),
+            std::chrono::zoned_time<std::chrono::days, offset_time_zone<offset_time_zone_flags::has_default_zone>>{},
+            std::chrono::choose::earliest};
+        static_assert(
+            std::same_as<decltype(zt), std::chrono::zoned_time<std::chrono::seconds, const std::chrono::time_zone*>>);
+      }
+    }
+  }
+
+  return 0;
+}

@mordante mordante force-pushed the users/mordante/zoned_time__members branch 2 times, most recently from 3dcb187 to 5195d39 Compare July 7, 2024 15:32
@mordante mordante force-pushed the users/mordante/zoned_time__deduction_guides branch from 2d25530 to 072f456 Compare July 7, 2024 17:22
@ldionne ldionne self-assigned this Jul 9, 2024
@ldionne ldionne added this to the LLVM 19.X Release milestone Jul 9, 2024
@mordante mordante force-pushed the users/mordante/zoned_time__members branch from 5195d39 to dd3aa9c Compare July 10, 2024 05:51
Base automatically changed from users/mordante/zoned_time__members to main July 10, 2024 09:46
Completes
- LWG3232 Inconsistency in zoned_time  deduction guides
- LWG3294 zoned_time  deduction guides misinterprets stringchar*

Implements parts of:
- P0355 Extending to chrono Calendars and Time Zones
@mordante mordante force-pushed the users/mordante/zoned_time__deduction_guides branch from 072f456 to 4f18b43 Compare July 10, 2024 09:48
@mordante mordante merged commit e77b295 into main Jul 10, 2024
45 of 56 checks passed
@mordante mordante deleted the users/mordante/zoned_time__deduction_guides branch July 10, 2024 11:46
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
Completes
- LWG3232 Inconsistency in zoned_time  deduction guides
- LWG3294 zoned_time  deduction guides misinterprets stringchar*

Implements parts of:
- P0355 Extending to chrono Calendars and Time Zones
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants