我已经写下了这段代码,我的程序对象是计算两个给定日期和时间之间的分钟数。 让我们说分钟之间的差异:
14/1/2016 23:18
and
14/1/2004 23:18
is:
6,311,520.00 minutes
这是我写的代码:
我在计算中有一些错误,从我发现我的问题最多是1440分钟与正确答案的差异 - 由excel检查。 我认为我的问题在于计算两个日期之间的LEAP DAYS:
#include <stdio.h>
typedef struct {
int year;
int month;
int day;
int hour;
int minute;
int second;
}time;
time time1,time2;
long calcTime(time,time);
int calcDaysFromStart(int,int);
int leapcheck(int);
int main()
{
printf("Hello\n");
printf("For calculating the difference between two times:\n");
printf("Enter the date for first time:\n");
printf("Enter day:\n");
scanf("%d",&time1.day);
printf("Enter month:\n");
scanf("%d",&time1.month);
printf("Enter year:\n");
scanf("%d",&time1.year);
printf("Enter the exact hour for first time:\n");
printf("Enter the hour:\n");
scanf("%d",&time1.hour);
printf("Enter the minutes:\n");
scanf("%d",&time1.minute);
printf("Enter the seconds:\n");
scanf("%d",&time1.second);
printf("-----------------------------------\n");
printf("Enter the date for second time:\n");
printf("Enter day:\n");
scanf("%d",&time2.day);
printf("Enter month:\n");
scanf("%d",&time2.month);
printf("Enter year:\n");
scanf("%d",&time2.year);
printf("Enter the exact hour for first time:\n");
printf("Enter the hour:\n");
scanf("%d",&time2.hour);
printf("Enter the minutes:\n");
scanf("%d",&time2.minute);
printf("Enter the seconds:\n");
scanf("%d",&time2.second);
printf("-----------------------------------\n");
printf("-----------------------------------\n");
printf("The first time is: %d:%d:%d %d/%d/%d\n", time1.hour ,time1.minute ,time1.second ,time1.day, time1.month ,time1.year);
printf("The second time is: %d:%d:%d %d/%d/%d\n", time2.hour ,time2.minute ,time2.second ,time2.day, time2.month ,time2.year);
printf("The Difference between the two times in minutes is:%ld\n", calcTime(time1,time2));
return 1;
}
long calcTime(time time1,time time2)
{
long t1,t2,totalDiff;
long yearDiffeInMinutes = 0;
int leapt1, leapt2,leapAdd;
leapt1 = leapcheck(time1.year);
leapt2 = leapcheck(time2.year);
int daysFromStartt1, daysFromStartt2;
daysFromStartt1 = calcDaysFromStart(time1.month,leapt1);
daysFromStartt2 = calcDaysFromStart(time2.month,leapt2);
t1 = time1.minute+time1.hour*60+time1.day*1440+daysFromStartt1*1440;
t2 = time2.minute+time2.hour*60+time2.day*1440+daysFromStartt2*1440;
if (time1.year>time2.year)
{
leapAdd = (time1.year-time2.year)/4;
if ((leapt1==1) && (time1.month<3))
leapAdd--;
if((leapt2==1) && (time2.month>2))
leapAdd--;
printf("THE PARAM leapApp IS:%d\n",leapAdd);
yearDiffeInMinutes = ((time1.year-time2.year)*525600+leapAdd*1440);
totalDiff = yearDiffeInMinutes+(t1-t2);
printf("The first time is bigger\n");
return totalDiff;
}
else if(time2.year>time1.year)
{
leapAdd = (time2.year-time1.year)/4;
if ((leapt2==1) && (time2.month<3))
leapAdd--;
if((leapt1==1) && (time1.month>2))
leapAdd--;
yearDiffeInMinutes = ((time2.year-time1.year)*525600+leapAdd*1440);
totalDiff = yearDiffeInMinutes+(t2-t1);
printf("The second time is bigger\n");
return totalDiff;
}
else if(t1>t2)/**both times are in the same year**/
{
printf("The first time is bigger\n");
if ((leapt1==1) && (time1.month>2))
if(time2.month<2)
return (t1-t2+1440);
return(t1-t2);
}
else if(t2>t1)
{
printf("The second time is bigger\n");
if ((leapt2==1) && (time2.month>2))
if(time1.month<2)
return (t2-t1+1440);
return (t2-t1);
}
else
{
printf("Both times are equals\n");
return 0;
}
}
/**check if the year is leap, return 0 if not a leap and 1 if a leap**/
int leapcheck(int year)
{
if(year%400==0 || (year%100!=0 && year%4==0))
{
printf("THE YEAR %d IS LEAP\n",year);
return 1;
}
printf("THE YEAR %d is NOT LEAP\n",year);
return 0;
}
/**clalculate how many days past from start ofthe year**/
int calcDaysFromStart(int month, int leap)
{
if (month==1)
return 0;
else if (month==2)
return 31;
else if (month==3)
return (59+leap);
else if (month==4)
return (90+leap);
else if (month==5)
return (120+leap);
else if (month==6)
return (151+leap);
else if (month==7)
return (181+leap);
else if (month==8)
return (212+leap);
else if (month==9)
return (243+leap);
else if (month==10)
return (273+leap);
else if (month==11)
return (304+leap);
else if (month==12)
return (334+leap);
else return -1;
}
答案 0 :(得分:5)
计算
leapAdd = (time1.year-time2.year)/4;
错了。从2003年到2005年有一个闰年,但这会忽略它。此外,13年的跨度可能包括三到四年的闰年,而没有达到世纪复杂性。
正确的代码是
leapAdd = time1.year/4 - time2.year/4 // how many Caesarian leap years
- time1.year/100 + time2.year/100 // Centuries
+ time1.year/400 - time2.year/400; // last correction.
(如果是time2.year > time1.year
,则使用交换的角色。)
此外,
t1 = time1.minute+time1.hour*60+time1.day*1440+daysFromStartt1*1440;
overcounts。在本月的第一天,没有完整的日子要添加,所以它应该是
(time1.day-1)*1440
同样(time1.hour-1)*60
,迂腐,也是分钟。由于过度计数是常数,因此不会影响时差的计算。
答案 1 :(得分:2)
有一个标准的库函数可以做到这一点。
time_t seconds_begin, seconds_end;
struct tm breakdown;
breakdown.tm_year = 2004 - 1900;
breakdown.tm_mon = 0; /* january */
breakdown.tm_mday = 14;
breakdown.tm_hour = 23;
breakdown.tm_min = 18;
seconds_begin = mktime( & breakdown );
breakdown.tm_year = 2016 - 1900;
seconds_end = mktime( & breakdown );
printf( "%.2f minutes", (seconds_end - seconds_begin) / 60. );
6311520.00分钟
在Unix系统上,我认为这比Excel更可靠。
答案 2 :(得分:0)
您示例的快速解决方法是在if
和else
中添加以下行:
if(leapt1==1 && leapt2==1)
leapAdd++;
但是这个代码将失败更大的间隔(包括100和400年的跳跃异常)。所以,我建议你重写这部分代码;例如,您可以在区间中迭代每年中的每一年,并检查它是否跳跃或常见,使用特殊的套管开始和结束间隔。)