asctime_r
getopt_long
getpwuid
+ localtime_r
posix_fallocate
realpath
setenv
// 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
#include <core/wincompat.hpp>
#include <util/file.hpp>
+#include <util/time.hpp>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
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 {
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(×tamp, &result)) {
- return result;
- } else {
- return std::nullopt;
- }
-}
-
std::string
make_relative_path(const std::string& base_dir,
const std::string& actual_cwd,
;
}
-// 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:
}
} // namespace Win32Util
-
-struct tm*
-localtime_r(time_t* clock, struct tm* result)
-{
- struct tm* p = localtime(clock);
- if (p) {
- *result = *p;
- }
- return p;
-}
# 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"
#include <util/path.hpp>
#include <util/process.hpp>
#include <util/string.hpp>
+#include <util/time.hpp>
#include "third_party/fmt/core.h"
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 {
#include <fmtmacros.hpp>
#include <util/TextTable.hpp>
#include <util/string.hpp>
+#include <util/time.hpp>
#include <algorithm>
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);
#include "Context.hpp"
#include "Logging.hpp"
#include "Stat.hpp"
-#include "Util.hpp"
#include "Win32Util.hpp"
#include "execute.hpp"
#include "macroskip.hpp"
#include <fmtmacros.hpp>
#include <util/file.hpp>
#include <util/string.hpp>
+#include <util/time.hpp>
#ifdef INODE_CACHE_SUPPORTED
# include "InodeCache.hpp"
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;
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;
path.cpp
process.cpp
string.cpp
+ time.cpp
zstd.cpp
)
--- /dev/null
+// 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(×tamp, &result)) {
+ return result;
+ }
+#else
+ struct tm* result = ::localtime(×tamp);
+ if (result) {
+ return *result;
+ }
+#endif
+ return std::nullopt;
+}
+
+} // namespace util
--- /dev/null
+// 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