I have 2 time_t
values and I want to find out which one is greater.
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.
The code will just run on Windows, so I don't care about portability.
Is it correct to compare the values in this way?
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.
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.
EDIT: I take that back actually. If you go to this link, post #6, you can read the following:
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. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
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: Use std::difftime
as suggested in the other answers.