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.

142 lines
6.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. RelativeTime::RelativeTime (const double secs) noexcept : numSeconds (secs) {}
  24. RelativeTime::RelativeTime (const RelativeTime& other) noexcept : numSeconds (other.numSeconds) {}
  25. RelativeTime::~RelativeTime() noexcept {}
  26. //==============================================================================
  27. RelativeTime RelativeTime::milliseconds (const int milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  28. RelativeTime RelativeTime::milliseconds (const int64 milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  29. RelativeTime RelativeTime::seconds (double s) noexcept { return RelativeTime (s); }
  30. RelativeTime RelativeTime::minutes (const double numberOfMinutes) noexcept { return RelativeTime (numberOfMinutes * 60.0); }
  31. RelativeTime RelativeTime::hours (const double numberOfHours) noexcept { return RelativeTime (numberOfHours * (60.0 * 60.0)); }
  32. RelativeTime RelativeTime::days (const double numberOfDays) noexcept { return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0)); }
  33. RelativeTime RelativeTime::weeks (const double numberOfWeeks) noexcept { return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0)); }
  34. //==============================================================================
  35. int64 RelativeTime::inMilliseconds() const noexcept { return (int64) (numSeconds * 1000.0); }
  36. double RelativeTime::inMinutes() const noexcept { return numSeconds / 60.0; }
  37. double RelativeTime::inHours() const noexcept { return numSeconds / (60.0 * 60.0); }
  38. double RelativeTime::inDays() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0); }
  39. double RelativeTime::inWeeks() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0 * 7.0); }
  40. //==============================================================================
  41. RelativeTime& RelativeTime::operator= (const RelativeTime& other) noexcept { numSeconds = other.numSeconds; return *this; }
  42. RelativeTime RelativeTime::operator+= (RelativeTime t) noexcept { numSeconds += t.numSeconds; return *this; }
  43. RelativeTime RelativeTime::operator-= (RelativeTime t) noexcept { numSeconds -= t.numSeconds; return *this; }
  44. RelativeTime RelativeTime::operator+= (const double secs) noexcept { numSeconds += secs; return *this; }
  45. RelativeTime RelativeTime::operator-= (const double secs) noexcept { numSeconds -= secs; return *this; }
  46. RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept { return t1 += t2; }
  47. RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept { return t1 -= t2; }
  48. bool operator== (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() == t2.inSeconds(); }
  49. bool operator!= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() != t2.inSeconds(); }
  50. bool operator> (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() > t2.inSeconds(); }
  51. bool operator< (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() < t2.inSeconds(); }
  52. bool operator>= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() >= t2.inSeconds(); }
  53. bool operator<= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() <= t2.inSeconds(); }
  54. //==============================================================================
  55. static void translateTimeField (String& result, int n, const char* singular, const char* plural)
  56. {
  57. result << TRANS (n == 1 ? singular : plural)
  58. .replace (n == 1 ? "1" : "2", String (n))
  59. << ' ';
  60. }
  61. String RelativeTime::getDescription (const String& returnValueForZeroTime) const
  62. {
  63. if (numSeconds < 0.001 && numSeconds > -0.001)
  64. return returnValueForZeroTime;
  65. String result;
  66. result.preallocateBytes (32);
  67. if (numSeconds < 0)
  68. result << '-';
  69. int fieldsShown = 0;
  70. int n = std::abs ((int) inWeeks());
  71. if (n > 0)
  72. {
  73. translateTimeField (result, n, NEEDS_TRANS("1 week"), NEEDS_TRANS("2 weeks"));
  74. ++fieldsShown;
  75. }
  76. n = std::abs ((int) inDays()) % 7;
  77. if (n > 0)
  78. {
  79. translateTimeField (result, n, NEEDS_TRANS("1 day"), NEEDS_TRANS("2 days"));
  80. ++fieldsShown;
  81. }
  82. if (fieldsShown < 2)
  83. {
  84. n = std::abs ((int) inHours()) % 24;
  85. if (n > 0)
  86. {
  87. translateTimeField (result, n, NEEDS_TRANS("1 hr"), NEEDS_TRANS("2 hrs"));
  88. ++fieldsShown;
  89. }
  90. if (fieldsShown < 2)
  91. {
  92. n = std::abs ((int) inMinutes()) % 60;
  93. if (n > 0)
  94. {
  95. translateTimeField (result, n, NEEDS_TRANS("1 min"), NEEDS_TRANS("2 mins"));
  96. ++fieldsShown;
  97. }
  98. if (fieldsShown < 2)
  99. {
  100. n = std::abs ((int) inSeconds()) % 60;
  101. if (n > 0)
  102. {
  103. translateTimeField (result, n, NEEDS_TRANS("1 sec"), NEEDS_TRANS("2 secs"));
  104. ++fieldsShown;
  105. }
  106. if (fieldsShown == 0)
  107. {
  108. n = std::abs ((int) inMilliseconds()) % 1000;
  109. if (n > 0)
  110. result << n << ' ' << TRANS ("ms");
  111. }
  112. }
  113. }
  114. }
  115. return result.trimEnd();
  116. }