因为某些原因,需要经常获取高精度的可读的时间型字符串,能用标准库获取的话,更好。于是就有了下面的代码:
#include <cstdint>
#include <chrono>
#include <ctime>
#include <string>
#include <iostream>
//标准库下,几种时间类型的转换.
void std_time_convert()
{
char tmBuf[64] = { 0 };
//(操作系统精度的)时间点.
std::chrono::time_point<std::chrono::system_clock> tpSys = std::chrono::system_clock::now();
//(各种精度的)时间点=>(秒精度的)Unix时间戳
std::time_t unixTimestamp = std::chrono::system_clock::to_time_t(tpSys);
//(秒精度的)Unix时间戳=>字符串
std::strftime(tmBuf, sizeof(tmBuf), "%Y-%m-%d %H:%M:%S", std::localtime(&unixTimestamp));
std::cout << tmBuf << std::endl;
//(操作系统精度的)时间点=>秒的小数位
//注: _XTIME_TICKS_PER_TIME_T,是我翻看std::chrono::system_clock::to_time_t的源码时看到的.
int decimalFractionSys = (int)((std::uint64_t)(tpSys.time_since_epoch().count() % _XTIME_TICKS_PER_TIME_T)