The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. RelativeTime::RelativeTime (const double seconds_) noexcept
  19. : seconds (seconds_)
  20. {
  21. }
  22. RelativeTime::RelativeTime (const RelativeTime& other) noexcept
  23. : seconds (other.seconds)
  24. {
  25. }
  26. RelativeTime::~RelativeTime() noexcept
  27. {
  28. }
  29. //==============================================================================
  30. const RelativeTime RelativeTime::milliseconds (const int milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  31. const RelativeTime RelativeTime::milliseconds (const int64 milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  32. const RelativeTime RelativeTime::minutes (const double numberOfMinutes) noexcept { return RelativeTime (numberOfMinutes * 60.0); }
  33. const RelativeTime RelativeTime::hours (const double numberOfHours) noexcept { return RelativeTime (numberOfHours * (60.0 * 60.0)); }
  34. const RelativeTime RelativeTime::days (const double numberOfDays) noexcept { return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0)); }
  35. const RelativeTime RelativeTime::weeks (const double numberOfWeeks) noexcept { return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0)); }
  36. //==============================================================================
  37. int64 RelativeTime::inMilliseconds() const noexcept { return (int64) (seconds * 1000.0); }
  38. double RelativeTime::inMinutes() const noexcept { return seconds / 60.0; }
  39. double RelativeTime::inHours() const noexcept { return seconds / (60.0 * 60.0); }
  40. double RelativeTime::inDays() const noexcept { return seconds / (60.0 * 60.0 * 24.0); }
  41. double RelativeTime::inWeeks() const noexcept { return seconds / (60.0 * 60.0 * 24.0 * 7.0); }
  42. //==============================================================================
  43. String RelativeTime::getDescription (const String& returnValueForZeroTime) const
  44. {
  45. if (seconds < 0.001 && seconds > -0.001)
  46. return returnValueForZeroTime;
  47. String result;
  48. result.preallocateBytes (32);
  49. if (seconds < 0)
  50. result << '-';
  51. int fieldsShown = 0;
  52. int n = std::abs ((int) inWeeks());
  53. if (n > 0)
  54. {
  55. result << n << (n == 1 ? TRANS(" week ")
  56. : TRANS(" weeks "));
  57. ++fieldsShown;
  58. }
  59. n = std::abs ((int) inDays()) % 7;
  60. if (n > 0)
  61. {
  62. result << n << (n == 1 ? TRANS(" day ")
  63. : TRANS(" days "));
  64. ++fieldsShown;
  65. }
  66. if (fieldsShown < 2)
  67. {
  68. n = std::abs ((int) inHours()) % 24;
  69. if (n > 0)
  70. {
  71. result << n << (n == 1 ? TRANS(" hr ")
  72. : TRANS(" hrs "));
  73. ++fieldsShown;
  74. }
  75. if (fieldsShown < 2)
  76. {
  77. n = std::abs ((int) inMinutes()) % 60;
  78. if (n > 0)
  79. {
  80. result << n << (n == 1 ? TRANS(" min ")
  81. : TRANS(" mins "));
  82. ++fieldsShown;
  83. }
  84. if (fieldsShown < 2)
  85. {
  86. n = std::abs ((int) inSeconds()) % 60;
  87. if (n > 0)
  88. {
  89. result << n << (n == 1 ? TRANS(" sec ")
  90. : TRANS(" secs "));
  91. ++fieldsShown;
  92. }
  93. if (fieldsShown == 0)
  94. {
  95. n = std::abs ((int) inMilliseconds()) % 1000;
  96. if (n > 0)
  97. result << n << TRANS(" ms");
  98. }
  99. }
  100. }
  101. }
  102. return result.trimEnd();
  103. }
  104. //==============================================================================
  105. RelativeTime& RelativeTime::operator= (const RelativeTime& other) noexcept
  106. {
  107. seconds = other.seconds;
  108. return *this;
  109. }
  110. //==============================================================================
  111. const RelativeTime& RelativeTime::operator+= (const RelativeTime& timeToAdd) noexcept
  112. {
  113. seconds += timeToAdd.seconds;
  114. return *this;
  115. }
  116. const RelativeTime& RelativeTime::operator-= (const RelativeTime& timeToSubtract) noexcept
  117. {
  118. seconds -= timeToSubtract.seconds;
  119. return *this;
  120. }
  121. const RelativeTime& RelativeTime::operator+= (const double secondsToAdd) noexcept
  122. {
  123. seconds += secondsToAdd;
  124. return *this;
  125. }
  126. const RelativeTime& RelativeTime::operator-= (const double secondsToSubtract) noexcept
  127. {
  128. seconds -= secondsToSubtract;
  129. return *this;
  130. }
  131. bool operator== (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() == t2.inSeconds(); }
  132. bool operator!= (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() != t2.inSeconds(); }
  133. bool operator> (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() > t2.inSeconds(); }
  134. bool operator< (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() < t2.inSeconds(); }
  135. bool operator>= (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() >= t2.inSeconds(); }
  136. bool operator<= (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() <= t2.inSeconds(); }
  137. RelativeTime operator+ (const RelativeTime& t1, const RelativeTime& t2) noexcept { RelativeTime t (t1); return t += t2; }
  138. RelativeTime operator- (const RelativeTime& t1, const RelativeTime& t2) noexcept { RelativeTime t (t1); return t -= t2; }