[libcxx-commits] [libcxx] [libc++][TZDB] Adds basics of zoned_time class. (PR #94999)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jul 7 03:08:25 PDT 2024


================
@@ -43,13 +50,55 @@ struct zoned_traits<const time_zone*> {
   }
 };
 
+template <class _Duration, class _TimeZonePtr = const time_zone*>
+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<zoned_time, decltype(__traits::locate_zone(string_view{}))>
+             // 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{}))>)
----------------
mordante wrote:

I tried your suggestion and the compiler complains with
```
build/include/c++/v1/__chrono/zoned_time.h:83:2: error: satisfaction of constraint 'constructible_from<zoned_time<_Duration, _TimeZonePtr>, decltype(__traits::locate_zone(string_view{}))>' depends on itself
# |    83 |         constructible_from<zoned_time, decltype(__traits::locate_zone(string_view{}))>)
# |       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
So the code is correct. I'll update the comment a bit. I have not checked which part of the wording does not permit this.

Note that adding a constraint like
```
constructible_from<zoned_time, _TimeZonePtr>
```
triggers the same error. So I expect the error is due to the fact this constructor is part of the overload set that the compiler needs to evaluate whether it's a viable overload.

https://github.com/llvm/llvm-project/pull/94999


More information about the libcxx-commits mailing list