File: cmFileTime.h

package info (click to toggle)
cmake 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 152,348 kB
  • sloc: ansic: 403,894; cpp: 303,807; sh: 4,097; python: 3,582; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 108; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (131 lines) | stat: -rw-r--r-- 3,174 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* 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;
};