XNSim/XNCore/XNCore_Function.cpp

58 lines
1.4 KiB
C++
Raw Permalink Normal View History

2025-05-20 15:39:40 +08:00
#include "XNCore_global.h"
namespace XNSim
{
2025-05-20 15:39:40 +08:00
XNTimePoint parseISOTime(const std::string &timeStr)
{
std::tm tm = {};
std::istringstream ss(timeStr);
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
return tp;
}
// 辅助函数:分割字符串
std::vector<std::string> split(const std::string &str, const std::string &delim)
{
std::vector<std::string> tokens;
size_t prev = 0, pos = 0;
do {
pos = str.find(delim, prev);
if (pos == std::string::npos)
pos = str.length();
std::string token = str.substr(prev, pos - prev);
if (!token.empty())
tokens.push_back(token);
prev = pos + delim.length();
} while (pos < str.length() && prev < str.length());
return tokens;
}
// 辅助函数:获取文件名(不含扩展名)
std::string getFileNameWithoutExt(const std::string &path)
{
size_t lastDot = path.find_last_of('.');
if (lastDot != std::string::npos) {
return path.substr(0, lastDot);
}
return path;
}
/**
* @brief
* @param str
* @param defaultValue
* @return
*/
int safe_stoi(const std::string &str, int defaultValue)
{
if (str.empty()) {
return defaultValue;
}
try {
return std::stoi(str);
} catch (const std::exception &) {
return defaultValue;
}
}
} // namespace XNSim