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