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.

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