社区
Linux/Unix社区
帖子详情
mktime返回-1的原因
macro2008
2008-11-26 11:17:08
程序运行正常,时间提取也正确,但mktime返回-1,到底是什么原因呢
...全文
1653
7
打赏
收藏
mktime返回-1的原因
程序运行正常,时间提取也正确,但mktime返回-1,到底是什么原因呢
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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年左右的时间,超过就溢出了。
ACE_OS::
mk
time
函数源代码
static __
time
64_t __cdecl _make__
time
64_t ( struct tm *tb, int ultflag ) { __
time
64_t tmptm1, tmptm2, tmptm3; struct tm tbtemp; long dstbias = 0; long
time
zone = 0; _VALIDATE_RETURN( ( tb != NULL ), EINVAL, ( ( __
time
64_t )( -1 ) ) ) /* * First, make sure tm_year is reasonably close to being in range. */ if ( ((tmptm1 = tb->tm_year) _MAX_YEAR64 + 1) ) goto err_
mk
time
; /* * 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_
mk
time
; } /***** 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 = (__
time
64_t)(tb->tm_mday)); /***** HERE: tmptm1 holds number of elapsed days *****/ /* * Calculate elapsed hours since base date */ tmptm2 = tmptm1 * 24; tmptm1 = tmptm2 + (tmptm3 = (__
time
64_t)tb->tm_hour); /***** HERE: tmptm1 holds number of elapsed hours *****/ /* * Calculate elapsed minutes since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__
time
64_t)tb->tm_min); /***** HERE: tmptm1 holds number of elapsed minutes *****/ /* * Calculate elapsed seconds since base date */ tmptm2 = tmptm1 * 60; tmptm1 = tmptm2 + (tmptm3 = (__
time
64_t)tb->tm_sec); /***** HERE: tmptm1 holds number of elapsed seconds *****/ if ( ultflag ) { /* * Adjust for
time
zone. No need to check for overflow since * local
time
() will check its arg value */ __tzset(); _ERRCHECK(_get_dstbias(&dstbias;)); _ERRCHECK(_get_
time
zone(&
time
zone;)); tmptm1 +=
time
zone; /* * Convert this second count back into a
time
block structure. * If local
time
returns NULL, return an error. */ if ( _local
time
64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_
mk
time
; /* * 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 ( _local
time
64_s(&tbtemp;, &tmptm1;) != 0 ) goto err_
mk
time
; } } else { if ( _gm
time
64_s(&tbtemp;, &tmptm1;) != 0) goto err_
mk
time
; } /***** HERE: tmptm1 holds number of elapsed seconds, adjusted *****/ /***** for local
time
if requested *****/ *tb = tbtemp; return tmptm1; err_
mk
time
: /* * All errors come to here */ errno = EINVAL; return (__
time
64_t)(-1); }
在Python中操作时间之
mk
time
()方法的使用教程
mk
time
()方法是local
time
()反函数。它的参数是struct_
time
或全9元组,它
返回
一个浮点数,为了兼容时
time
()。 如果输入值不能表示为有效的时间,那么OverflowError或ValueError错误将被引发。 Syntax 以下是
mk
time
()方法的语法:
time
.
mk
time
(t) 参数 t — 这是struct_
time
或满9元组。
返回
值 此方法
返回
一个浮点数,对于兼容性
time
()。 例子 下面的例子显示了
mk
time
()方法的使用。 #!/usr/bin/python import
time
t = (2009, 2, 17, 17
php中
time
()和
mk
time
()方法的区别
time
()函数是
返回
当前时间的。而
mk
time
()函数的主要功能不是
返回
当前时间,而是格式化时间。虽然单独写
mk
time
()而不加任何参数如:echo
mk
time
()和echo
time
()的效果是一样的。但本质上是不一样的。 PHP
mk
time
() 函数 PHP Date /
Time
函数 定义和用法
mk
time
() 函数
返回
一个日期的 Unix 时间戳。参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。 语法
mk
time
(hour,minute,second,month,day,year,is_d
PHP程序设计-3期(KC016) 3.2.2构造特定日期拓展知识.doc
PHP程序设计-3期(KC016) 3.2.2构造特定日期拓展知识.doc 学习资料 复习资料 教学资源
PHP中使用
mk
time
获取时间戳的一个黑色幽默分析
mk
time
(hour,minute,second,month,day,year,is_dst)这是
mk
time
的语法说明,一目了然应该不难写出一个时间戳的代码来
Linux/Unix社区
23,216
社区成员
74,539
社区内容
发帖
与我相关
我的任务
Linux/Unix社区
Linux/Unix社区 应用程序开发区
复制链接
扫一扫
分享
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章