C++ c++;11 difftime()计算闰年错误?
我制作了一个简单的程序来计算两天之间的天数:C++ c++;11 difftime()计算闰年错误?,c++,c++11,difftime,C++,C++11,Difftime,我制作了一个简单的程序来计算两天之间的天数: #include <stdio.h> #include <iostream> #include <ctime> #include <utility> using namespace std; int main(){ struct tm t1 = {0,0,0,28,2,104}; struct tm t2 = {0,0,0,1,3,104}; time_t x = mktime
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <utility>
using namespace std;
int main(){
struct tm t1 = {0,0,0,28,2,104};
struct tm t2 = {0,0,0,1,3,104};
time_t x = mktime(&t1);
time_t y = mktime(&t2);
cout << difftime(y,x)/3600/24 << endl;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
struct tm t1={0,0,0,28,2104};
struct tm t2={0,0,0,1,3104};
时间x=mktime(&t1);
时间y=mktime(&t2);
cout在struct tm
中,月数从0
到11
(不是1
到12
),因此2
是三月,3
是四月,您编码输出三月二十八日到四月一日之间的天数,即4
正确的版本是:
struct tm t1 = {0, 0, 0, 28, 1, 104};
struct tm t2 = {0, 0, 0, 1, 2, 104};
顺便说一下,2004年是闰年,因此2月有29天,2月28日和3月1日之间有两天(不是一天)
difftime
提供02/28/2004 00:00:00
和03/01/2004 00:00:00
之间的秒数(第一天计入差异)“< /P>月开始在<代码> 0 /代码>未<代码> 1 < /C>,因此代码< 2 /代码>是三月和<代码> 3 < /C> >四月,三月二十八日到4月1日之间总是4天。现在我明白了,谢谢您的清晰解释!酷,所以,月0不仅仅是java问题!也许java已经从C++移植了。