用标准库获取微秒(micro)精度的时间型字符串

本文介绍如何利用标准库获取具有微秒精度且易读的时间字符串。
摘要由CSDN通过智能技术生成

因为某些原因,需要经常获取高精度的可读的时间型字符串,能用标准库获取的话,更好。于是就有了下面的代码:

#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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值