Comparing time_t values using comparison operators

余生长醉 提交于 2019-12-13 11:35:12

问题


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?


回答1:


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.




回答2:


The C standard (from which C++ inherits the definition of time_t) says only that time_t is an arithmetic type capable of representing times. It says nothing about how it does so. In principle, an implementation could define a time_t value as the number of seconds until some future date, or it could encode month, day, year, hours, minutes, and seconds in that order. Comparing time_t values is guaranteed to behave consistently (t0 < t1 && t1 < t2 implies t0 < t2, for example), but the result of a comparison doesn't necessarily say anything about the order of the times that are represented.

To be 100% portable, you can check the result of difftime().

But if you're willing to settle for about 99% portability, you should be safe in assuming that comparisons work as expected (that t0 < t1 implies t0 precedes t1 in real time). I've never heard of an implementation where that doesn't work. If it falls, you can complain to the implementers, but you can't say they've failed to confirm to the standard.




回答3:


Yes, you can use normal comparative operators in the limited case you give. There may be other systems where this doesn't work, but modern Linux and Windows OS's will work fine to compare time_t values.




回答4:


Assuming time_t has a high enough resolution for your use case then yes you can. If you need a higher resolution and Windows is your only concern then consider switching to the Win32 functions that deal in the FILETIME type.



来源:https://stackoverflow.com/questions/25198242/comparing-time-t-values-using-comparison-operators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!