]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::localtime to util
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 17 Jul 2023 13:46:19 +0000 (15:46 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 18 Jul 2023 19:37:15 +0000 (21:37 +0200)
13 files changed:
cmake/GenerateConfigurationFile.cmake
cmake/config.h.in
src/Logging.cpp
src/Util.cpp
src/Util.hpp
src/Win32Util.cpp
src/Win32Util.hpp
src/ccache.cpp
src/core/Statistics.cpp
src/hashutil.cpp
src/util/CMakeLists.txt
src/util/time.cpp [new file with mode: 0644]
src/util/time.hpp [new file with mode: 0644]

index 4fac7b032ccd72671a5d38496637a39dd39dabdb..dc6ce3d726332a53a62299b76da4240dde8dd0bb 100644 (file)
@@ -28,6 +28,7 @@ set(functions
     asctime_r
     getopt_long
     getpwuid
+    localtime_r
     posix_fallocate
     realpath
     setenv
index 5a106e6fe81367bd39270554fc54cf9386fcc50d..2d23fcbf1f3b4555b86d4551f90d000d86795b31 100644 (file)
@@ -84,6 +84,9 @@
 // Define if you have the <linux/fs.h> header file.
 #cmakedefine HAVE_LINUX_FS_H
 
+// Define if you have the "localtime_r" function.
+#cmakedefine HAVE_LOCALTIME_R
+
 // Define if the system has the type "long long".
 #cmakedefine HAVE_LONG_LONG
 
index 25bdea1a79359af59d6e2f64cb93b84c8bca5dcb..4c3bfc535db75dcc7ee92b5c8b93202f228f13c5 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <core/wincompat.hpp>
 #include <util/file.hpp>
+#include <util/time.hpp>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
@@ -83,7 +84,7 @@ do_log(std::string_view message, bool bulk)
   if (!bulk) {
     char timestamp[100];
     auto now = util::TimePoint::now();
-    auto tm = Util::localtime(now);
+    auto tm = util::localtime(now);
     if (tm) {
       strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", &*tm);
     } else {
index 8282835a886c33001a3ad675b75cfa9a4cbf8adb..8118dc656a32528b5c998aee19419bc4af5b5e3d 100644 (file)
@@ -363,18 +363,6 @@ is_ccache_executable(const std::string_view path)
   return util::starts_with(name, "ccache");
 }
 
-std::optional<tm>
-localtime(std::optional<util::TimePoint> time)
-{
-  time_t timestamp = time ? time->sec() : util::TimePoint::now().sec();
-  tm result;
-  if (localtime_r(&timestamp, &result)) {
-    return result;
-  } else {
-    return std::nullopt;
-  }
-}
-
 std::string
 make_relative_path(const std::string& base_dir,
                    const std::string& actual_cwd,
index 01c826a55ea5e32e12c6f500361b3f9d1cb0c5db..755ab2b5e176d6830b3d5fd69cb35e59a9569aeb 100644 (file)
@@ -93,10 +93,6 @@ is_dir_separator(char ch)
     ;
 }
 
-// Thread-safe version of `localtime(3)`. If `time` is not specified the current
-// time of day is used.
-std::optional<tm> localtime(std::optional<util::TimePoint> time = {});
-
 // Construct a normalized native path.
 //
 // Example:
index 7e1bdb6011ff612a05304d085962579a228dd2aa..a32211ff21ed0a30f0ed95769bfec7e84b681a69 100644 (file)
@@ -134,13 +134,3 @@ get_last_ntstatus()
 }
 
 } // namespace Win32Util
-
-struct tm*
-localtime_r(time_t* clock, struct tm* result)
-{
-  struct tm* p = localtime(clock);
-  if (p) {
-    *result = *p;
-  }
-  return p;
-}
index a246381fcc40c62122f817d867e2649d16b083e4..03f2fe26cb7809aeea28b4c698ad497d08e8a8c0 100644 (file)
@@ -24,8 +24,6 @@
 
 #  include <string>
 
-struct tm* localtime_r(time_t* _clock, struct tm* _result);
-
 namespace Win32Util {
 
 // Add ".exe" suffix to `program` if it doesn't already end with ".exe", ".bat"
index d581c1de4dfc81b3a100618460e6ca00717dbc92..4a9882547fa6b9aa03f0a6a1824b3c30c67e1931 100644 (file)
@@ -61,6 +61,7 @@
 #include <util/path.hpp>
 #include <util/process.hpp>
 #include <util/string.hpp>
+#include <util/time.hpp>
 
 #include "third_party/fmt/core.h"
 
@@ -196,7 +197,7 @@ prepare_debug_path(const std::string& debug_dir,
   fs::create_directories(Util::dir_name(prefix));
 
   char timestamp[100];
-  const auto tm = Util::localtime(time_of_invocation);
+  const auto tm = util::localtime(time_of_invocation);
   if (tm) {
     strftime(timestamp, sizeof(timestamp), "%Y%m%d_%H%M%S", &*tm);
   } else {
index f3b1d72de6590a81291fb28baf9bfd75cff8337d..df776d494f541f967d141298f75875bd4faba573 100644 (file)
@@ -24,6 +24,7 @@
 #include <fmtmacros.hpp>
 #include <util/TextTable.hpp>
 #include <util/string.hpp>
+#include <util/time.hpp>
 
 #include <algorithm>
 
@@ -263,7 +264,7 @@ format_timestamp(const util::TimePoint& value)
   if (value.sec() == 0) {
     return "never";
   } else {
-    const auto tm = Util::localtime(value);
+    const auto tm = util::localtime(value);
     char buffer[100] = "?";
     if (tm) {
       strftime(buffer, sizeof(buffer), "%c", &*tm);
index b42289da0ea07e3a6b6fb553b44d808f8d764dbc..6312156fa17b824d8ff4ee0af0c53f94041cb680 100644 (file)
@@ -23,7 +23,6 @@
 #include "Context.hpp"
 #include "Logging.hpp"
 #include "Stat.hpp"
-#include "Util.hpp"
 #include "Win32Util.hpp"
 #include "execute.hpp"
 #include "macroskip.hpp"
@@ -33,6 +32,7 @@
 #include <fmtmacros.hpp>
 #include <util/file.hpp>
 #include <util/string.hpp>
+#include <util/time.hpp>
 
 #ifdef INODE_CACHE_SUPPORTED
 #  include "InodeCache.hpp"
@@ -269,7 +269,7 @@ hash_source_code_file(const Context& ctx,
     LOG("Found __DATE__ in {}", path);
 
     hash.hash_delimiter("date");
-    auto now = Util::localtime();
+    auto now = util::localtime();
     if (!now) {
       result.insert(HashSourceCode::error);
       return result;
@@ -297,7 +297,7 @@ hash_source_code_file(const Context& ctx,
       return result;
     }
 
-    auto modified_time = Util::localtime(stat.mtime());
+    auto modified_time = util::localtime(stat.mtime());
     if (!modified_time) {
       result.insert(HashSourceCode::error);
       return result;
index 1739c1dafeb3e8b1ee8a285355a2851f0e96bfc9..0bac8faff990158087f050d2ec55c199361043b2 100644 (file)
@@ -12,6 +12,7 @@ set(
   path.cpp
   process.cpp
   string.cpp
+  time.cpp
   zstd.cpp
 )
 
diff --git a/src/util/time.cpp b/src/util/time.cpp
new file mode 100644 (file)
index 0000000..067b070
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright (C) 2023 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "time.hpp"
+
+namespace util {
+
+std::optional<tm>
+localtime(std::optional<util::TimePoint> time)
+{
+  time_t timestamp = time ? time->sec() : util::TimePoint::now().sec();
+#ifdef HAVE_LOCALTIME_R
+  struct tm result;
+  if (localtime_r(&timestamp, &result)) {
+    return result;
+  }
+#else
+  struct tm* result = ::localtime(&timestamp);
+  if (result) {
+    return *result;
+  }
+#endif
+  return std::nullopt;
+}
+
+} // namespace util
diff --git a/src/util/time.hpp b/src/util/time.hpp
new file mode 100644 (file)
index 0000000..3f26339
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright (C) 2023 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include <util/TimePoint.hpp>
+
+#include <ctime>
+#include <optional>
+
+namespace util {
+
+// Thread-safe version of `localtime(3)`. If `time` is not specified the current
+// time of day is used.
+std::optional<tm> localtime(std::optional<util::TimePoint> time = {});
+
+} // namespace util