[英]C time.h wrap around
我想将最大可能时间分配给time_t变量,然后将其转换为字符串并打印出结果。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int main()
{
time_t maxTime;
maxTime= LONG_LONG_MAX;
char *strOfMaxTime;
*strOfMaxTime = ctime(maxTime);
printf("%s",strOfMaxTime);
return 0;
}
OP的代码错误地使用了char *ctime(const time_t *timer)
。
time_t maxTime;
char *strOfMaxTime;
// *strOfMaxTime = ctime(maxTime);
strOfMaxTime = ctime(&maxTime);
只需分配maxTime= LONG_LONG_MAX;
确定系统可以处理的最长时间的正确方法不是必需的。
以下是反复试验的方法-可能会受到各种实施限制。 当time_t
超出范围时, localtime()
返回NULL
。
#include <stdio.h>
#include <time.h>
time_t max_time() {
time_t t0, t1;
time_t delta = 1;
time(&t0); // now
while (t0 != -1) {
t1 = t0 + delta;
if (localtime(&t1) == NULL) { // If conversion fail, quit doubling.
break;
}
delta *= 2; // 2x for the next increment.
t0 = t1;
}
while (delta) {
t1 = t0 + delta;
if (localtime(&t1) != NULL) { // if succeeds, update t0
t0 = t1;
}
delta /= 2; // try smaller and smaller deltas.
}
printf("%s %lld\n", ctime(&t0), (long long) t0);
return t0;
}
int main(void) {
max_time();
return 0;
}
输出(请注意17:59:59取决于时区,并且2,147,483,647年是最大的32位有符号整数。YMMV。)
Tue Dec 31 17:59:59 2147483647
67767976233532799
在clock_t和time_t中可表示的时间范围和精度是实现定义的 。
首先,您需要在程序中解决该问题。 下面的语句在编译时必须给出错误:
*strOfMaxTime = ctime(maxTime);
更改为:
strOfMaxTime = ctime(&maxTime);
您可以使用perror()
获取给定输入LONG_LONG_MAX
的错误消息,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
int main()
{
time_t maxTime;
maxTime= LONG_LONG_MAX;
char *strOfMaxTime;
strOfMaxTime = ctime(&maxTime);
if (errno != 0)
perror ("Error");
else
printf("%d,%s",errno, strOfMaxTime);
return 0;
}
在我的设置中,我得到以下输出:
Error: Value too large to be stored in data type
实际上, LONG_LONG_MAX
是无效的输入。
正如标准所提到的, time_t
的范围是实现定义的,因此,如果我给出UINT_MAX
我将得到输出:
0,Sun Feb 7 11:58:15 2106
这是错误的:
*strOfMaxTime = ctime(maxTime);
这试图将ctime
(指向char的指针)的返回值分配给*strOfMaxTime
char。
而是调用:
strOfMaxTime = ctime(&maxTime);
然后检查strOfMaxTime
的返回值,因为如果ctime
未能转换maxTime
可能为NULL
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int main()
{
time_t maxTime;
maxTime = INT_MAX;
char *strOfMaxTime = ctime(&maxTime);
printf("%s",strOfMaxTime);
return 0;
}
最长年份为2038,这称为2038年问题: https : //en.wikipedia.org/wiki/Year_2038_problem
在其他发布中指出了许多错误(将ctime()
输出分配给*strOfMaxTime
, LONG_LONG_MAX
等)。 在我的64位Ubuntu 16.04 Linux系统上, time_t
定义为long int
, long int
定义为8个字节,而long long int
定义为8个字节。 但是,将LLONG_MAX
分配给maxTime
仍然会导致ctime()
失败。 因此,我修改了您的代码以获取ctime()
可接受的有效值上限的范围。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
int main()
{
time_t maxTime;
maxTime= LONG_MAX;
char *strOfMaxTime;
strOfMaxTime = ctime(&maxTime);
while( strOfMaxTime == NULL )
{
perror("ctime error");
printf("%ld div by 2\n", maxTime);
maxTime /= 2;
strOfMaxTime = ctime(&maxTime);
}
printf("%s\n",strOfMaxTime);
return 0;
}
运行它会产生以下输出:
ctime error: Invalid argument
9223372036854775807 div by 2
ctime error: Invalid argument
4611686018427387903 div by 2
ctime error: Invalid argument
2305843009213693951 div by 2
ctime error: Invalid argument
1152921504606846975 div by 2
ctime error: Invalid argument
576460752303423487 div by 2
ctime error: Invalid argument
288230376151711743 div by 2
ctime error: Invalid argument
144115188075855871 div by 2
ctime error: Invalid argument
72057594037927935 div by 2
Sat Jun 12 22:26:07 1141709097
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.