我需要一个可以计算两个日期时间(年,月,日,小时,分钟,秒)之间差异的函数。然后以相同的格式返回差异。
int main (){
struct datetime dt_from;
init_datetime(&dt_from, 1995, 9, 15, 10, 40, 15);
struct datetime dt_to;
init_datetime(&dt_to, 2004, 6, 15, 10, 40, 20);
struct datetime dt_res;
datetime_diff(&dt_from, &dt_to, &dt_res);
return 0;
}
void datetime_diff(struct datetime *dt_from, struct datetime *dt_to
, struct datetime *dt_res) {
//What can I do here to calculate the difference, and get it in the dt_res?
}
答案 0 :(得分:0)
这是基本的想法:
将您的datetime
转换为整数类型(最好是long long
或unsigned long long
),代表您的datetime
值作为它的最小单位(在您的情况下为第二位) )。怎么实现呢?轻松将单个值转换为秒,并将所有内容添加到一起。 (seconds + minutes * 60 + hours * 3600 ...
)
对两个值执行此操作,然后减去整数值。
现在将单个整数值(时差)转换回datetime
。怎么样?从最大单位(年)开始,将差异除以一年内的秒数(60 * 60 * 24 * 365)。现在您知道两个datetime
之间有多少年了。取其余部分并将其除以每月的秒数,依此类推......
(显然我忽略了一切相当复杂的事情,例如夏令时)
但我强烈建议您使用评论中提到的struct tm
time.h
。它是便携式的,您可以使用difftime
。
答案 1 :(得分:0)
请查看并尝试使用time.h
的示例,并且应该是可移植的。它会计算您问题中日期之间的天数差异。您可以稍微更改程序,使其按您希望的方式工作。
#include <stdio.h>
#include <time.h>
#include <math.h>
int main() {
time_t start_daylight, start_standard, end_daylight, end_standard;;
struct tm start_date = {0};
struct tm end_date = {0};
double diff;
printf("Start date: ");
scanf("%d %d %d", &start_date.tm_mday, &start_date.tm_mon, &start_date.tm_year);
printf("End date: ");
scanf("%d %d %d", &end_date.tm_mday, &end_date.tm_mon, &end_date.tm_year);
/* first with standard time */
start_date.tm_isdst = 0;
end_date.tm_isdst = 0;
start_standard = mktime(&start_date);
end_standard = mktime(&end_date);
diff = difftime(end_standard, start_standard);
printf("%.0f days difference\n", round(diff / (60.0 * 60 * 24)));
/* now with daylight time */
start_date.tm_isdst = 1;
end_date.tm_isdst = 1;
start_daylight = mktime(&start_date);
end_daylight = mktime(&end_date);
diff = difftime(end_daylight, start_daylight);
printf("%.0f days difference\n", round(diff / (60.0 * 60 * 24)));
return 0;
}
测试
Start date: 15 9 1995
End date: 15 6 2004
3195 days difference
甚至更简单的非交互式代码以及标准或夏令时:
#include <stdio.h>
#include <time.h>
#include <math.h>
int main()
{
time_t start_daylight, start_standard, end_daylight, end_standard;;
struct tm start_date = {0};
struct tm end_date = {0};
double diff;
start_date.tm_year = 1995;
start_date.tm_mon = 9;
start_date.tm_mday = 15;
start_date.tm_hour = 10;
start_date.tm_min = 40;
start_date.tm_sec = 15;
end_date.tm_mday = 15;
end_date.tm_mon = 6;
end_date.tm_year = 2004;
end_date.tm_hour = 10;
end_date.tm_min = 40;
end_date.tm_sec = 20;
/* first with standard time */
start_date.tm_isdst = 0;
end_date.tm_isdst = 0;
start_standard = mktime(&start_date);
end_standard = mktime(&end_date);
diff = difftime(end_standard, start_standard);
printf("%.0f days difference\n", round(diff / (60.0 * 60 * 24)));
/* now with daylight time */
start_date.tm_isdst = 1;
end_date.tm_isdst = 1;
start_daylight = mktime(&start_date);
end_daylight = mktime(&end_date);
diff = difftime(end_daylight, start_daylight);
printf("%.0f days difference\n", round(diff / (60.0 * 60 * 24)));
return 0;
}