在开始时,我想强调一下,我已经阅读了堆栈溢出中的所有类似文章。什么也帮不了我。
#include <stdio.h>
#include <stdlib.h>
#define _USE_32BIT_TIME_T 1
#include <time.h>
int main(void)
{
struct tm beg;
struct tm aft;
beg.tm_hour=0; beg.tm_min=0; beg.tm_sec=0;
aft.tm_hour=11; aft.tm_min=19; aft.tm_sec=19;
long long c;
c=difftime(mktime(&aft),mktime(&beg));
printf("%lld",c);
return 0;
}所有的timr打印0没有其他的,但是当我试图将mktime(&aft)更改为time now (&now)时,我得到了非零的结果。在这段代码中我应该更正什么?
发布于 2014-06-24 11:17:29
如果传递给mktime的参数引用1970年1月1日午夜之前的日期,或者如果无法表示日历时间,则函数返回- 1强制转换为time_t。(http://msdn.microsoft.com/en-us/library/aa246472(v=vs.60).aspx)
这可能导致mktime失败,因为beg和aft中的tm_year变量无效。
将此变量设置为表示1970年后一年的值将产生预期的结果。
#include <stdio.h>
#include <stdlib.h>
#define _USE_32BIT_TIME_T 1
#include <time.h>
int main(void)
{
struct tm beg = {0};
struct tm aft = {0};
beg.tm_hour=0; beg.tm_min=0; beg.tm_sec=0;
beg.tm_year = 71; //Set to 1971
aft.tm_hour=11; aft.tm_min=19; aft.tm_sec=19;
aft.tm_year = 71; //Set to 1971
long long c;
c=difftime(mktime(&aft),mktime(&beg));
printf("%lld",c);
return 0;
}https://stackoverflow.com/questions/24381575
复制相似问题