c++ - 将 time_point 转换为字符串的最漂亮方法是什么?

标签 c++ string c++11 c++14 c++-chrono

简单的问题,如何使用尽可能少的代码将 std::chrono::time_point 正确转换为 std::string

注意:我不想将 coutput_time() 一起使用。接受 C++11 和 C++14 解决方案。

最佳答案

仅使用标准库头文件(适用于 >= C++11):

    #include <ctime>
    #include <chrono>
    #include <string>
  
    using sc = std::chrono::system_clock ;
    std::time_t t = sc::to_time_t(sc::now());
    char buf[20];
    strftime(buf, 20, "%d.%m.%Y %H:%M:%S", localtime(&t));
    std::string s(buf);

关于c++ - 将 time_point 转换为字符串的最漂亮方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48217017/

相关文章:

c++ - 支持相等比较的类的 constexpr ID

c++ - 如何从 IDWriteFontFace 创建 IDWriteTextFormat

Python 为单个非 ascii 字符串返回长度为 2

swift - 为什么 string.append(character) 编译失败,错误为 "cannot assign value of type ' ( )' to type ' String' "?

c++ - 删除自身内的 std::function 对象

c++ - 不可能的隐式 move 操作?

c++ - 我如何在我的构造函数中将 weak_ptr 分发给它?

c++ - 使用 std::move() 是不是过早的优化?

c++ - 在 C++ 中删除此指针的行为

java - 调整 ASCII 艺术的高度,使不同的图形打印在同一行