C - mktime() returns -1 不应该的地方
C - mktime() returns -1 where it shouldn't
我正在尝试编写一个函数来查找当前日期与格式化字符串中给定日期(如 dd.mm.yy H:M:S
)之间的天数差异,但 mktime returns -1 every单次。尝试用零初始化两个时间结构,但这没有帮助。
而且,将 time_t
更改为 size_t
使其适用于 mktime(now)
,但不适用于 mktime(parsedDate)
我真的很困惑,我做错了什么?
我在 Windows 10.
上使用 MSVC2019 (v142)
int findDifferenceInDays(char* dateStr)
{
// Date/time format example:
// 12.09.2018 13:44:30
char date[] = "12.09.2018 13:44:30";
struct tm *now, *parsedTime;
time_t t_now = time(0);
now = localtime(&t_now);
time_t temp_time = time(&temp_time);
parsedTime = localtime(&temp_time);
int day, month, year, hour, minutes, seconds;
sscanf(dateStr, "%d.%d.%d %d:%d:%d",
&day, &month, &year, &hour, &minutes, &seconds);
parsedTime->tm_year = year - 1990;
parsedTime->tm_mon = month - 1;
parsedTime->tm_mday = day;
parsedTime->tm_hour = hour;
parsedTime->tm_min = minutes;
parsedTime->tm_sec = seconds;
time_t endTime, begTime;
endTime = mktime(now);
begTime = mktime(parsedTime);
double diffInSeconds = difftime(endTime, begTime);
int daysDifference = difftime(endTime, begTime) / (60*60*24);
printf("Days between %s and now: %lf\n", dateStr, daysDifference / (60 * 60 * 24));
return daysDifference;
}
可能不是你的直接错误,但是
parsedTime->tm_year = year - 1900; /* not 1990 */
我正在尝试编写一个函数来查找当前日期与格式化字符串中给定日期(如 dd.mm.yy H:M:S
)之间的天数差异,但 mktime returns -1 every单次。尝试用零初始化两个时间结构,但这没有帮助。
而且,将 time_t
更改为 size_t
使其适用于 mktime(now)
,但不适用于 mktime(parsedDate)
我真的很困惑,我做错了什么?
我在 Windows 10.
上使用 MSVC2019 (v142)int findDifferenceInDays(char* dateStr)
{
// Date/time format example:
// 12.09.2018 13:44:30
char date[] = "12.09.2018 13:44:30";
struct tm *now, *parsedTime;
time_t t_now = time(0);
now = localtime(&t_now);
time_t temp_time = time(&temp_time);
parsedTime = localtime(&temp_time);
int day, month, year, hour, minutes, seconds;
sscanf(dateStr, "%d.%d.%d %d:%d:%d",
&day, &month, &year, &hour, &minutes, &seconds);
parsedTime->tm_year = year - 1990;
parsedTime->tm_mon = month - 1;
parsedTime->tm_mday = day;
parsedTime->tm_hour = hour;
parsedTime->tm_min = minutes;
parsedTime->tm_sec = seconds;
time_t endTime, begTime;
endTime = mktime(now);
begTime = mktime(parsedTime);
double diffInSeconds = difftime(endTime, begTime);
int daysDifference = difftime(endTime, begTime) / (60*60*24);
printf("Days between %s and now: %lf\n", dateStr, daysDifference / (60 * 60 * 24));
return daysDifference;
}
可能不是你的直接错误,但是
parsedTime->tm_year = year - 1900; /* not 1990 */