c++ - strftime 不能正确显示年份

标签 c++ datetime time

我有下面的代码:

const char* timeformat = "%Y-%m-%d %H:%M:%S";
const int timelength = 20;
char timecstring[timelength];

strftime(timecstring, timelength, timeformat, currentstruct);

cout << "timecstring is: " << timecstring << "\n";

currentstruct 是一个 tm*。 cout 以正确的格式给我日期,但年份不是 2010 年,而是 3910。我知道与从 1900 年开始的年份有关,但我不确定如何让 strftime 识别这个而不是将 1900 添加到那里的 2010 的值,任何人都可以帮忙。

问候

保罗

最佳答案

根据定义,struct tmtm_year 字段是自 1900 年以来的年数。处理 struct tm< 的每一段代码 期望是这种情况。您不能将 tm_year 设置为 2010 年并期望代码神奇地确定您真正指的是 2010 年而不是 3910 年。

tm_year 设置为 110,strftime 将起作用。

关于c++ - strftime 不能正确显示年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2578866/

相关文章:

c++ - 串口ReadFile接收重复数据

.localize 和 tzinfo 之间的 Python 日期时间差异

Python 文本文件之间的日期/时间转换

c - 这个基本算法O(n)怎么样?

PHP - 将 time() 调整为可被 5 整除

没有动态调度开销的基类抽象方法的 C++ 习惯用法?

c++ - VS2010编译错误。 stdio.h(378) : fatal error C1003: error count exceeds 100; stopping compilation

c++ - poly2tri getTriangles() 和 getMap() 有什么区别?

java - 使用 Java 8 DateTimeFormatter 和西类牙月份名称进行解析

matlab - 如何在 MATLAB 或 Octave 中在 x 轴上绘制日期时间