From: Joel Rosdahl Date: Mon, 17 Jul 2023 13:46:19 +0000 (+0200) Subject: refactor: Move Util::localtime to util X-Git-Tag: v4.9~107 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f657af6a3b5702326186801eefcf8ff4777dc7eb;p=thirdparty%2Fccache.git refactor: Move Util::localtime to util --- diff --git a/cmake/GenerateConfigurationFile.cmake b/cmake/GenerateConfigurationFile.cmake index 4fac7b032..dc6ce3d72 100644 --- a/cmake/GenerateConfigurationFile.cmake +++ b/cmake/GenerateConfigurationFile.cmake @@ -28,6 +28,7 @@ set(functions asctime_r getopt_long getpwuid + localtime_r posix_fallocate realpath setenv diff --git a/cmake/config.h.in b/cmake/config.h.in index 5a106e6fe..2d23fcbf1 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -84,6 +84,9 @@ // Define if you have the 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 diff --git a/src/Logging.cpp b/src/Logging.cpp index 25bdea1a7..4c3bfc535 100644 --- a/src/Logging.cpp +++ b/src/Logging.cpp @@ -28,6 +28,7 @@ #include #include +#include #ifdef HAVE_UNISTD_H # include @@ -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 { diff --git a/src/Util.cpp b/src/Util.cpp index 8282835a8..8118dc656 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -363,18 +363,6 @@ is_ccache_executable(const std::string_view path) return util::starts_with(name, "ccache"); } -std::optional -localtime(std::optional time) -{ - time_t timestamp = time ? time->sec() : util::TimePoint::now().sec(); - tm result; - if (localtime_r(×tamp, &result)) { - return result; - } else { - return std::nullopt; - } -} - std::string make_relative_path(const std::string& base_dir, const std::string& actual_cwd, diff --git a/src/Util.hpp b/src/Util.hpp index 01c826a55..755ab2b5e 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -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 localtime(std::optional time = {}); - // Construct a normalized native path. // // Example: diff --git a/src/Win32Util.cpp b/src/Win32Util.cpp index 7e1bdb601..a32211ff2 100644 --- a/src/Win32Util.cpp +++ b/src/Win32Util.cpp @@ -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; -} diff --git a/src/Win32Util.hpp b/src/Win32Util.hpp index a246381fc..03f2fe26c 100644 --- a/src/Win32Util.hpp +++ b/src/Win32Util.hpp @@ -24,8 +24,6 @@ # include -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" diff --git a/src/ccache.cpp b/src/ccache.cpp index d581c1de4..4a9882547 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #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 { diff --git a/src/core/Statistics.cpp b/src/core/Statistics.cpp index f3b1d72de..df776d494 100644 --- a/src/core/Statistics.cpp +++ b/src/core/Statistics.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -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); diff --git a/src/hashutil.cpp b/src/hashutil.cpp index b42289da0..6312156fa 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -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 #include #include +#include #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; diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 1739c1daf..0bac8faff 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -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 index 000000000..067b07052 --- /dev/null +++ b/src/util/time.cpp @@ -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 +localtime(std::optional time) +{ + time_t timestamp = time ? time->sec() : util::TimePoint::now().sec(); +#ifdef HAVE_LOCALTIME_R + struct tm result; + if (localtime_r(×tamp, &result)) { + return result; + } +#else + struct tm* result = ::localtime(×tamp); + 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 index 000000000..3f263393f --- /dev/null +++ b/src/util/time.hpp @@ -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 + +#include +#include + +namespace util { + +// Thread-safe version of `localtime(3)`. If `time` is not specified the current +// time of day is used. +std::optional localtime(std::optional time = {}); + +} // namespace util