简体   繁体   English

如何使用计算两个日期之间的天数<time.h>

[英]How to count number of days between two dates using <time.h>

I'm trying to code ac function that calculates de number of DAYS between two dates ( format: yyyy-mm-dd ) using time.h.我正在尝试编写 ac 函数,该函数使用 time.h 计算两个日期之间的天数(格式:yyyy-mm-dd)。

The date is read from a string.从字符串中读取日期。 I don't need to account for seconds or minutes.我不需要考虑几秒钟或几分钟。

I know I probably need to use difftime() but how can It be used in that format?我知道我可能需要使用 difftime() 但它如何以这种格式使用?

Thank you,谢谢,


This is what I tried to do but I get segmentation fault.这就是我试图做的,但我遇到了分段错误。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define CURRDATE "2019-10-5"
#define OTHER "2019-10-10"

void auxDate(struct tm *t, char *date)
{
    struct tm p;

    p.tm_year = atoi(strtok(date, "-")) - 1900;
    p.tm_mon = atoi(strtok(date, "-"));
    p.tm_mday = atoi(date);

    *t = p;
}
int main()
{
    struct tm date1, date2;

    auxDate(&date1, CURRDATE);
    auxDate(&date2, OTHER);

    printf("%.0lf\n", difftime(mktime(&date1), mktime(&date2)));
}

Code fails for various reasons.代码由于各种原因失败。

"2019-10-5" is a string literal that strtok(date, "-") attempts to modify. "2019-10-5"strtok(date, "-")试图修改的字符串文字 This leads to undefined behavior (UB).这会导致未定义的行为(UB)。 Certainly the cause of the segmentation fault.当然是分段错误的原因。

Instead use void auxDate(struct tm *t, const char *date) and parse date without attempting to change it.而是使用void auxDate(struct tm *t, const char *date)并解析date而不尝试更改它。 See strspn() and strcspn() .请参阅strspn()strcspn()


mktime(&date1) uses a not completely assigned struct tm . mktime(&date1)使用未完全分配的struct tm Initialize all struct tm members with struct tm p = { 0 };使用struct tm p = { 0 };初始化所有struct tm成员struct tm p = { 0 }; . . Otherwise the uninitialized members can provide erroneous values to mktime() .否则,未初始化的成员可能会向mktime()提供错误的值。


.tm_mon is the number of month since January. .tm_mon一月以来的月份数。 I'd expect a - 1 somewhere.我希望某处有- 1


Since difftime() return the difference in seconds and the goal is difference in days.由于difftime()以秒为单位返回差异,而目标是以天为单位的差异。 Division by the seconds-per-day is missing.缺少每日秒数的除法。


The are other corner case to solve that involve daylight savings time and redefinitions of a timezone's offset that will shift the time differences by a hour or so.其他需要解决的极端情况涉及夏令时和时区偏移的重新定义,这将使时差移动一个小时左右。

To handle, code could use the time at noon and set the DST to -1为了处理,代码可以使用中午的时间并将 DST 设置为 -1

p.tm_hour = 12;
p.tm_isdst = -1;

Then round the division:然后围绕分区:

days = round(difftime(...)/(24.0*60*60));

Even this will not handle very rare cases when a Pacific area timezone changed which side of the international date line it was on, which effectively repeated or cancelled a day.即使这样也无法处理非常罕见的情况,即太平洋地区时区更改了它所在的国际日期变更线的哪一侧,从而有效地重复或取消了一天。


On the good side, your posted code does compile.从好的方面来说,您发布的代码确实可以编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM