You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2013(V120)中std::put_time运行异常问题求助

问题解决:VS2013下含毫秒时间戳的日志文件名生成异常修复

异常根源

Visual Studio 2013(v120)并未实现C++11标准中的std::put_time函数,你的代码中调用该函数直接导致了运行时异常。

修复方案

改用C标准库的strftime函数完成日期时间格式化(VS2013完全支持),再拼接毫秒部分,修改后的代码如下:

#include <ctime>
#include <sstream>
#include <iomanip>
#include <chrono>

std::string Logger::GetTimeStampWithMilliSeconds() const
{
    const auto now = std::chrono::system_clock::now();
    // 提取当前时间的毫秒部分
    const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;

    const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now);
    struct tm tm {};
    localtime_s(&tm, &nowAsTimeT);

    // 用strftime格式化日期时间到字符缓冲区
    char timeBuf[20]; // 足够容纳"YYYY-MM-DD HH:MM:SS"格式的字符串
    strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", &tm);

    std::stringstream nowSs;
    nowSs << timeBuf << '.' << std::setfill('0') << std::setw(3) << ms.count();
    return nowSs.str();
}

关键说明

  • strftime是兼容性极强的标准日期格式化函数,完美适配VS2013环境;
  • 字符缓冲区timeBuf长度设为20,足以覆盖%Y-%m-%d %H:%M:%S格式的输出(含字符串结束符);
  • 毫秒部分的补零逻辑保持不变,确保输出格式为YYYY-MM-DD HH:MM:SS.xxx(xxx为三位毫秒数)。

内容的提问来源于stack exchange,提问作者pesala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 07:50:17