c++11 输出时间
2015-09-09 18:50
429 查看
C++11中输出当前时间最直接的方法:
这种方法可以输出年月日时分秒,不过却不能输出毫秒,如果要输出毫秒需要用下面的方法:
将绝对时间转换为标准时间字符串的方法:
std::time_t t2 = std::time(nullptr); cout << std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S") << "." << msecs << endl;
这种方法可以输出年月日时分秒,不过却不能输出毫秒,如果要输出毫秒需要用下面的方法:
auto n = chrono::system_clock::now(); auto m = n.time_since_epoch(); auto diff = duration_cast<milliseconds>(ms).count(); auto const msecs = diff % 1000; std::time_t t = system_clock::to_time_t(n1); cout << std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S") << "." << msecs << endl;
将绝对时间转换为标准时间字符串的方法:
#include <string> #include <chrono> #include <cinttypes> #include <ctime> #include <sstream> #include <iomanip> std::string millisecond_to_str(std::int64_t milliseconds) { std::chrono::milliseconds ms(milliseconds); std::chrono::time_point<std::chrono::high_resolution_clock, std::chrono::milliseconds> t1(ms); std::time_t t = std::chrono::system_clock::to_time_t(t1); std::stringstream ss; auto const msecs = ms.count() % 1000; ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H.%M.%S") << "." << msecs; return ss.str(); }
相关文章推荐
- C++中引用(&)的用法和应用实例
- C/C++ 指针数组、二维数组
- C++ inline weak symbol and so on
- 2015.09.08 C++笔记
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(1)
- 【more effective c++读书笔记】【第5章】技术(7)——让函数根据一个以上的对象类型来决定如何虚化(1)
- C语言获取时间
- TMS320C28x C/C++ Data Type
- MySql C++调用库Connector/c++编译 和 接口封装【二】Connector/c++编译
- C++11 变长模版和完美转发实例代码
- C++智能指针详解
- C语言之#和##
- C++ 学习笔记
- 递归分治--汉诺塔问题
- C++ 基础知识回顾(string基础、智能指针、迭代器、容器类)
- 【leetcod】Product of Array Except Self -C++
- C语言篇——指针(一)
- 关于&的思考
- C++STL之函数对象及谓词
- c++ 成员变量初始化