Linux time_t 的最大值(struct timespec)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5617925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:36:42  来源:igfitidea点击:

Maximum values for time_t (struct timespec)

clinuxtime

提问by kingsmasher1

I am using the struct timespecstructure and here it is:

我正在使用struct timespec结构,这里是:

struct timespec {
           time_t tv_sec;                /* Seconds */
           long   tv_nsec;               /* Nanoseconds */
};

Thing is, user will be entering the values for each of these individual members, and i want to put a check a max. value the user can enter.

问题是,用户将输入每个成员的值,我想检查一个最大值。用户可以输入的值。

Can I take the max. value of time_tas int max value? i.e INT_MAXfor tv_secand LONG_MAX(defined in limits.h) for the tv_nsec? What will be the minimum acceptable values for both? Is it zero? I guess negative values can't be accepted? Just to add, these values will be using in a timer.

我可以拿最大吗?的值time_t作为INT最大值?即INT_MAX对于tv_secLONG_MAX(在limits.h 中定义)对于tv_nsec?两者的最小可接受值是多少?是零吗?我猜不能接受负值?补充一点,这些值将在计时器中使用。

P.S: Where is the typedef for time_t? Could not find it in time.h.

PS:typedef 在哪里time_t?在 time.h 中找不到它。

采纳答案by Ben Stott

A time_t is simply a long int.
It's defined in (on my Ubuntu linux system) /usr/include/time.h, however the definition stretches back all the way to /usr/include/bits/types.h, where __SLONGWORD_TYPE(which is what __TIME_T_TYPEis defined to) is defined.

time_t 只是一个长整数。
它是在(在我的 Ubuntu linux 系统上)/usr/include/time.h 中定义的,但是该定义一直延伸到 /usr/include/bits/types.h,其中__SLONGWORD_TYPE(这__TIME_T_TYPE是定义的内容)被定义.

The problem with simply checking if a value is greater than, say, LONG_MAX, is that once a value exceeds this value it will automatically wrap around and become negative. Thus you can't check to see if anything is greater than this value - the macro is defined as the largest value this type can take.

简单地检查一个值是否大于,比如说,的问题LONG_MAX是,一旦一个值超过这个值,它就会自动环绕并变成负数。因此,您无法检查是否有任何大于此值的值 - 宏被定义为此类型可以采用的最大值。

You don't really want a user to input these values - unless by 'user' you mean 'developer'. The only real "safe" way to test this would be to let the user input a string (c-style, of course) and then run two checks:
1) Check to see if the user entered more digits than is allowed (a cheap trick is int(log10(number)) + 1to count the amount of digits in a number).
2) If this is equal to the amount of digits, start comparing digit-by-digit. You can compare digit-by-digit by using a little bit of modulo arithmetic.

您真的不希望用户输入这些值 - 除非“用户”是指“开发人员”。测试这个唯一真正“安全”的方法是让用户输入一个字符串(当然是 c 样式),然后运行两个检查:
1)检查用户输入的数字是否超过允许的数字(便宜的诀窍是int(log10(number)) + 1计算数字中的位数)。
2) 如果这等于位数,则开始逐位比较。您可以通过使用一点模运算来逐位比较。

This is really the safest way to check whether or not the user inputs a number that's far too large. You won't run into any overflow issues this way, though it isterrifically tedious. Hope this helps.

这确实是检查用户输入的数字是否过大的最安全方法。你不会遇到任何问题溢出这种方式,虽然它得不得了的繁琐。希望这可以帮助。

回答by sverre

According to Wikipedia, time_tmay be an integer or floating point number, but is usually a 32-bit or 64-bit signed integer. I think the largest safe value you can assume is INT_MAX. For time_tat least negative numbers are legal and refer to before 1 January 1970.

根据维基百科,time_t可能是整数或浮点数,但通常是 32 位或 64 位有符号整数。我认为您可以假设的最大安全值是INT_MAX. 因为time_t至少负数是合法的,并且指的是 1970 年 1 月 1 日之前。

回答by Damon

I would not care so much about what goes into a time_t, but about what is reasonable. On any system that I have seen, a time_tcan encode timespans anywhere from 63 years to 1011years (pretty much every system I know uses 64 bit numbers ever since these geniusses came up with the Y2K world-will-end thing in 1999, it remains to be seen who will notice the much bigger "event" when the year 2038 goes past).

我不会太在意 a 的内容time_t,而是在意什么是合理的。在我见过的任何系统上,atime_t可以编码从 63 年到 10 11年的任何时间跨度(几乎我知道的每个系统都使用 64 位数字,因为这些天才在 1999 年提出了 Y2K 世界末日的事情,它当 2038 年过去时,谁会注意到更大的“事件”还有待观察)。

If you reasonably expectthat your program will be running for no more than 50 years, reject any value greater than 50*365*86400, or simply saturate the value. I don't expect any of the programs that I write now to be in use in 50 years (though I will not live to verify that).
On the other hand, if your system doesuse a 32 bit time_t, then it does not matter anyway, because the system time will have overflown in 50 years either way, so one can't construct a meaningful time anyway without shifting epoch.

如果您合理预期您的程序将运行不超过 50 年,请拒绝任何大于 50*365*86400 的值,或者简单地使该值饱和。我不指望我现在编写的任何程序在 50 年内会被使用(尽管我不会活着去验证这一点)。
另一方面,如果您的系统确实使用 32 位time_t,那么无论如何都无所谓,因为无论哪种方式,系统时间都会在 50 年内溢出,因此无论如何都无法在不移动纪元的情况下构建有意义的时间。

If you ask "how long do you want to pause?" and the user says "250 years", I would deem it not truly incorrect program behaviour if you said "yeah right, 50 will do, too". Because, hey, the difference really isn't observable.

如果你问“你想暂停多久?” 并且用户说“250 年”,如果您说“是的,50 年也可以”,我认为这并不是真正不正确的程序行为。因为,嘿,差异确实是不可观察的。

回答by swalog

Since people here are answering how to set the maximum time_tvalue, and make further guesswork as to its type, I thought I'd add the c++way to do it:

由于这里的人们正在回答如何设置最大值time_t,并对其类型进行进一步的猜测,我想我会添加c++这样做的方法:

#include <limits>
...
time_t maxTime = std::numeric_limits<time_t>::max();

回答by Ottavio Campana

Taken from ISO/IEC 9899:TC3§7.23

摘自ISO/IEC 9899:TC3§7.23

  1. The types declared are size_t(described in 7.17); clock_tand time_twhich are arithmetic types capable of representing times; and struct tmwhich holds the components of a calendar time, called the broken-down time .

  2. The range and precision of times representable in clock_tand time_tare implementation-defined

  1. 声明的类型是size_t(在 7.17 中描述);clock_ttime_t其能够表示时间的算术类型; 并 struct tm保存日历时间的组成部分,称为细分时间。

  2. 的范围和精度的时间中所能表述clock_ttime_t由实现定义

Therefore you cannot make any assumption on its max value based on the C standard.

因此,您不能根据 C 标准对其最大值进行任何假设。

If you need to write portable code, you'll probably use the autotols. Autoconfoffers the AC_CHECK_SIZEOFmacro that can help you dealing with the architecture-specific data limits.

如果您需要编写可移植的代码,您可能会使用自动工具。Autoconf提供了AC_CHECK_SIZEOF宏,可以帮助您处理特定于体系结构的数据限制。

回答by vinc17

Unfortunately the ISO C standard (currently C11) does not provide any way to get the maximum value of time_t. So, unless one uses tools like Autoconf providing information, one needs to make some assumptions.

不幸的是,ISO C 标准(当前为 C11)没有提供任何方法来获得 的最大值time_t。因此,除非使用 Autoconf 之类的工具提供信息,否则需要做出一些假设。

Assuming that time_tis an integer type without padding bits (which is the case on most platforms nowadays, if not all), one can probably take:

假设这time_t是一个没有填充位的整数类型(现在大多数平台都是这种情况,如果不是全部的话),人们可能会采取:

(((time_t) 1 << (sizeof(time_t) * CHAR_BIT - 2)) - 1) * 2 + 1

which is the maximum representable value for a signed integer type (but the fact that a value is representable in time_tdoes not mean that it is supported by the system as a time_tvalue).

这是有符号整数类型的最大可表示值(但值可表示的time_t事实并不意味着系统支持它作为time_t值)。

One may also want to detect whether time_tis an integer type. The ISO C standard specifies that time_tis a real type (Clause 7.27.1). By definition, a real typeis either an integer type or a real floating type(float, doubleor long double, and possibly others added in future versions of the standard, as mentioned in Clause 6.11.1). Thus, if time_tis not an integer type, it is necessarily a real floating type. As a consequence, one can detect whether time_tis an integer type with the test (time_t) 1 / 2 == 0.

可能还想检测是否time_t是整数类型。ISO C 标准指定这time_t是一个实数类型(第 7.27.1 条)。根据定义,实数类型要么是整数类型,要么是实数浮点类型floatdoublelong double,以及可能在标准的未来版本中添加的其他类型,如第 6.11.1 条所述)。因此,如果time_t不是整数类型,则必然是真正的浮点类型。因此,可以time_t通过 test检测是否是整数类型(time_t) 1 / 2 == 0

Note: The C standard does not strictly require that (T) 1 / 2be different from 0 if Tis a floating type, but if this is not the case, I suspect that such platforms would have serious issues with floating-point calculations.

注意:(T) 1 / 2如果T是浮点类型,C 标准并没有严格要求与 0 不同,但如果不是这种情况,我怀疑此类平台在浮点计算方面会出现严重问题。

回答by Borland P

For LINUX ctimemay accept any time_tthat will keep resulting year lower or equal to INT_MAX limit, that is for 64bit system:

对于 LINUXctime可以接受任何time_t将使结果年份低于或等于 INT_MAX 限制的值,即 64 位系统:

time_t maxtime = 67767976233521999;
printf("%s\n", ctime(&maxtime));

Will produce the following output: Tue Dec 31 23:59:59 2147483647

将产生以下输出: Tue Dec 31 23:59:59 2147483647