mktime返回-1的原因

macro2008 2008-11-26 11:17:08
程序运行正常,时间提取也正确,但mktime返回-1,到底是什么原因呢
...全文
1653 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
macro2008 2008-11-26
  • 打赏
  • 举报
回复
上面打印出来的值也都是正确的,但mktime返回值是-1,真搞不懂。。。
macro2008 2008-11-26
  • 打赏
  • 举报
回复
那些对啊,现在把问题缩小了
tmnow->tm_sec = 10;
tmnow->tm_min = 30;
tmnow->tm_hour = 12;
tmnow->tm_mday = 25;
tmnow->tm_mon = 11 - 1;
tmnow->tm_year = 2009 - 1900;
这样赋值的话,就可以显示出时间,但要是像下面那样传DateTime的话就返回-1,找了好久也不知道是什么原因
memset( temp, 0x00, sizeof(temp) );
memcpy( temp, DateTime, 4 );
tmnow->tm_year = atoi(temp);
printf( "line%d year = %04d\n", __LINE__, tmnow->tm_year );

memset( temp, 0x00, sizeof(temp) );
memcpy( temp, DateTime + 4, 2 );
tmnow->tm_mon = atoi(temp);
printf( "line%d mon = %02d\n", __LINE__, tmnow->tm_mon );

memset( temp, 0x00, sizeof(temp) );
memcpy( temp, DateTime + 6, 2 );
tmnow->tm_mday = atoi(temp);
printf( "line%d mday = %02d\n", __LINE__, tmnow->tm_mday );

memset( temp, 0x00, sizeof(temp) );
memcpy( temp, DateTime + 8, 2 );
tmnow->tm_hour = atoi(temp);
printf( "line%d hour = %02d\n", __LINE__, tmnow->tm_hour );

memset( temp, 0x00, sizeof(temp) );
memcpy( temp, DateTime + 10, 2 );
tmnow->tm_min = atoi(temp);
printf( "line%d min = %02d\n", __LINE__, tmnow->tm_min );

memset( temp, 0x00, sizeof(temp) );
memcpy( temp, DateTime + 12, 2 );
tmnow->tm_sec = atoi(temp);
printf( "line%d sec = %02d\n", __LINE__, tmnow->tm_sec );
请高手指教!
brookmill 2008-11-26
  • 打赏
  • 举报
回复
$ man mktime
......
RETURNS
If the contents of the structure at TIMP do not form a valid calendar
time representation, the result is `-1'. Otherwise, the result is the
time, converted to a `time_t' value.
检查一下给mktime的参数struct tm *TIMP是否正确
macro2008 2008-11-26
  • 打赏
  • 举报
回复
问题解决了,谢谢各位
但就是不懂原来为什么为什么用makefile编译运行成功,可用arm-linux-gcc编译时运行却发生段错误呢?
macro2008 2008-11-26
  • 打赏
  • 举报
回复
问题解决了,谢谢各位
但就是不懂原来为什么为什么用makefile编译运行成功,可用arm-linux-gcc编译时运行却发生段错误呢?
macro2008 2008-11-26
  • 打赏
  • 举报
回复
年份、月份都改过来了,可以用了。
但却发现一个奇怪的问题,为什么用makefile编译运行成功,可用arm-linux-gcc编译时运行却发生段错误呢?
cceczjxy 2008-11-26
  • 打赏
  • 举报
回复 1
tmnow->tm_year = atoi(temp);
printf( "line%d year = %04d\n", __LINE__, tmnow->tm_year );

应该是这的问题
tm_year内存储的值是1900年到现在的年数,而不是实际的年.

在32位的机器上,如果tm_year的值大于137,也就出错了。因为,time_t是个4字节的值,存储的是1900年到现在的秒数,即只能存储137年左右的时间,超过就溢出了。


static __time64_t __cdecl _make__time64_t ( struct tm *tb, int ultflag ) { __time64_t tmptm1, tmptm2, tmptm3; struct tm tbtemp; long dstbias = 0; long timezone = 0; _VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __time64_t )( -1 ) ) ) /* * First, make sure tm_year is reasonably close to being in range. */ if ( ((tmptm1 = tb->tm_year) _MAX_YEAR64 + 1) ) goto err_mktime; /* * Adjust month value so it is in the range 0 - 11. This is because * we don't know how many days are in months 12, 13, 14, etc. */ if ( (tb->tm_mon tm_mon > 11) ) { tmptm1 += (tb->tm_mon / 12); if ( (tb->tm_mon %= 12) tm_mon += 12; tmptm1--; } /* * Make sure year count is still in range. */ if ( (tmptm1 _MAX_YEAR64 + 1) ) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed years *****/ /* * Calculate days elapsed minus one, in the given year, to the given * month. Check for leap year and adjust if necessary. */ tmptm2 = _days[tb->tm_mon]; if ( _IS_LEAP_YEAR(tmptm1) && (tb->tm_mon > 1) ) tmptm2++; /* * Calculate elapsed days since base date (midnight, 1/1/70, UTC) * * * 365 days for each elapsed year since 1970, plus one more day for * each elapsed leap year. no danger of overflow because of the range * check (above) on tmptm1. */ tmptm3 = (tmptm1 - _BASE_YEAR) * 365 + _ELAPSED_LEAP_YEARS(tmptm1); /* * elapsed days to current month (still no possible overflow) */ tmptm3 += tmptm2; /* * elapsed days to current date. */ tmptm1 = tmptm3 + (tmptm2 = (__time64_t)(tb->tm_mday)); /***** HERE: tmptm1 holds number of elapsed days *****/ /* * Calculate elapsed hours since base date */ tmptm2 = tmptm1 * 24; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_hour); /***** HERE: tmptm1 holds number of elapsed hours *****/ /* * Calculate elapsed minutes since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_min); /***** HERE: tmptm1 holds number of elapsed minutes *****/ /* * Calculate elapsed seconds since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__time64_t)tb->tm_sec); /***** HERE: tmptm1 holds number of elapsed seconds *****/ if ( ultflag ) { /* * Adjust for timezone. No need to check for overflow since * localtime() will check its arg value */ __tzset(); _ERRCHECK(_get_dstbias(&dstbias;)); _ERRCHECK(_get_timezone(&timezone;)); tmptm1 += timezone; /* * Convert this second count back into a time block structure. * If localtime returns NULL, return an error. */ if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; /* * Now must compensate for DST. The ANSI rules are to use the * passed-in tm_isdst flag if it is non-negative. Otherwise, * compute if DST applies. Recall that tbtemp has the time without * DST compensation, but has set tm_isdst correctly. */ if ( (tb->tm_isdst > 0) || ((tb->tm_isdst 0)) ) { tmptm1 += dstbias; if ( _localtime64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_mktime; } } else { if ( _gmtime64_s(&tbtemp;, &tmptm1;) != 0) goto err_mktime; } /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/ /***** for local time if requested *****/ *tb = tbtemp; return tmptm1; err_mktime: /* * All errors come to here */ errno = EINVAL; return (__time64_t)(-1); }

23,216

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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