fmtlib的日期相关格式化在 chrono.h文件
主要API
支持三种类型格式化
std::chrono::duration
std::chrono::time_point
std::tm
std::tm fmt::localtime(std::time_t time)
比 std::localtime 而言,线程安全
std::tm fmt::gmtime(std::time_t time)
比 std::gmtime 而言,线程安全
测试代码:
int main(int argc, char** argv)
{
std::time_t t = std::time(nullptr);
// Prints "The date is 2020-11-07." (with the current date):
fmt::print("The date is {:%Y-%m-%d}.\n", fmt::localtime(t));
using namespace std::literals::chrono_literals;
// Prints "Default format: 42s 100ms":
fmt::print("Default format: {} {}\n", 42s, 100ms);
// Prints "strftime-like format: 03:15:30":
fmt::print("strftime-like format: {:%H:%M:%S}\n", 3h + 15min + 30s);
return 0;
}
运行结果