| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file LICENSE.rst or https://cmake.org/licensing for details. */
- #pragma once
- #include "cmConfigure.h" // IWYU pragma: keep
- #include <string>
- /** \class cmFileTime
- * \brief Abstract file modification time with support for comparison with
- * other file modification times.
- */
- class cmFileTime
- {
- public:
- using TimeType = long long;
- // unit time per second
- #if !defined(_WIN32) || defined(__CYGWIN__)
- // unit time is one nanosecond
- static constexpr TimeType UtPerS = 1000000000;
- #else
- // unit time is 100 nanosecond
- static constexpr TimeType UtPerS = 10000000;
- #endif
- /**
- * @brief Loads the file time of fileName from the file system
- * @return true on success
- */
- bool Load(std::string const& fileName);
- /**
- * @brief Return true if this is older than ftm
- */
- bool Older(cmFileTime ftm) const { return (this->Time - ftm.Time) < 0; }
- /**
- * @brief Return true if this is newer than ftm
- */
- bool Newer(cmFileTime ftm) const { return (ftm.Time - this->Time) < 0; }
- /**
- * @brief Return true if this is the same as ftm
- */
- bool Equal(cmFileTime ftm) const { return this->Time == ftm.Time; }
- /**
- * @brief Return true if this is not the same as ftm
- */
- bool Differ(cmFileTime ftm) const { return this->Time != ftm.Time; }
- /**
- * @brief Compare file modification times.
- * @return -1, 0, +1 for this older, same, or newer than ftm.
- */
- int Compare(cmFileTime ftm) const
- {
- TimeType const diff = this->Time - ftm.Time;
- if (diff == 0) {
- return 0;
- }
- return (diff < 0) ? -1 : 1;
- }
- // -- Comparison in second resolution
- /**
- * @brief Return true if this is at least a second older than ftm
- */
- bool OlderS(cmFileTime ftm) const
- {
- return (ftm.Time - this->Time) >= cmFileTime::UtPerS;
- }
- /**
- * @brief Return true if this is at least a second newer than ftm
- */
- bool NewerS(cmFileTime ftm) const
- {
- return (this->Time - ftm.Time) >= cmFileTime::UtPerS;
- }
- /**
- * @brief Return true if this is within the same second as ftm
- */
- bool EqualS(cmFileTime ftm) const
- {
- TimeType diff = this->Time - ftm.Time;
- if (diff < 0) {
- diff = -diff;
- }
- return (diff < cmFileTime::UtPerS);
- }
- /**
- * @brief Return true if this is older or newer than ftm by at least a second
- */
- bool DifferS(cmFileTime ftm) const
- {
- TimeType diff = this->Time - ftm.Time;
- if (diff < 0) {
- diff = -diff;
- }
- return (diff >= cmFileTime::UtPerS);
- }
- /**
- * @brief Compare file modification times.
- * @return -1: this at least a second older, 0: this within the same second
- * as ftm, +1: this at least a second newer than ftm.
- */
- int CompareS(cmFileTime ftm) const
- {
- TimeType const diff = this->Time - ftm.Time;
- if (diff <= -cmFileTime::UtPerS) {
- return -1;
- }
- if (diff >= cmFileTime::UtPerS) {
- return 1;
- }
- return 0;
- }
- /**
- * @brief The file modification time in unit time per second
- */
- TimeType GetTime() const { return this->Time; }
- private:
- TimeType Time = 0;
- };
|