resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmFileTime.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <string>
8
9 /** \class cmFileTime
10  * \brief Abstract file modification time with support for comparison with
11  *        other file modification times.
12  */
13 class cmFileTime
14 {
15 public:
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;
21 #else
22   // unit time is 100 nanosecond
23   static constexpr TimeType UtPerS = 10000000;
24 #endif
25   cmFileTime() = default;
26   ~cmFileTime() = default;
27   cmFileTime(const cmFileTime&) = default;
28   cmFileTime& operator=(const cmFileTime&) = default;
29
30   /**
31    * @brief Loads the file time of fileName from the file system
32    * @return true on success
33    */
34   bool Load(std::string const& fileName);
35
36   /**
37    * @brief Return true if this is older than ftm
38    */
39   bool Older(cmFileTime const& ftm) const
40   {
41     return (this->Time - ftm.Time) < 0;
42   }
43
44   /**
45    * @brief Return true if this is newer than ftm
46    */
47   bool Newer(cmFileTime const& ftm) const
48   {
49     return (ftm.Time - this->Time) < 0;
50   }
51
52   /**
53    * @brief Return true if this is the same as ftm
54    */
55   bool Equal(cmFileTime const& ftm) const { return this->Time == ftm.Time; }
56
57   /**
58    * @brief Return true if this is not the same as ftm
59    */
60   bool Differ(cmFileTime const& ftm) const { return this->Time != ftm.Time; }
61
62   /**
63    * @brief Compare file modification times.
64    * @return -1, 0, +1 for this older, same, or newer than ftm.
65    */
66   int Compare(cmFileTime const& ftm) const
67   {
68     TimeType const diff = this->Time - ftm.Time;
69     if (diff == 0) {
70       return 0;
71     }
72     return (diff < 0) ? -1 : 1;
73   }
74
75   // -- Comparison in second resolution
76
77   /**
78    * @brief Return true if this is at least a second older than ftm
79    */
80   bool OlderS(cmFileTime const& ftm) const
81   {
82     return (ftm.Time - this->Time) >= cmFileTime::UtPerS;
83   }
84
85   /**
86    * @brief Return true if this is at least a second newer than ftm
87    */
88   bool NewerS(cmFileTime const& ftm) const
89   {
90     return (this->Time - ftm.Time) >= cmFileTime::UtPerS;
91   }
92
93   /**
94    * @brief Return true if this is within the same second as ftm
95    */
96   bool EqualS(cmFileTime const& ftm) const
97   {
98     TimeType diff = this->Time - ftm.Time;
99     if (diff < 0) {
100       diff = -diff;
101     }
102     return (diff < cmFileTime::UtPerS);
103   }
104
105   /**
106    * @brief Return true if this is older or newer than ftm by at least a second
107    */
108   bool DifferS(cmFileTime const& ftm) const
109   {
110     TimeType diff = this->Time - ftm.Time;
111     if (diff < 0) {
112       diff = -diff;
113     }
114     return (diff >= cmFileTime::UtPerS);
115   }
116
117   /**
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.
121    */
122   int CompareS(cmFileTime const& ftm) const
123   {
124     TimeType const diff = this->Time - ftm.Time;
125     if (diff <= -cmFileTime::UtPerS) {
126       return -1;
127     }
128     if (diff >= cmFileTime::UtPerS) {
129       return 1;
130     }
131     return 0;
132   }
133
134   /**
135    * @brief The file modification time in unit time per second
136    */
137   TimeType GetTime() const { return this->Time; }
138
139 private:
140   TimeType Time = 0;
141 };