使用比较运算符比较 time_t 值 [英] Comparing time_t values using comparison operators
问题描述
我有 2 个 time_t
值,我想找出哪个值更大.
I have 2 time_t
values and I want to find out which one is greater.
time_t
在我的平台上内部是 __int64
.我可以使用 <
、>
和 ==
运算符来比较值吗?我不想知道两个时间值之间的差异.
time_t
is internally __int64
on my platform. Can I use <
, >
and ==
operators to compare the values?
I don't want to know the difference between the two time values.
代码只会在 Windows 上运行,所以我不关心可移植性.
The code will just run on Windows, so I don't care about portability.
以这种方式比较值是否正确?
Is it correct to compare the values in this way?
推荐答案
根据 C 标准的 7.27.1(3) 部分(在这种情况下 C++ 标准指的是)time_t
是实型,在6.2.5(17)中定义为整型或浮点型.
According to section 7.27.1(3) of the C standard (which the C++ standard refers to in this case) time_t
is a real type, which is defined in 6.2.5(17) as either integer or floating type.
只要您不明确假设整数(即通过执行 time_t t; int x = t;
),这可能会失去精度,您就是安全的.
As long as you don't explicitly assume integers (i.e. by doing time_t t; int x = t;
) which could lose precision you are safe.
编辑:我实际上收回了.如果你去这个链接,发布#6,你可以阅读以下内容:
EDIT: I take that back actually. If you go to this link, post #6, you can read the following:
仅仅因为 time_t 是算术,这并不意味着它存储时间作为随着时间的推移单调递增的值.事实上,我们的标准C 库不保证 t1
Just because time_t is arithmetic, that doesn't mean it stores time as monotone increasing values for advancing time. In fact, our Standard C library does not guarantee t1 < t2 for t2 later than t1. We fold a limited range of representable values different ways for different systems.
P.J.普拉格
Dinkumware, Ltd.
http://www.dinkumware.com
由于 Dinkumware 广泛用于 Windows 系统(虽然我不知道这是否也适用于 C++ 标准库)我认为这很相关.
Since Dinkumware is widely used on Windows systems (although I don't know whether this also holds for the C++ standard library) I consider this relevant.
tl;dr: 按照其他答案中的建议使用 std::difftime
.
tl;dr: Use std::difftime
as suggested in the other answers.
这篇关于使用比较运算符比较 time_t 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!