cmFileTime.h 3.1 KB

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