是什么difftime和之间的差值“ - ”? [英] what is the difference between difftime and '-'?

查看:174
本文介绍了是什么difftime和之间的差值“ - ”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有time_t类型的2个变量 - varEnd和varStart。
现在,为了看它们之间的区别
要么我可以做

I have 2 variables of type time_t - varEnd and varStart. Now in order to see the difference between them Either I can do

varEnd - varStart; 

difftime(varEnd, varStart);

和两个返回的秒数。

请让我知道,如果他们有什么区别吗?或者是推荐一个?

Please let me know, if they have any difference? or which is the recommended one?

推荐答案

语言指定 time_t的能够重新presenting倍的算术类型。它不要求它重新present时间以任何具体方式。

The language specifies that time_t is an arithmetic type capable of representing times. It doesn't require it to represent times in any particular way.

如果 time_t的重新presents时间,因为某些时刻的秒数,在 - 运营商将正确计算两个 time_t的值之间以秒为单位的差异。

If time_t represents time as the number of seconds since some moment, the - operator will correctly compute the difference in seconds between two time_t values.

如果它没有(比方说,如果粒度是一毫秒,或者如果一个 time_t的被分成组重新presenting年,月的位,天等),那么 - 运营商可以产生无意义的结果。

If it doesn't (say, if the granularity is one millisecond, or if the bits of a time_t are divided into groups representing years, months, days, etc.), then the - operator can yield meaningless results.

difftime() 功能,而另一方面,知道如何 time_t的重新presents时间,并使用该信息来计算秒的差异。

The difftime() function, on the other hand, "knows" how a time_t represents a time, and uses that information to compute the difference in seconds.

在大多数的实现,简单的减法和 difftime()碰巧做同样的事情 - 但只有 difftime()是保证在正常工作的所有的实现。

On most implementations, simple subtraction and difftime() happen to do the same thing -- but only difftime() is guaranteed to work correctly on all implementations.

另一个区别: difftime()返回浮点型的结果双击,而 - time_t的值类型产生的结果 time_t的。在大多数情况下,结果将被隐式转换到任何你把它分配到的类型,但是,如果 time_t的恰好是一个无符号整数类型,从稍后的时间减法较早的时间将产生一个非常大的值,而不是一个负值。每个系统我见过的农具 time_t的为32位或64位有符号整数类型,但使用一个无符号类型猫 - 一个原因是简单的减法 time_t的值是没有必要的意义。

Another difference: difftime() returns a result of the floating-point type double, while "-" on time_t values yields a result of type time_t. In most cases the result will be implicitly converted to the type of whatever you assign it to, but if time_t happens to be an unsigned integer type, subtraction of a later time from an earlier time will yield a very large value rather than a negative value. Every system I've seen implements time_t as a 32-bit or 64-bit signed integer type, but using an unsigned type is permitted -- one more reason that simple subtraction of time_t values isn't necessary meaningful.

这篇关于是什么difftime和之间的差值“ - ”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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