From a19a43a4f73046e72db1843b189c604f0d00c925 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 20 May 2023 16:14:12 +0200 Subject: [PATCH] Codechange: use fmt::format and time conversions over "custom" implementation --- src/CMakeLists.txt | 1 - src/console_cmds.cpp | 6 +-- src/crashlog.cpp | 8 ++-- src/debug.cpp | 10 ++--- src/newgrf_profiling.cpp | 10 +---- src/stdafx.h | 3 -- src/walltime_func.h | 80 ---------------------------------------- 7 files changed, 13 insertions(+), 105 deletions(-) delete mode 100644 src/walltime_func.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b5124200e3..e7230c42e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -512,7 +512,6 @@ add_files( viewport_type.h void_cmd.cpp void_map.h - walltime_func.h water.h water_cmd.cpp water_cmd.h diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 06777da6cc..1df9541384 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -42,7 +42,7 @@ #include "rail.h" #include "game/game.hpp" #include "table/strings.h" -#include "walltime_func.h" +#include "3rdparty/fmt/chrono.h" #include "company_cmd.h" #include "misc_cmd.h" @@ -1460,9 +1460,7 @@ DEF_CONSOLE_CMD(ConGetSysDate) return true; } - char buffer[lengthof("2000-01-02 03:04:05")]; - LocalTime::Format(buffer, lastof(buffer), "%Y-%m-%d %H:%M:%S"); - IConsolePrint(CC_DEFAULT, "System Date: {}", buffer); + IConsolePrint(CC_DEFAULT, "System Date: {:%Y-%m-%d %H:%M:%S}", fmt::localtime(time(nullptr))); return true; } diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 848424d058..19bb8191e6 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -33,7 +33,7 @@ #include "game/game_info.hpp" #include "company_base.h" #include "company_func.h" -#include "walltime_func.h" +#include "3rdparty/fmt/chrono.h" #ifdef WITH_ALLEGRO # include @@ -342,8 +342,7 @@ int CrashLog::CreateFileName(char *filename, const char *filename_last, const ch static std::string crashname; if (crashname.empty()) { - UTCTime::Format(filename, filename_last, "crash%Y%m%d%H%M%S"); - crashname = filename; + crashname = fmt::format("crash{:%Y%m%d%H%M%S}", fmt::gmtime(time(nullptr))); } return seprintf(filename, filename_last, "%s%s%s", with_dir ? _personal_dir.c_str() : "", crashname.c_str(), ext); } @@ -357,7 +356,8 @@ int CrashLog::CreateFileName(char *filename, const char *filename_last, const ch char *CrashLog::FillCrashLog(char *buffer, const char *last) const { buffer += seprintf(buffer, last, "*** OpenTTD Crash Report ***\n\n"); - buffer += UTCTime::Format(buffer, last, "Crash at: %Y-%m-%d %H:%M:%S (UTC)\n"); + std::string temp = fmt::format("Crash at: {:%Y-%m-%d %H:%M:%S} (UTC)\n", fmt::gmtime(time(nullptr))); + buffer = strecpy(buffer, temp.c_str(), last); TimerGameCalendar::YearMonthDay ymd; TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date, &ymd); diff --git a/src/debug.cpp b/src/debug.cpp index 5038025cba..8876fdd021 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -19,7 +19,7 @@ #include "os/windows/win32.h" #endif -#include "walltime_func.h" +#include "3rdparty/fmt/chrono.h" #include "network/network_admin.h" SOCKET _debug_socket = INVALID_SOCKET; @@ -245,13 +245,13 @@ const char *GetDebugString() */ const char *GetLogPrefix() { - static char _log_prefix[24]; + static std::string _log_prefix; if (_settings_client.gui.show_date_in_logs) { - LocalTime::Format(_log_prefix, lastof(_log_prefix), "[%Y-%m-%d %H:%M:%S] "); + _log_prefix = fmt::format("[{:%Y-%m-%d %H:%M:%S}] ", fmt::localtime(time(nullptr))); } else { - *_log_prefix = '\0'; + _log_prefix.clear(); } - return _log_prefix; + return _log_prefix.c_str(); } /** diff --git a/src/newgrf_profiling.cpp b/src/newgrf_profiling.cpp index 6b7d656c54..54ca5f782f 100644 --- a/src/newgrf_profiling.cpp +++ b/src/newgrf_profiling.cpp @@ -12,7 +12,7 @@ #include "string_func.h" #include "console_func.h" #include "spritecache.h" -#include "walltime_func.h" +#include "3rdparty/fmt/chrono.h" #include "timer/timer.h" #include "timer/timer_game_tick.h" @@ -131,13 +131,7 @@ void NewGRFProfiler::Abort() */ std::string NewGRFProfiler::GetOutputFilename() const { - char timestamp[16] = {}; - LocalTime::Format(timestamp, lastof(timestamp), "%Y%m%d-%H%M"); - - char filepath[MAX_PATH] = {}; - seprintf(filepath, lastof(filepath), "%sgrfprofile-%s-%08X.csv", FiosGetScreenshotDir(), timestamp, BSWAP32(this->grffile->grfid)); - - return std::string(filepath); + return fmt::format("{}grfprofile-{%Y%m%d-%H%M}-{:08X}.csv", FiosGetScreenshotDir(), fmt::localtime(time(nullptr)), BSWAP32(this->grffile->grfid)); } /* static */ uint32 NewGRFProfiler::FinishAll() diff --git a/src/stdafx.h b/src/stdafx.h index 74bd6601f0..fcc0a82c2b 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -93,7 +93,6 @@ /* Warn about functions using 'printf' format syntax. First argument determines which parameter * is the format string, second argument is start of values passed to printf. */ # define WARN_FORMAT(string, args) __attribute__ ((format (printf, string, args))) -# define WARN_TIME_FORMAT(string) __attribute__ ((format (strftime, string, 0))) # if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7) # define FINAL final # else @@ -129,7 +128,6 @@ # define NORETURN # define CDECL # define WARN_FORMAT(string, args) -# define WARN_TIME_FORMAT(string) # define FINAL # define FALLTHROUGH # include @@ -172,7 +170,6 @@ # define CDECL _cdecl # define WARN_FORMAT(string, args) -# define WARN_TIME_FORMAT(string) # define FINAL final /* fallthrough attribute, VS 2017 */ diff --git a/src/walltime_func.h b/src/walltime_func.h deleted file mode 100644 index 216a726662..0000000000 --- a/src/walltime_func.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * This file is part of OpenTTD. - * OpenTTD 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, version 2. - * OpenTTD 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 OpenTTD. If not, see . - */ - - /** @file walltime_func.h Functionality related to the time of the clock on your wall. */ - -#ifndef WALLTIME_FUNC_H -#define WALLTIME_FUNC_H - -#include - -/** Helper for safely converting a std::time_t to a local time std::tm using localtime_s. */ -struct LocalTimeToStruct { - static inline std::tm ToTimeStruct(std::time_t time_since_epoch) - { - std::tm time = {}; -#ifdef _WIN32 - /* Windows has swapped the parameters around for localtime_s. */ - localtime_s(&time, &time_since_epoch); -#else - localtime_r(&time_since_epoch, &time); -#endif - return time; - } -}; - -/** Helper for safely converting a std::time_t to a UTC time std::tm using gmtime_s. */ -struct UTCTimeToStruct { - static inline std::tm ToTimeStruct(std::time_t time_since_epoch) - { - std::tm time = {}; -#ifdef _WIN32 - /* Windows has swapped the parameters around for gmtime_s. */ - gmtime_s(&time, &time_since_epoch); -#else - gmtime_r(&time_since_epoch, &time); -#endif - return time; - } -}; - -/** - * Container for wall clock time related functionality not directly provided by C++. - * @tparam T The type of the time-to-struct implementation class. - */ -template -struct Time { - /** - * Format the current time with the given strftime format specifiers. - * @param buffer The buffer to write the time string to. - * @param last The last element in the buffer. - * @param format The format according to strftime format specifiers. - * @return The number of characters that were written to the buffer. - */ - static inline size_t Format(char *buffer, const char *last, const char *format) NOACCESS(2) WARN_TIME_FORMAT(3) - { - std::tm time_struct = T::ToTimeStruct(time(nullptr)); -#ifndef _MSC_VER - /* GCC bug #39438; unlike for printf where the appropriate attribute prevent the - * "format non literal" warning, that does not happen for strftime. Even though - * format warnings will be created for invalid strftime formats. */ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-nonliteral" -#endif /* _MSC_VER */ - return strftime(buffer, last - buffer + 1, format, &time_struct); -#ifndef _MSC_VER -#pragma GCC diagnostic pop -#endif /* _MSC_VER */ - } -}; - -/** Wall clock time functionality using the local time zone. */ -using LocalTime = Time; -/** Wall clock time functionality using the UTC time zone. */ -using UTCTime = Time; - -#endif