Files
blackbird/gateway/inc/LocalTime.hpp
T
2026-07-21 23:03:27 +08:00

68 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <chrono>
#include <string>
#include <format>
#include <optional>
#include <sstream>
namespace qing {
class LocalTime {
public:
// 获取当前本地时间字符串
static std::string getCurrent(const std::string& format = "%Y-%m-%d %H:%M:%S") {
using namespace std::chrono;
auto now = system_clock::now();
auto zone = current_zone();
zoned_time zt{zone, now};
return std::vformat(format, std::make_format_args(zt));
}
// 解析时间字符串为 system_clock::time_point(本地时间 → UTC)
static std::optional<std::chrono::system_clock::time_point> parse(
const std::string& str,
const std::string& format = "%Y-%m-%d %H:%M:%S"
) {
using namespace std::chrono;
std::istringstream iss{str};
local_time<seconds> lt;
iss >> std::chrono::parse(format, lt); // ✅ 修复
if (iss.fail()) {
return std::nullopt;
}
auto zone = current_zone();
zoned_time zt{zone, lt};
return zt.get_sys_time();
}
// ----- 过期判断 -----
static bool isExpired(const std::chrono::system_clock::time_point& tp) {
using namespace std::chrono;
return system_clock::now() > tp;
}
template <typename Rep, typename Period>
static bool isExpired(const std::chrono::system_clock::time_point& tp,
const std::chrono::duration<Rep, Period>& gracePeriod) {
using namespace std::chrono;
return system_clock::now() > tp + gracePeriod;
}
static bool isExpired(const std::string& timeStr,
const std::string& format = "%Y-%m-%d %H:%M:%S") {
auto parsed = parse(timeStr, format);
if (!parsed) return true;
return isExpired(*parsed);
}
template <typename Rep, typename Period>
static bool isExpired(const std::string& timeStr,
const std::chrono::duration<Rep, Period>& gracePeriod,
const std::string& format = "%Y-%m-%d %H:%M:%S") {
auto parsed = parse(timeStr, format);
if (!parsed) return true;
return isExpired(*parsed, gracePeriod);
}
};
}