cmFileTime.h 3.2 KB

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