为什么在传递给 mktime 时,我应该将月份数字设置为实际月份
Why should I set the month number to the actual month when passing to mktime?
#include <time.h>
#include <iostream>
int main()
{
struct tm dayofmonth = {0};
int iYear = 2012;
int iMonth = 2; // February
dayofmonth.tm_year = iYear - 1900;
dayofmonth.tm_mon = iMonth;
dayofmonth.tm_mday = 0;
dayofmonth.tm_isdst = 0;
mktime(&dayofmonth);
std::cout << "Number of days for the month " << dayofmonth.tm_mon << " is " << dayofmonth.tm_mday << std::endl;
}
需要编写一个简单的例程,以查找给定月份的天数。但是,对于 mktime,为什么我应该传递实际的月份编号而不是月份编号 -1。
更令人困惑的是,在调用 mktime 后,tm_mon返回月份 -1 而不是经过的原始月份。
因为你设置了tm_mday = 0
.该月的"零日"(tm_mon = 2
表示三月)回滚到上个月的最后一天(二月)。
是的,令人困惑的是,tm_mday
是基于 1 的,而tm_mon
是基于 0 的。你最终会习惯的:-)
相关文章:
- 使用设置精度时如何阻止数字向上舍入?
- 您可以使用 setfill() 来设置 fill 2 位数字吗?
- 如何正确取消设置64位数字中的位?
- 找到非常大的数字的最右边的未设置位
- 根据参数设置十进制数字
- 如何设置数字格式以显示C 中的所有小数位数
- 如何将精度设置为C 的100位数字
- 设置::使用数字中的引号进行向量初始化
- MinGW中的数字格式设置更改
- 找到使用 2 个设置位形成的第 n 个数字
- 将数字设置在小数点后的浮子中
- 当区域设置需要"3,14"时,如何使用scanf解析像"3.14"这样的数字
- 将数字动态数组设置为数组或容器 c++
- 为什么 for 循环没有达到循环的第二个条件中设置的数字
- 十进制数字的区域设置感知编辑控件子类化(格式[sign][xxx..][decimal separator][yy.])
- 在小数点之前设置数字的精度
- 如何将精度设置为一个数字
- 将数字从字符串流转换为具有设置精度的字符串
- 为什么在传递给 mktime 时,我应该将月份数字设置为实际月份
- 函数本身将数字设置为零