]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/utime_fmt.h: replace deprecated fmt::localtime() with localtime_r() 63800/head
authorKefu Chai <tchaikov@gmail.com>
Sun, 8 Jun 2025 12:11:21 +0000 (20:11 +0800)
committerKefu Chai <tchaikov@gmail.com>
Sun, 8 Jun 2025 12:19:15 +0000 (20:19 +0800)
Replace fmt::localtime() with localtime_r() to fix build failure with
fmt 11.2.0, where fmt::localtime() was deprecated in favor of std::localtime.

The deprecated function causes build errors when treating warnings as errors:

```
In file included from /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/LogEntry.cc:7:
In file included from /home/jenkins-build/build/workspace/ceph-pull-requests/src/common/LogEntry.h:22:
/home/jenkins-build/build/workspace/ceph-pull-requests/src/include/utime_fmt.h:37:25: error: 'localtime' is deprecated [-Werror,-Wdeprecated-declarations]
   37 |     auto aslocal = fmt::localtime(utime.sec());
      |                         ^
/home/jenkins-build/build/workspace/ceph-pull-requests/src/fmt/include/fmt/chrono.h:538:1: note: 'localtime' has been explicitly marked deprecated here
  538 | FMT_DEPRECATED inline auto localtime(std::time_t time) -> std::tm {
      | ^
/home/jenkins-build/build/workspace/ceph-pull-requests/src/fmt/include/fmt/base.h:207:28: note: expanded from macro 'FMT_DEPRECATED'
  207 | #  define FMT_DEPRECATED [[deprecated]]
      |                            ^
1 error generated.
```

Using localtime_r() provides thread-safe behavior with user-provided
buffers instead of static storage, addressing the deprecation while
maintaining functionality.

See: https://github.com/fmtlib/fmt/releases/tag/11.2.0

This should address the build failure, and should prepare us for
switching to fmt 11.2.0, which in turn includes a bug fix preventing us
from building with Clang 20.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/include/utime_fmt.h

index 9d49d1bf050f7c122b816644ad85d8a802a07c92..841f68d08d01e6576018e065f5ef4775a2e6e2a7 100644 (file)
@@ -34,7 +34,11 @@ struct fmt::formatter<utime_t> {
     // this looks like an absolute time.
     // conform to http://en.wikipedia.org/wiki/ISO_8601
     // (unless short_format is set)
-    auto aslocal = fmt::localtime(utime.sec());
+    std::time_t time = utime.sec();
+    std::tm aslocal;
+    if (!localtime_r(&time, &aslocal)) {
+      throw fmt::format_error("time_t value out of range");
+    }
     if (short_format) {
       return fmt::format_to(ctx.out(), "{:%FT%T}.{:03}", aslocal,
                            utime.usec() / 1000);