[英]C time.h wrap around
I would like to assign max possible time to a time_t variable then convert it to a string and print out the result. 我想将最大可能时间分配给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's code is using char *ctime(const time_t *timer)
incorrectly. OP的代码错误地使用了char *ctime(const time_t *timer)
。
time_t maxTime;
char *strOfMaxTime;
// *strOfMaxTime = ctime(maxTime);
strOfMaxTime = ctime(&maxTime);
Yet simply assigning maxTime= LONG_LONG_MAX;
只需分配maxTime= LONG_LONG_MAX;
is not necessary the correct way to determine the maximum time a system can handle. 确定系统可以处理的最长时间的正确方法不是必需的。
Below is a trial and error method - likely with various implementation limitations. 以下是反复试验的方法-可能会受到各种实施限制。 localtime()
returns NULL
when time_t
is out of range. 当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;
}
Output (Note that 17:59:59 depends on timezone and the year 2,147,483,647 is the max 32-bit signed integer. YMMV.) 输出(请注意17:59:59取决于时区,并且2,147,483,647年是最大的32位有符号整数。YMMV。)
Tue Dec 31 17:59:59 2147483647
67767976233532799
From C Standards#7.27.1p4 来自C Standards#7.27.1p4
The range and precision of times representable in clock_t and time_t are implementation-defined . 在clock_t和time_t中可表示的时间范围和精度是实现定义的 。
First, you need to fix the issue in your program. 首先,您需要在程序中解决该问题。 The below statement must be giving error while compiling: 下面的语句在编译时必须给出错误:
*strOfMaxTime = ctime(maxTime);
Change this to: 更改为:
strOfMaxTime = ctime(&maxTime);
You can use perror()
to get the error message for the given input - LONG_LONG_MAX
, like this: 您可以使用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;
}
On my setup I am getting this output: 在我的设置中,我得到以下输出:
Error: Value too large to be stored in data type
Indeed, LONG_LONG_MAX
is invalid input. 实际上, LONG_LONG_MAX
是无效的输入。
As the standard mentions that the range of time_t
is implementation-defined, so if I give UINT_MAX
I am getting the output: 正如标准所提到的, time_t
的范围是实现定义的,因此,如果我给出UINT_MAX
我将得到输出:
0,Sun Feb 7 11:58:15 2106
This is wrong: 这是错误的:
*strOfMaxTime = ctime(maxTime);
This tries to assign the return value of ctime
(a pointer to a char) to *strOfMaxTime
a char. 这试图将ctime
(指向char的指针)的返回值分配给*strOfMaxTime
char。
Instead call: 而是调用:
strOfMaxTime = ctime(&maxTime);
And then check the return value of strOfMaxTime
as it may be NULL if ctime
fails to convert 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;
}
The maximum year is 2038, and this is known as Year 2038 problem: https://en.wikipedia.org/wiki/Year_2038_problem 最长年份为2038,这称为2038年问题: https : //en.wikipedia.org/wiki/Year_2038_problem
Numerous errors have been pointed out in other postings (assigning output of ctime()
to *strOfMaxTime
, LONG_LONG_MAX
, etc). 在其他发布中指出了许多错误(将ctime()
输出分配给*strOfMaxTime
, LONG_LONG_MAX
等)。 On my 64bit Ubuntu 16.04 Linux system, time_t
is defined as a long int
and a long int
is defined as 8 bytes as is a long long int
. 在我的64位Ubuntu 16.04 Linux系统上, time_t
定义为long int
, long int
定义为8个字节,而long long int
定义为8个字节。 However assigning LLONG_MAX
to maxTime
still causes ctime()
to fail. 但是,将LLONG_MAX
分配给maxTime
仍然会导致ctime()
失败。 So I modified your code to get a range of what the upper limit of valid values ctime()
will accept. 因此,我修改了您的代码以获取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;
}
Running it yields the following output: 运行它会产生以下输出:
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.