From 2be0e9631729cf85e79b23fb921209182e0907be Mon Sep 17 00:00:00 2001 From: taynpg Date: Tue, 14 Jan 2025 12:31:56 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=B8=80=E4=B8=AA=E6=96=87=E4=BB=B6=E5=90=8D=E7=9A=84?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E6=97=B6=E9=97=B4=E5=90=8E=E7=BC=80=E7=9A=84?= =?UTF-8?q?=E6=96=B0=E5=90=8D=E5=AD=97=E5=87=BD=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/of_str.h | 4 ++++ src/of_str.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/of_str.h b/include/of_str.h index 0a6698a..fba8eb0 100644 --- a/include/of_str.h +++ b/include/of_str.h @@ -15,6 +15,10 @@ public: static ofString replace(const ofString& str, const ofString& from, const ofString& to); static std::vector split(const ofString& input, const ofString& delimiter); static ofString trim(const ofString& input); + /// @brief 获取一个添加当前日期后缀的字符串(到秒,如 some.txt => some_20150115151221.txt)。 + /// @param name + /// @return + static ofString get_ofile_name(const ofString& name); }; }; // namespace ofen diff --git a/src/of_str.cpp b/src/of_str.cpp index 8a8f0ea..63e6fb5 100644 --- a/src/of_str.cpp +++ b/src/of_str.cpp @@ -1,4 +1,9 @@ #include "of_str.h" +#include +#include +#include +#include +#include namespace ofen { COfStr::COfStr() @@ -40,4 +45,27 @@ ofString COfStr::trim(const ofString& input) size_t end = input.find_last_not_of(ofT(" \t\n\r\f\v")); return input.substr(start, end - start + 1); } +ofString COfStr::get_ofile_name(const ofString& name) +{ + auto now = std::chrono::system_clock::now(); + std::time_t now_c = std::chrono::system_clock::to_time_t(now); + std::tm local_tm = *std::localtime(&now_c); + + std::ostringstream oss; + oss << std::put_time(&local_tm, "%Y%m%d%H%M%S"); + ofString timestamp; +#ifdef UNICODE_OFSTR + std::string temp = oss.str(); + timestamp.assign(temp.begin(), temp.end()); +#else + timestamp = oss.str(); +#endif + size_t dot_pos = name.find_last_of(ofT('.')); + if (dot_pos == ofString::npos) { + return name + ofT("_") + timestamp; + } + ofString base_name = name.substr(0, dot_pos); + ofString extension = name.substr(dot_pos); + return base_name + ofT("_") + timestamp + extension; +} } // namespace ofen