-1

如果我这样做,我对如何使用 tm 结构对象感到有些困惑;

printf("The current date is %d-%d-%d\n", now.tm_mon, now.tm_mday, now.tm_year);

我的输出是;

The current date is 11-31-112

但是当我这样做时;

printf("The current date is %d-%d-%d\n", now.tm_mon + 1, now.tm_mday, now.tm_year 
+ 1900);

我的输出是;

The current date is 12-31-2012

我有点糊涂了怎么把+1和+1900的加法正确格式化输出时间?预先感谢您的任何帮助!

4

2 回答 2

4

从手册页:

       tm_mon 自一月以来的月数,范围为 0 到 11。

   tm_year   The number of years since 1900.

您可能还想查看strftime输出功能。

于 2013-01-01T00:50:59.977 回答
3

这就是定义tm 结构中的值的方式:

  • tm_mon = 自一月以来的月数,范围为 0 到 11。

  • tm_year = 自 1900 年以来的年数。

我怀疑将起始年份设为 1900 年的原因是为了启用两位数年份值,我们都看到了效果如何。

一个有趣的旁注是,1900 年是全球时区成为标准的时间,所以也许人们认为这是一个设定开始年份的好时机。不过只是猜测。

于 2013-01-01T00:51:39.613 回答