Pab*_*ggi 7 c++ gcc clang libstdc++
当格式包含“%y”或“%Y”时,std::get_time 的行为方式相同,在这两种情况下,它都会尝试读取四位数年份。我做错了什么还是一个错误?
示例代码:
#include <iostream>
#include <iomanip>
void testDate(const char *format,const char *date)
{
std::istringstream ds(date);
std::tm tm = {};
ds >> std::get_time(&tm,format);
std::cout<<date<<" parsed using "<<format<<" -> Year: "<<tm.tm_year+1900<<" Month: "<<tm.tm_mon<<" Day: "<<tm.tm_mday<<std::endl;
}
int main()
{
testDate("%y%m%d","101112");
testDate("%Y%m%d","101112");
testDate("%y%m%d","20101112");
testDate("%Y%m%d","20101112");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
101112 parsed using %y%m%d -> Year: 1011 Month: 11 Day: 0
101112 parsed using %Y%m%d -> Year: 1011 Month: 11 Day: 0
20101112 parsed using %y%m%d -> Year: 2010 Month: 10 Day: 12
20101112 parsed using %Y%m%d -> Year: 2010 Month: 10 Day: 12
Run Code Online (Sandbox Code Playgroud)
测试用:
g++ (SUSE Linux) 11.2.1 20210816 [修订版 056e324ce46a7924b5cf10f61010cf9dd2ca10e9]
铿锵++版本12.0.1
dah*_*527 -1
测试后发现
\n%y- 仅输入两位数字%Y- 仅输入四位数字使用时如果使用4位%y,则直接输出4位,如果是2位,则与文档一致
月份,来自std::tm
\ntm_mon : months since January \xe2\x80\x93 [0, 11]\nRun Code Online (Sandbox Code Playgroud)\n所以它打印出 11 代表 12 月。您可以使用下面的代码打印出日期。
\n std::cout << std::put_time(&tm, "%c") << std::endl;\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
418 次 |
| 最近记录: |