cmFileTime.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFileTime_h
  4. #define cmFileTime_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <string>
  7. /** \class cmFileTime
  8. * \brief Abstract file modification time with support for comparison with
  9. * other file modification times.
  10. */
  11. class cmFileTime
  12. {
  13. public:
  14. typedef long long NSC;
  15. static constexpr NSC NsPerS = 1000000000;
  16. cmFileTime() = default;
  17. ~cmFileTime() = default;
  18. /**
  19. * @brief Loads the file time of fileName from the file system
  20. * @return true on success
  21. */
  22. bool Load(std::string const& fileName);
  23. /**
  24. * @brief Return true if this is older than ftm
  25. */
  26. bool Older(cmFileTime const& ftm) const { return (this->NS - ftm.NS) < 0; }
  27. /**
  28. * @brief Return true if this is newer than ftm
  29. */
  30. bool Newer(cmFileTime const& ftm) const { return (ftm.NS - this->NS) < 0; }
  31. /**
  32. * @brief Return true if this is the same as ftm
  33. */
  34. bool Equal(cmFileTime const& ftm) const { return this->NS == ftm.NS; }
  35. /**
  36. * @brief Return true if this is not the same as ftm
  37. */
  38. bool Differ(cmFileTime const& ftm) const { return this->NS != ftm.NS; }
  39. /**
  40. * @brief Compare file modification times.
  41. * @return -1, 0, +1 for this older, same, or newer than ftm.
  42. */
  43. int Compare(cmFileTime const& ftm)
  44. {
  45. NSC const diff = this->NS - ftm.NS;
  46. if (diff == 0) {
  47. return 0;
  48. }
  49. return (diff < 0) ? -1 : 1;
  50. }
  51. // -- Comparison in second resolution
  52. /**
  53. * @brief Return true if this is at least a second older than ftm
  54. */
  55. bool OlderS(cmFileTime const& ftm) const
  56. {
  57. return (ftm.NS - this->NS) >= cmFileTime::NsPerS;
  58. }
  59. /**
  60. * @brief Return true if this is at least a second newer than ftm
  61. */
  62. bool NewerS(cmFileTime const& ftm) const
  63. {
  64. return (this->NS - ftm.NS) >= cmFileTime::NsPerS;
  65. }
  66. /**
  67. * @brief Return true if this is within the same second as ftm
  68. */
  69. bool EqualS(cmFileTime const& ftm) const
  70. {
  71. NSC diff = this->NS - ftm.NS;
  72. if (diff < 0) {
  73. diff = -diff;
  74. }
  75. return (diff < cmFileTime::NsPerS);
  76. }
  77. /**
  78. * @brief Return true if this is older or newer than ftm by at least a second
  79. */
  80. bool DifferS(cmFileTime const& ftm) const
  81. {
  82. NSC diff = this->NS - ftm.NS;
  83. if (diff < 0) {
  84. diff = -diff;
  85. }
  86. return (diff >= cmFileTime::NsPerS);
  87. }
  88. /**
  89. * @brief Compare file modification times.
  90. * @return -1: this at least a second older, 0: this within the same second
  91. * as ftm, +1: this at least a second newer than ftm.
  92. */
  93. int CompareS(cmFileTime const& ftm) const
  94. {
  95. NSC const diff = this->NS - ftm.NS;
  96. if (diff <= -cmFileTime::NsPerS) {
  97. return -1;
  98. }
  99. if (diff >= cmFileTime::NsPerS) {
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. /**
  105. * @brief The file modification time in nanoseconds
  106. */
  107. NSC GetNS() const { return this->NS; }
  108. private:
  109. NSC NS = 0;
  110. };
  111. #endif