获取 unix 时间戳
std::time_t result = std::time(nullptr);
或者:
const auto p1 = std::chrono::system_clock::now();
std::cout << "seconds since epoch: "
<< std::chrono::duration_cast<std::chrono::seconds>(
p1.time_since_epoch()).count() << '\n';
time_t
转换为int或者string :
std::to_string((int)std::time(nullptr))
参考:
Get Unix timestamp with C++
获取格式化时间戳字符串(string)
auto t = std::time(nullptr);
std::ostringstream os;
os << std::put_time(std::localtime(&t), "%Y_%m_%dT%H_%M_%S%z%Z");
std::string timestamp = os.str();
- 输出timestamp:
2022_11_03T23_09_37+0800CST
- 综合参考:
Converting ostream into standard string
Convert timestamp to the formatted date time using C++
std::basic_ostream
std::put_time