Skip to content

[libc++][TZDB] Implements zoned_time's operator==. #95140

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

Implements parts of:

  • P0355 Extending to chrono Calendars and Time Zones
  • P1614R2 The Mothership has Landed

@mordante mordante requested a review from a team as a code owner June 11, 2024 16:34
@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

Implements parts of:

  • P0355 Extending to chrono Calendars and Time Zones
  • P1614R2 The Mothership has Landed

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

4 Files Affected:

  • (modified) libcxx/docs/Status/SpaceshipProjects.csv (+2-2)
  • (modified) libcxx/include/__chrono/zoned_time.h (+6)
  • (modified) libcxx/include/chrono (+4)
  • (added) libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.nonmembers/eq.pass.cpp (+71)
diff --git a/libcxx/docs/Status/SpaceshipProjects.csv b/libcxx/docs/Status/SpaceshipProjects.csv
index 128b23b0c2c74..74441dcdea496 100644
--- a/libcxx/docs/Status/SpaceshipProjects.csv
+++ b/libcxx/docs/Status/SpaceshipProjects.csv
@@ -140,7 +140,7 @@ Section,Description,Dependencies,Assignee,Complete
 "| `[class.slice.overview] <https://wg21.link/class.slice.overview>`_
 | `[slice.ops] <https://wg21.link/slice.ops>`_",| `slice <https://reviews.llvm.org/D152617>`_,None,Hristo Hristov,|Complete|
 - `5.12 Clause 27: Time library <https://wg21.link/p1614r2#clause-27-time-library>`_,,,,
-| `[time.syn] <https://wg21.link/time.syn>`_,|,None,Mark de Wever,|In Progress|
+| `[time.syn] <https://wg21.link/time.syn>`_,|,None,Mark de Wever,|Complete|
 | `[time.duration.comparisons] <https://wg21.link/time.duration.comparisons>`_, `chrono::duration <https://reviews.llvm.org/D145881>`_, None, Hristo Hristov, |Complete|
 | `[time.point.comparisons] <https://wg21.link/time.point.comparisons>`_, `chrono::time_point <https://reviews.llvm.org/D146250>`_, None, Hristo Hristov, |Complete|
 "| `[time.cal.day.nonmembers] <https://wg21.link/time.cal.day.nonmembers>`_
@@ -172,7 +172,7 @@ Section,Description,Dependencies,Assignee,Complete
 | `year_month_weekday <https://reviews.llvm.org/D152699>`_
 | `year_month_weekday_last <https://reviews.llvm.org/D152699>`_",None,Hristo Hristov,|Complete|
 `[time.zone.nonmembers] <https://wg21.link/time.zone.nonmembers>`_,"`chrono::time_zone`",,Mark de Wever,|Complete|
-`[time.zone.zonedtime.nonmembers] <https://wg21.link/time.zone.zonedtime.nonmembers>`_,"`chrono::zoned_time`",A ``<chrono>`` implementation,Mark de Wever,|In Progress|
+`[time.zone.zonedtime.nonmembers] <https://wg21.link/time.zone.zonedtime.nonmembers>`_,"`chrono::zoned_time`",,Mark de Wever,|Complete|
 `[time.zone.leap.nonmembers] <https://wg21.link/time.zone.leap.nonmembers>`_,"`chrono::time_leap_seconds`",,Mark de Wever,|Complete|
 `[time.zone.link.nonmembers] <https://wg21.link/time.zone.link.nonmembers>`_,"`chrono::time_zone_link`",,Mark de Wever,|Complete|
 - `5.13 Clause 28: Localization library <https://wg21.link/p1614r2#clause-28-localization-library>`_,,,,
diff --git a/libcxx/include/__chrono/zoned_time.h b/libcxx/include/__chrono/zoned_time.h
index 33e0ac0603a6e..db5e67135d844 100644
--- a/libcxx/include/__chrono/zoned_time.h
+++ b/libcxx/include/__chrono/zoned_time.h
@@ -204,6 +204,12 @@ 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>>;
 
+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 && !defined(_LIBCPP_HAS_NO_TIME_ZONE_DATABASE) && !defined(_LIBCPP_HAS_NO_FILESYSTEM)
diff --git a/libcxx/include/chrono b/libcxx/include/chrono
index 026e920342e1e..df7ebf45bd907 100644
--- a/libcxx/include/chrono
+++ b/libcxx/include/chrono
@@ -793,6 +793,10 @@ template<class T> struct zoned_traits;
 template<class Duration, class TimeZonePtr = const time_zone*>                   // C++20
 class zoned_time;
 
+template<class Duration1, class Duration2, class TimeZonePtr>                    // C++20
+  bool operator==(const zoned_time<Duration1, TimeZonePtr>& x,
+                  const zoned_time<Duration2, TimeZonePtr>& y);
+
 // [time.zone.leap], leap second support
 class leap_second {                                                              // C++20
 public:
diff --git a/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.nonmembers/eq.pass.cpp b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.nonmembers/eq.pass.cpp
new file mode 100644
index 0000000000000..2ccb060d24ee6
--- /dev/null
+++ b/libcxx/test/std/time/time.zone/time.zone.zonedtime/time.zone.zonedtime.nonmembers/eq.pass.cpp
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// REQUIRES: has-unix-headers
+// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
+// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
+
+// XFAIL: libcpp-has-no-experimental-tzdb
+
+// <chrono>
+
+// template<class Duration1, class Duration2, class TimeZonePtr>
+//  bool operator==(const zoned_time<Duration1, TimeZonePtr>& x,
+//                  const zoned_time<Duration2, TimeZonePtr>& y);
+//
+// Note operator!= is generated by the compiler
+
+#include <chrono>
+
+#include "test_comparisons.h"
+
+int main(int, char**) {
+  {
+    std::chrono::zoned_time zt;
+    assert(testEquality(zt, zt, true));
+  }
+  {
+    std::chrono::zoned_time lhs{"UTC"};
+    std::chrono::zoned_time rhs{"Europe/Berlin"};
+    assert(testEquality(lhs, rhs, false));
+  }
+  {
+    std::chrono::zoned_time lhs{"UTC", std::chrono::sys_time<std::chrono::seconds>{std::chrono::seconds{123}}};
+
+    assert(testEquality(lhs,
+                        std::chrono::zoned_time{
+                            std::chrono::sys_time<std::chrono::nanoseconds>{std::chrono::nanoseconds{123'000'000'000}}},
+                        true));
+    assert(testEquality(lhs,
+                        std::chrono::zoned_time{
+                            std::chrono::sys_time<std::chrono::nanoseconds>{std::chrono::nanoseconds{123'000'000'001}}},
+                        false));
+
+    assert(testEquality(lhs,
+                        std::chrono::zoned_time{
+                            std::chrono::sys_time<std::chrono::microseconds>{std::chrono::microseconds{123'000'000}}},
+                        true));
+    assert(testEquality(lhs,
+                        std::chrono::zoned_time{
+                            std::chrono::sys_time<std::chrono::microseconds>{std::chrono::microseconds{123'000'001}}},
+                        false));
+
+    assert(testEquality(
+        lhs,
+        std::chrono::zoned_time{std::chrono::sys_time<std::chrono::milliseconds>{std::chrono::milliseconds{123'000}}},
+        true));
+    assert(testEquality(
+        lhs,
+        std::chrono::zoned_time{std::chrono::sys_time<std::chrono::milliseconds>{std::chrono::milliseconds{123'001}}},
+        false));
+  }
+
+  return 0;
+}

@mordante mordante force-pushed the users/mordante/zoned_time__deduction_guides branch from 2d25530 to 072f456 Compare July 7, 2024 17:22
@mordante mordante force-pushed the users/mordante/zoned_time__operator== branch from 86dc932 to 24e1151 Compare July 8, 2024 05:43
@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__deduction_guides branch from 072f456 to 4f18b43 Compare July 10, 2024 09:48
Base automatically changed from users/mordante/zoned_time__deduction_guides to main July 10, 2024 11:46
@mordante mordante force-pushed the users/mordante/zoned_time__operator== branch 2 times, most recently from 32b3172 to c2640df Compare July 10, 2024 12:25
Copy link

github-actions bot commented Jul 10, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Implements parts of:
- P0355 Extending to chrono Calendars and Time Zones
- P1614R2 The Mothership has Landed
@mordante mordante force-pushed the users/mordante/zoned_time__operator== branch from c2640df to 7ec00b6 Compare July 10, 2024 12:42
@mordante mordante merged commit 2b1b4e3 into main Jul 10, 2024
49 of 58 checks passed
@mordante mordante deleted the users/mordante/zoned_time__operator== branch July 10, 2024 14:06
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
Implements parts of:
- P0355 Extending to chrono Calendars and Time Zones
- P1614R2 The Mothership has Landed
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