1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
10 * \brief Abstract file modification time with support for comparison with
11 * other file modification times.
16 using TimeType = long long;
17 // unit time per second
18 #if !defined(_WIN32) || defined(__CYGWIN__)
19 // unit time is one nanosecond
20 static constexpr TimeType UtPerS = 1000000000;
22 // unit time is 100 nanosecond
23 static constexpr TimeType UtPerS = 10000000;
25 cmFileTime() = default;
26 ~cmFileTime() = default;
27 cmFileTime(const cmFileTime&) = default;
28 cmFileTime& operator=(const cmFileTime&) = default;
31 * @brief Loads the file time of fileName from the file system
32 * @return true on success
34 bool Load(std::string const& fileName);
37 * @brief Return true if this is older than ftm
39 bool Older(cmFileTime const& ftm) const
41 return (this->Time - ftm.Time) < 0;
45 * @brief Return true if this is newer than ftm
47 bool Newer(cmFileTime const& ftm) const
49 return (ftm.Time - this->Time) < 0;
53 * @brief Return true if this is the same as ftm
55 bool Equal(cmFileTime const& ftm) const { return this->Time == ftm.Time; }
58 * @brief Return true if this is not the same as ftm
60 bool Differ(cmFileTime const& ftm) const { return this->Time != ftm.Time; }
63 * @brief Compare file modification times.
64 * @return -1, 0, +1 for this older, same, or newer than ftm.
66 int Compare(cmFileTime const& ftm) const
68 TimeType const diff = this->Time - ftm.Time;
72 return (diff < 0) ? -1 : 1;
75 // -- Comparison in second resolution
78 * @brief Return true if this is at least a second older than ftm
80 bool OlderS(cmFileTime const& ftm) const
82 return (ftm.Time - this->Time) >= cmFileTime::UtPerS;
86 * @brief Return true if this is at least a second newer than ftm
88 bool NewerS(cmFileTime const& ftm) const
90 return (this->Time - ftm.Time) >= cmFileTime::UtPerS;
94 * @brief Return true if this is within the same second as ftm
96 bool EqualS(cmFileTime const& ftm) const
98 TimeType diff = this->Time - ftm.Time;
102 return (diff < cmFileTime::UtPerS);
106 * @brief Return true if this is older or newer than ftm by at least a second
108 bool DifferS(cmFileTime const& ftm) const
110 TimeType diff = this->Time - ftm.Time;
114 return (diff >= cmFileTime::UtPerS);
118 * @brief Compare file modification times.
119 * @return -1: this at least a second older, 0: this within the same second
120 * as ftm, +1: this at least a second newer than ftm.
122 int CompareS(cmFileTime const& ftm) const
124 TimeType const diff = this->Time - ftm.Time;
125 if (diff <= -cmFileTime::UtPerS) {
128 if (diff >= cmFileTime::UtPerS) {
135 * @brief The file modification time in unit time per second
137 TimeType GetTime() const { return this->Time; }