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.

134 lines
6.3KB

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