我无法理解这个程序,即tm_mon +1
part.
我是 C 语言新手,通常我总是编写自己的小程序来应对我在遵循的课程书中设置的任何挑战,但我不得不咨询其他人来解决这个问题(它是课本和他们的代码,所以不是我的),我不明白为什么+1
被添加到tm_mon
。我明白那个tm_mon = January
但我不清楚 +1 有何不同,并且why它是 +1 而不是 + 2 或任何其他数字!
该程序旨在告诉您 400 万秒后的日期。
include <stdio.h>
#include <time.h>
int main(int argc, const char * argv[])
{
long secondsToFuture = time(NULL) + 4000000;
struct tm futureTime;
localtime_r(&secondsToFuture, &futureTime);
printf("The date in 4,000,000 seconds will be %d-%d-%d\n", futureTime.tm_mon + 1, futureTime.tm_mday, futureTime.tm_year + 1900);
return 0;
}
感谢任何帮助,因为我对此有点困惑。
看看下表。如您所见,tm_mon 是从 0=一月到 11=十二月的值。
'*' tm_sec 一般为 0-59。额外的范围是为了适应某些系统中的闰秒。
EDIT
那么在你的情况下没有+1如果日期为 2015 年 1 月 1 日,实际输出将是
The date in 4,000,000 seconds will be 0/1/2015
With +1将推动输出
The date in 4,000,000 seconds will be 1/1/2015
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)