[libcxx-commits] [libcxx] [libc++][chrono] implements UTC clock. (PR #90393)

Matt Stephanson via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 16 11:29:27 PDT 2024


================
@@ -0,0 +1,115 @@
+//===----------------------------------------------------------------------===//
+//
+// 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-incomplete-tzdb
+// XFAIL: availability-tzdb-missing
+
+// <chrono>
+//
+// class utc_clock;
+
+// static sys_time<common_type_t<_Duration, seconds>>
+// to_sys(const utc_time<_Duration>& __time);
+
+#include <chrono>
+#include <cassert>
+#include <fstream>
+#include <string>
+#include <string_view>
+
+#include "test_macros.h"
+#include "assert_macros.h"
+#include "concat_macros.h"
+#include "filesystem_test_helper.h"
+#include "test_tzdb.h"
+
+scoped_test_env env;
+[[maybe_unused]] const std::filesystem::path dir = env.create_dir("zoneinfo");
+const std::filesystem::path tzdata               = env.create_file("zoneinfo/tzdata.zi");
+const std::filesystem::path leap_seconds         = env.create_file("zoneinfo/leap-seconds.list");
+
+std::string_view std::chrono::__libcpp_tzdb_directory() {
+  static std::string result = dir.string();
+  return result;
+}
+
+static void write(std::string_view input) {
+  static int version = 0;
+
+  std::ofstream f{tzdata};
+  f << "# version " << version++ << '\n';
+  std::ofstream{leap_seconds}.write(input.data(), input.size());
+}
+
+template <class Duration>
+static void test_leap_seconds(std::chrono::utc_time<Duration> time, std::chrono::sys_time<Duration> expected) {
+  auto result = std::chrono::utc_clock::to_sys(time);
+  TEST_REQUIRE(result == expected,
+               TEST_WRITE_CONCATENATED("\nExpected output ", expected, "\nActual output   ", result, '\n'));
+}
+
+// Note at the time of writing all leap seconds are positive. This test uses
+// fake data to test the behaviour of negative leap seconds.
+int main(int, const char**) {
+  using namespace std::literals::chrono_literals;
+
+  // Use small values for simplicity. The dates are seconds since 1.1.1970.
+  write(
+      R"(
+1 10
----------------
MattStephanson wrote:

I'm not sure in what sense the tzdb "supports" it. There's a file https://data.iana.org/time-zones/tzdb/leapseconds that only indicates if the leap second is positive or negative. It's true that leap-seconds.list could represent a larger leap second, but on the other hand:
1. [ITU-R TF.460-6](https://www.itu.int/dms_pubrec/itu-r/rec/tf/R-REC-TF.460-6-200202-I!!PDF-E.pdf), in sections 2.1 and 2.2, refers to "_A_ [...] leap-second" (emphasis added). The remainder of section 2.2, and Annex 3, make clear that these have a duration of one second.
2. [[time.zone.leap.members]/2](https://eel.is/c++draft/time.zone.leap#members-2) specifies that `leap_second::value()` returns `+1` or `-1`. [LWG3359](https://cplusplus.github.io/LWG/issue3359), which added this member, was only concerned with representing negative leap seconds.
3. Since at least C99/C++11/IEEE 1003.1-2008, `tm::tm_sec` is shown to have the range [0, 60].
4. Similarly, the specification for [`strptime`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html)/[`strftime`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html) says that [0, 60] is the valid range for `%S`.

Given the number of other things that would be broken, I'm not sure it's fair to say that this is "supported" and "must" be handled.

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


More information about the libcxx-commits mailing list