mktime的一个简单问题

zxcdewq 2006-03-08 09:09:57
struct tm tm1;
tm1.tm_year= 2006 - 1900;
tm1.tm_mon=4-1;
tm1.tm_mday=0;
tm1.tm_hour=0;
tm1.tm_min=0;
tm1.tm_sec=0;

mktime( &tm1 );

为什么tm1.tm_mday=0; mktime后就变成了31?
...全文
687 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovezn0424 2006-03-09
  • 打赏
  • 举报
回复
结构体 tm 在time.h 的声明
struct tm {
int tm_sec; /* 秒 [0-61] 到最大2秒喂的时候,考虑闰秒 */
int tm_min; /* 分 [0-59] */
int tm_hour; /* 时 [0-23] */
int tm_mday; /* 日 [1-31] */
int tm_mon; /* 月 [0-11] 0から始まることに注意 */
int tm_year; /* 年 [1900开始经过的年数] */
int tm_wday; /* 星期 [0:日... 6:六] */
int tm_yday; /* 年内的日子数 [0-365] 注意是从0开始*/
int tm_isdst; /* 夏时标志位, */
};


这样应该很清楚了吧!
zez 2006-03-09
  • 打赏
  • 举报
回复
老大,自己看一下 msdn 或man 一下,看看各个数据的取值范围
逸学堂 2006-03-09
  • 打赏
  • 举报
回复
Run-Time Library Reference

mktime, _mktime64See Also
Time Management Routines | asctime | gmtime | localtime | time | Run-Time Routines and .NET Framework Equivalents
Requirements
Routine Required header Compatibility
mktime <time.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_mktime64 <time.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.
Convert the local time to a calendar value.

time_t mktime(
struct tm *timeptr
);
__time64_t _mktime64(
struct tm *timeptr
);
Parameter
timeptr
Pointer to time structure; see asctime.
Return Value
mktime returns the specified calendar time encoded as a value of type time_t. If timeptr references a date before midnight, January 1, 1970, or if the calendar time cannot be represented, mktime returns –1 cast to type time_t. When using mktime and if timeptr references a date after 3:14:07 January 19, 2038, UTC, it will return –1 cast to type time_t.

_mktime64 will return –1 cast to type __time64_t if timeptr references a date after 23:59:59, December 31, 3000, UTC.

Remarks
The mktime function converts the supplied time structure (possibly incomplete) pointed to by timeptr into a fully defined structure with normalized values and then converts it to a time_t calendar time value. The converted time has the same encoding as the values returned by the time function. The original values of the tm_wday and tm_yday components of the timeptr structure are ignored, and the original values of the other components are not restricted to their normal ranges.

After an adjustment to Greenwich Mean Time (GMT), mktime handles dates from midnight, January 1, 1970, to January 19, 3:14:07, 2038. This adjustment may cause mktime to return -1 (cast to time_t) even though the date you specify is within range. For example, if you are in Cairo, Egypt, which is two hours ahead of GMT, two hours will first be subtracted from the date you specify in timeptr; this may now put your date out of range.

If successful, mktime sets the values of tm_wday and tm_yday as appropriate and sets the other components to represent the specified calendar time, but with their values forced to the normal ranges. The final value of tm_mday is not set until tm_mon and tm_year are determined. When specifying a tm structure time, set the tm_isdst field to:

Zero (0) to indicate that standard time is in effect.
A value greater than 0 to indicate that daylight savings time is in effect.
A value less than zero to have the C run-time library code compute whether standard time or daylight savings time is in effect.
(The C run-time library assumes the United States' rules for implementing the calculation of Daylight Saving Time.) tm_isdst is a required field. If not set, its value is undefined and the return value from mktime is unpredictable. If timeptr points to a tm structure returned by a previous call to asctime, gmtime, or localtime, the tm_isdst field contains the correct value.

Note that gmtime and localtime use a single statically allocated buffer for the conversion. If you supply this buffer to mktime, the previous contents are destroyed.

Requirements
Routine Required header Compatibility
mktime <time.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_mktime64 <time.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example
// crt_mktime.c
/* The example takes a number of days
* as input and returns the time, the current
* date, and the specified number of days.
*/

#include <time.h>
#include <stdio.h>

int main( void )
{
struct tm when;
__time64_t now, result;
int days;

_time64( &now );
when = *_localtime64( &now );
printf( "Current time is %s\n", asctime( &when ) );
days = 20;
when.tm_mday = when.tm_mday + days;
if( (result = _mktime64( &when )) != (time_t)-1 )
printf( "In %d days the time will be %s\n",
days, asctime( &when ) );
else
perror( "_mktime64 failed" );
}
Sample Output
Current time is Tue Feb 12 09:57:44 2002

In 20 days the time will be Mon Mar 04 09:57:44 2002
See Also
Time Management Routines | asctime | gmtime | localtime | time | Run-Time Routines and .NET Framework Equivalents
zxcdewq 2006-03-09
  • 打赏
  • 举报
回复
tm1.tm_mon=0; 为什么就不会变?
junguo 2006-03-08
  • 打赏
  • 举报
回复
没有0这个日期啊.函数里小于1就自动给你变了.

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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