mktime函数

zhongyj2004 2006-07-20 10:05:29
void main()
{
tm tmDate = {0};
tmDate.tm_mday=31;
tmDate.tm_mon=1;
tmDate.tm_year=106;
time_t m_time=mktime(&tmDate);
cout<<m_time<<endl;
cout<<tmDate.tm_year<<endl;
cout<<tmDate.tm_mon<<endl;
cout<<tmDate.tm_mday<<endl;
}
输出为
106
2
3
是不是mktime函数不仅仅是得到一个秒数,还会修改参数tmDate的值???
上面给定的日期是2006-2-31,是一个不存在的日期,经过函数mktime之后就改变了变量,变成2006-3-2号了
...全文
444 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhongyj2004 2006-07-20
  • 打赏
  • 举报
回复
英文太差,看了上面的英文,还是不知道
jixingzhong 2006-07-20
  • 打赏
  • 举报
回复
The original values of tm_wday and tm_yday members of ptm are ignored and filled with the correspondent ones to the calculated date. The range of tm_mday is not checked until tm_mon and tm_year are determined.
jixingzhong 2006-07-20
  • 打赏
  • 举报
回复
mktime
<time.h>

time_t mktime ( struct tm * ptm );
Convert tm structure to time_t value.
Checks the members of the tm structure passed as parameter ptm adjusting the values if the ones provided are not in the possible range or they are incomplete or mistaken and then translates that structure to a time_t value (seconds elapsed since Jan 1, 1970) that is returned.
The original values of tm_wday and tm_yday members of ptm are ignored and filled with the correspondent ones to the calculated date. The range of tm_mday is not checked until tm_mon and tm_year are determined.


Parameters.

ptm
Pointer to a tm structure, that contains data to be computed.
Return Value.
A time_t value corresponding to the date and time passed in ptm parameter.
On error, a -1 value is returned.

Portability.
Defined in ANSI-C.


Example.


/* mktime example: weekday calculator */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;
struct tm * timeinfo;
int year, month ,day;
char * weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};

/* prompt user for date */
printf ("Enter year: "); scanf ("%d",&year);
printf ("Enter month: "); scanf ("%d",&month);
printf ("Enter day: "); scanf ("%d",&day);

/* get current timeinfo and modify it to user's choice */
time ( &rawtime );
timeinfo = localtime ( &rawtime );
timeinfo->tm_year = year - 1900;
timeinfo->tm_mon = month - 1;
timeinfo->tm_mday = day;

/* call mktime: timeinfo->tm_wday will be set */
mktime ( timeinfo );

printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);

return 0;
}

Output:
Enter year: 2000
Enter month: 5
Enter day: 20
That day is a Saturday.

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧