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.

174 lines
6.7KB

  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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. RelativeTime::RelativeTime (const double seconds_) noexcept
  21. : seconds (seconds_)
  22. {
  23. }
  24. RelativeTime::RelativeTime (const RelativeTime& other) noexcept
  25. : seconds (other.seconds)
  26. {
  27. }
  28. RelativeTime::~RelativeTime() noexcept
  29. {
  30. }
  31. //==============================================================================
  32. const RelativeTime RelativeTime::milliseconds (const int milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  33. const RelativeTime RelativeTime::milliseconds (const int64 milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  34. const RelativeTime RelativeTime::minutes (const double numberOfMinutes) noexcept { return RelativeTime (numberOfMinutes * 60.0); }
  35. const RelativeTime RelativeTime::hours (const double numberOfHours) noexcept { return RelativeTime (numberOfHours * (60.0 * 60.0)); }
  36. const RelativeTime RelativeTime::days (const double numberOfDays) noexcept { return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0)); }
  37. const RelativeTime RelativeTime::weeks (const double numberOfWeeks) noexcept { return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0)); }
  38. //==============================================================================
  39. int64 RelativeTime::inMilliseconds() const noexcept { return (int64) (seconds * 1000.0); }
  40. double RelativeTime::inMinutes() const noexcept { return seconds / 60.0; }
  41. double RelativeTime::inHours() const noexcept { return seconds / (60.0 * 60.0); }
  42. double RelativeTime::inDays() const noexcept { return seconds / (60.0 * 60.0 * 24.0); }
  43. double RelativeTime::inWeeks() const noexcept { return seconds / (60.0 * 60.0 * 24.0 * 7.0); }
  44. //==============================================================================
  45. String RelativeTime::getDescription (const String& returnValueForZeroTime) const
  46. {
  47. if (seconds < 0.001 && seconds > -0.001)
  48. return returnValueForZeroTime;
  49. String result;
  50. result.preallocateBytes (32);
  51. if (seconds < 0)
  52. result << '-';
  53. int fieldsShown = 0;
  54. int n = std::abs ((int) inWeeks());
  55. if (n > 0)
  56. {
  57. result << n << (n == 1 ? TRANS(" week ")
  58. : TRANS(" weeks "));
  59. ++fieldsShown;
  60. }
  61. n = std::abs ((int) inDays()) % 7;
  62. if (n > 0)
  63. {
  64. result << n << (n == 1 ? TRANS(" day ")
  65. : TRANS(" days "));
  66. ++fieldsShown;
  67. }
  68. if (fieldsShown < 2)
  69. {
  70. n = std::abs ((int) inHours()) % 24;
  71. if (n > 0)
  72. {
  73. result << n << (n == 1 ? TRANS(" hr ")
  74. : TRANS(" hrs "));
  75. ++fieldsShown;
  76. }
  77. if (fieldsShown < 2)
  78. {
  79. n = std::abs ((int) inMinutes()) % 60;
  80. if (n > 0)
  81. {
  82. result << n << (n == 1 ? TRANS(" min ")
  83. : TRANS(" mins "));
  84. ++fieldsShown;
  85. }
  86. if (fieldsShown < 2)
  87. {
  88. n = std::abs ((int) inSeconds()) % 60;
  89. if (n > 0)
  90. {
  91. result << n << (n == 1 ? TRANS(" sec ")
  92. : TRANS(" secs "));
  93. ++fieldsShown;
  94. }
  95. if (fieldsShown == 0)
  96. {
  97. n = std::abs ((int) inMilliseconds()) % 1000;
  98. if (n > 0)
  99. result << n << TRANS(" ms");
  100. }
  101. }
  102. }
  103. }
  104. return result.trimEnd();
  105. }
  106. //==============================================================================
  107. RelativeTime& RelativeTime::operator= (const RelativeTime& other) noexcept
  108. {
  109. seconds = other.seconds;
  110. return *this;
  111. }
  112. //==============================================================================
  113. const RelativeTime& RelativeTime::operator+= (const RelativeTime& timeToAdd) noexcept
  114. {
  115. seconds += timeToAdd.seconds;
  116. return *this;
  117. }
  118. const RelativeTime& RelativeTime::operator-= (const RelativeTime& timeToSubtract) noexcept
  119. {
  120. seconds -= timeToSubtract.seconds;
  121. return *this;
  122. }
  123. const RelativeTime& RelativeTime::operator+= (const double secondsToAdd) noexcept
  124. {
  125. seconds += secondsToAdd;
  126. return *this;
  127. }
  128. const RelativeTime& RelativeTime::operator-= (const double secondsToSubtract) noexcept
  129. {
  130. seconds -= secondsToSubtract;
  131. return *this;
  132. }
  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. bool operator>= (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() >= t2.inSeconds(); }
  138. bool operator<= (const RelativeTime& t1, const RelativeTime& t2) noexcept { return t1.inSeconds() <= t2.inSeconds(); }
  139. RelativeTime operator+ (const RelativeTime& t1, const RelativeTime& t2) noexcept { RelativeTime t (t1); return t += t2; }
  140. RelativeTime operator- (const RelativeTime& t1, const RelativeTime& t2) noexcept { RelativeTime t (t1); return t -= t2; }
  141. END_JUCE_NAMESPACE