[llvm-branch-commits] [libcxx] [libc++][TZDB] Finishes zoned_time member functions. (PR #95026)
Louis Dionne via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jul 9 08:52:15 PDT 2024
================
@@ -0,0 +1,243 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+
+// template<class Duration, class TimeZonePtr = const time_zone*>
+// class zoned_time;
+//
+// zoned_time& operator=(const local_time<Duration>& st);
+
+// TODO TZDB Investigate the issues in this test, this seems like
+// a design issue of the class.
+//
+// [time.zone.zonedtime.members]/3
+// Effects: After assignment, get_local_time() == lt.
+// This assignment has no effect on the return value of get_time_zone().
+//
+// The test cases describe the issues.
+
+#include <cassert>
+#include <chrono>
+#include <concepts>
+#include <type_traits>
+
+#include "test_macros.h"
+
+namespace cr = std::chrono;
+
+// Tests unique conversions. To make sure the test is does not depend on changes
+// in the database it uses a time zone with a fixed offset.
+static void test_unique() {
+ // common_type_t<duration, seconds> -> duration
+ {
+ using duration = cr::nanoseconds;
+ using sys_time_point = cr::sys_time<duration>;
+ using local_time_point = cr::local_time<duration>;
+ using zoned_time = cr::zoned_time<duration>;
+ zoned_time zt{"Etc/GMT+1", sys_time_point{duration{42}}};
+
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{42}});
+ assert(zt.get_local_time() == local_time_point{duration{42} - cr::hours{1}});
+
+ [[maybe_unused]] std::same_as<zoned_time&> decltype(auto) _ = zt = local_time_point{duration{99}};
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{99} + cr::hours{1}});
+ assert(zt.get_local_time() == local_time_point{duration{99}});
+ }
+ {
+ using duration = cr::microseconds;
+ using sys_time_point = cr::sys_time<duration>;
+ using local_time_point = cr::local_time<duration>;
+ using zoned_time = cr::zoned_time<duration>;
+ zoned_time zt{"Etc/GMT+1", sys_time_point{duration{42}}};
+
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{42}});
+ assert(zt.get_local_time() == local_time_point{duration{42} - cr::hours{1}});
+
+ [[maybe_unused]] std::same_as<zoned_time&> decltype(auto) _ = zt = local_time_point{duration{99}};
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{99} + cr::hours{1}});
+ assert(zt.get_local_time() == local_time_point{duration{99}});
+ }
+ {
+ using duration = cr::milliseconds;
+ using sys_time_point = cr::sys_time<duration>;
+ using local_time_point = cr::local_time<duration>;
+ using zoned_time = cr::zoned_time<duration>;
+ zoned_time zt{"Etc/GMT+1", sys_time_point{duration{42}}};
+
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{42}});
+ assert(zt.get_local_time() == local_time_point{duration{42} - cr::hours{1}});
+
+ [[maybe_unused]] std::same_as<zoned_time&> decltype(auto) _ = zt = local_time_point{duration{99}};
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{99} + cr::hours{1}});
+ assert(zt.get_local_time() == local_time_point{duration{99}});
+ }
+ // common_type_t<seconds, seconds> -> seconds
+ {
+ using duration = cr::seconds;
+ using sys_time_point = cr::sys_time<duration>;
+ using local_time_point = cr::local_time<duration>;
+ using zoned_time = cr::zoned_time<duration>;
+ zoned_time zt{"Etc/GMT+1", sys_time_point{duration{42}}};
+
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{42}});
+ assert(zt.get_local_time() == local_time_point{duration{42} - cr::hours{1}});
+
+ [[maybe_unused]] std::same_as<zoned_time&> decltype(auto) _ = zt = local_time_point{duration{99}};
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == sys_time_point{duration{99} + cr::hours{1}});
+ assert(zt.get_local_time() == local_time_point{duration{99}});
+ }
+ // common_type_t<duration, seconds> -> seconds
+ {
+ using duration = cr::days;
+ using sys_time_point = cr::sys_time<duration>;
+ using local_time_point = cr::local_time<duration>;
+ using zoned_time = cr::zoned_time<duration>;
+ zoned_time zt{"Etc/GMT+1", sys_time_point{duration{42}}};
+
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == cr::sys_seconds{duration{42}});
+ assert(zt.get_local_time() == cr::local_seconds{duration{42} - cr::hours{1}});
+
+ [[maybe_unused]] std::same_as<zoned_time&> decltype(auto) _ = zt = local_time_point{duration{99}};
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == cr::sys_seconds{duration{99} + cr::hours{1}});
+ assert(zt.get_local_time() == cr::local_seconds{duration{99}});
+ }
+ {
+ using duration = cr::weeks;
+ using sys_time_point = cr::sys_time<duration>;
+ using local_time_point = cr::local_time<duration>;
+ using zoned_time = cr::zoned_time<duration>;
+ zoned_time zt{"Etc/GMT+1", sys_time_point{duration{42}}};
+
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == cr::sys_seconds{duration{42}});
+ assert(zt.get_local_time() == cr::local_seconds{duration{42} - cr::hours{1}});
+
+ [[maybe_unused]] std::same_as<zoned_time&> decltype(auto) _ = zt = local_time_point{duration{99}};
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == cr::sys_seconds{duration{99} + cr::hours{1}});
+ assert(zt.get_local_time() == cr::local_seconds{duration{99}});
+ }
+ /* This does not work; due to using __tp_ = __zone_->to_sys(__tp);
+ * Here the ambiguous/non-existent exception can't stream months and years,
+ * leading to a compilation error.
+ {
+ using duration = cr::months;
+ using sys_time_point = cr::sys_time<duration>;
+ using local_time_point = cr::local_time<duration>;
+ using zoned_time = cr::zoned_time<duration>;
+ zoned_time zt{"Etc/GMT+1", sys_time_point{duration{42}}};
+
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == cr::sys_seconds{duration{42}});
+ assert(zt.get_local_time() == cr::local_seconds{duration{42} - cr::hours{1}});
+
+ [[maybe_unused]] std::same_as<zoned_time&> decltype(auto) _ = zt = local_time_point{duration{99}};
+ assert(zt.get_time_zone() == cr::locate_zone("Etc/GMT+1"));
+ assert(zt.get_sys_time() == cr::sys_seconds{duration{99} + cr::hours{1}});
+ assert(zt.get_local_time() == cr::local_seconds{duration{99}});
+ } */
+}
+
+// Tests non-existant conversions.
----------------
ldionne wrote:
```suggestion
// Tests non-existent conversions.
```
https://github.com/llvm/llvm-project/pull/95026
More information about the llvm-branch-commits
mailing list