扩散时间和“-”之间有什么区别?

时间:2022-05-11 13:54:15

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

我有两个类型为time_t - varEnd和varStart的变量。为了看到它们之间的区别,我可以这样做

varEnd - varStart; 

or

difftime(varEnd, varStart);

and both returns number of seconds.

两者都返回秒数。

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

如果有什么不同,请告诉我。或者哪一个是推荐的?

2 个解决方案

#1


28  

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是一种能够表示时间的算术类型。它不要求它以任何特定的方式表示时间。

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表示时间为某个时刻之后的秒数,那么-操作符将正确地计算两个time_t值之间的秒数。

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.

如果它没有(例如,如果粒度是1毫秒,或者如果时间t的位被划分为代表年份、月、日等的组),那么-操作符可以产生无意义的结果。

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()函数“知道”time_t是如何表示时间的,并使用该信息来计算秒的差值。

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()恰好做了相同的事情——但是只有difftime()才能保证在所有实现上正确工作。

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()返回浮点类型double的结果,而“-”在time_t值上的结果是类型time_t的结果。在大多数情况下,结果将隐式地转换为您所赋值的类型,但是如果time_t恰好是无符号整数类型,则从较早时间减去较晚时间将产生一个非常大的值,而不是一个负值。我看到的每个系统都将time_t实现为32位或64位有符号整数类型,但是允许使用无符号类型——这也是为什么简单地减去time_t值没有意义的原因之一。

#2


1  

difftime() returns a floating point double, just subtracting them doesn't unless you cast them to double first.
source: here

difftime()返回一个浮点值为double的浮点数,除非你先将它们转换为double,否则仅仅减去它们是不可能的。来源:这里

#1


28  

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是一种能够表示时间的算术类型。它不要求它以任何特定的方式表示时间。

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表示时间为某个时刻之后的秒数,那么-操作符将正确地计算两个time_t值之间的秒数。

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.

如果它没有(例如,如果粒度是1毫秒,或者如果时间t的位被划分为代表年份、月、日等的组),那么-操作符可以产生无意义的结果。

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()函数“知道”time_t是如何表示时间的,并使用该信息来计算秒的差值。

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()恰好做了相同的事情——但是只有difftime()才能保证在所有实现上正确工作。

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()返回浮点类型double的结果,而“-”在time_t值上的结果是类型time_t的结果。在大多数情况下,结果将隐式地转换为您所赋值的类型,但是如果time_t恰好是无符号整数类型,则从较早时间减去较晚时间将产生一个非常大的值,而不是一个负值。我看到的每个系统都将time_t实现为32位或64位有符号整数类型,但是允许使用无符号类型——这也是为什么简单地减去time_t值没有意义的原因之一。

#2


1  

difftime() returns a floating point double, just subtracting them doesn't unless you cast them to double first.
source: here

difftime()返回一个浮点值为double的浮点数,除非你先将它们转换为double,否则仅仅减去它们是不可能的。来源:这里