/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 | | /** \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 | | |
26 | | /** |
27 | | * @brief Loads the file time of fileName from the file system |
28 | | * @return true on success |
29 | | */ |
30 | | bool Load(std::string const& fileName); |
31 | | |
32 | | /** |
33 | | * @brief Return true if this is older than ftm |
34 | | */ |
35 | 0 | bool Older(cmFileTime ftm) const { return (this->Time - ftm.Time) < 0; } |
36 | | |
37 | | /** |
38 | | * @brief Return true if this is newer than ftm |
39 | | */ |
40 | 0 | bool Newer(cmFileTime ftm) const { return (ftm.Time - this->Time) < 0; } |
41 | | |
42 | | /** |
43 | | * @brief Return true if this is the same as ftm |
44 | | */ |
45 | 0 | bool Equal(cmFileTime ftm) const { return this->Time == ftm.Time; } |
46 | | |
47 | | /** |
48 | | * @brief Return true if this is not the same as ftm |
49 | | */ |
50 | 0 | bool Differ(cmFileTime ftm) const { return this->Time != ftm.Time; } |
51 | | |
52 | | /** |
53 | | * @brief Compare file modification times. |
54 | | * @return -1, 0, +1 for this older, same, or newer than ftm. |
55 | | */ |
56 | | int Compare(cmFileTime ftm) const |
57 | 0 | { |
58 | 0 | TimeType const diff = this->Time - ftm.Time; |
59 | 0 | if (diff == 0) { |
60 | 0 | return 0; |
61 | 0 | } |
62 | 0 | return (diff < 0) ? -1 : 1; |
63 | 0 | } |
64 | | |
65 | | // -- Comparison in second resolution |
66 | | |
67 | | /** |
68 | | * @brief Return true if this is at least a second older than ftm |
69 | | */ |
70 | | bool OlderS(cmFileTime ftm) const |
71 | 0 | { |
72 | 0 | return (ftm.Time - this->Time) >= cmFileTime::UtPerS; |
73 | 0 | } |
74 | | |
75 | | /** |
76 | | * @brief Return true if this is at least a second newer than ftm |
77 | | */ |
78 | | bool NewerS(cmFileTime ftm) const |
79 | 0 | { |
80 | 0 | return (this->Time - ftm.Time) >= cmFileTime::UtPerS; |
81 | 0 | } |
82 | | |
83 | | /** |
84 | | * @brief Return true if this is within the same second as ftm |
85 | | */ |
86 | | bool EqualS(cmFileTime ftm) const |
87 | 0 | { |
88 | 0 | TimeType diff = this->Time - ftm.Time; |
89 | 0 | if (diff < 0) { |
90 | 0 | diff = -diff; |
91 | 0 | } |
92 | 0 | return (diff < cmFileTime::UtPerS); |
93 | 0 | } |
94 | | |
95 | | /** |
96 | | * @brief Return true if this is older or newer than ftm by at least a second |
97 | | */ |
98 | | bool DifferS(cmFileTime ftm) const |
99 | 0 | { |
100 | 0 | TimeType diff = this->Time - ftm.Time; |
101 | 0 | if (diff < 0) { |
102 | 0 | diff = -diff; |
103 | 0 | } |
104 | 0 | return (diff >= cmFileTime::UtPerS); |
105 | 0 | } |
106 | | |
107 | | /** |
108 | | * @brief Compare file modification times. |
109 | | * @return -1: this at least a second older, 0: this within the same second |
110 | | * as ftm, +1: this at least a second newer than ftm. |
111 | | */ |
112 | | int CompareS(cmFileTime ftm) const |
113 | 0 | { |
114 | 0 | TimeType const diff = this->Time - ftm.Time; |
115 | 0 | if (diff <= -cmFileTime::UtPerS) { |
116 | 0 | return -1; |
117 | 0 | } |
118 | 0 | if (diff >= cmFileTime::UtPerS) { |
119 | 0 | return 1; |
120 | 0 | } |
121 | 0 | return 0; |
122 | 0 | } |
123 | | |
124 | | /** |
125 | | * @brief The file modification time in unit time per second |
126 | | */ |
127 | 0 | TimeType GetTime() const { return this->Time; } |
128 | | |
129 | | private: |
130 | | TimeType Time = 0; |
131 | | }; |