为time_t的最大值(结构的timespec) [英] Maximum values for time_t (struct timespec)

查看:1507
本文介绍了为time_t的最大值(结构的timespec)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是结构的timespec 结构,在这里它是:

I am using the struct timespec structure and here it is:

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.

我能把最大。 time_t的的价值为int最大值?即 INT_MAX tv_sec LONG_MAX (在规定范围内确定。高)为 tv_nsec ?什么将是双方可接受的最低值?难道是零?我想负值不能接受?刚添加,这些值将是使用一个定时器。

Can I take the max. value of time_t as int max value? i.e INT_MAX for tv_sec and 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.

P.S:哪里是为 time_t的 typedef的?找不到它time.h中。

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

推荐答案

一个time_t的仅仅是一个长整型。结果
它的定义(在我的Ubuntu Linux系统)/usr/include/time.h,但是定义可以追溯到所有/usr/include/bits/types.h,其中 __ SLONGWORD_TYPE (这是 __ TIME_T_TYPE 定义为)的定义。

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_TYPE is defined to) is defined.

与检查简单,如果值大于问题,比方说, LONG_MAX ,是一旦值超过这个值就会自动环绕,成为负值。因此,你不能检查,看看是否有任何事情比这个值大 - 中宏定义为这种类型的可以采取的最大值

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.

您真的不希望用户输入这些值 - 除非用户你的意思是开发者。唯一真正的安全的方法来测试,这将是让用户输入一个字符串(C-风格,当然),然后运行两个检查:结果
1)检查如果用户输入更多的数字比允许的(一个廉价的窍门是 INT(LOG10(数字))+ 1 来计算一个号码的数字量)。结果
2)如果这是等于的数位,开始比较数字逐位。您可以通过使用模运算的一点点比较位数按位数。

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)) + 1 to 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.

这是真的来检查用户是否输入一个数字,远远大过最安全的方式。你不会遇到任何问题的溢出这种方式,虽然它的的繁琐得不得了。
希望这有助于。

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 is terrifically tedious. Hope this helps.

这篇关于为time_t的最大值(结构的timespec)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆