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.

139 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. RelativeTime::RelativeTime (const double secs) noexcept : numSeconds (secs) {}
  20. RelativeTime::RelativeTime (const RelativeTime& other) noexcept : numSeconds (other.numSeconds) {}
  21. RelativeTime::~RelativeTime() noexcept {}
  22. //==============================================================================
  23. RelativeTime RelativeTime::milliseconds (const int milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  24. RelativeTime RelativeTime::milliseconds (const int64 milliseconds) noexcept { return RelativeTime (milliseconds * 0.001); }
  25. RelativeTime RelativeTime::seconds (double s) noexcept { return RelativeTime (s); }
  26. RelativeTime RelativeTime::minutes (const double numberOfMinutes) noexcept { return RelativeTime (numberOfMinutes * 60.0); }
  27. RelativeTime RelativeTime::hours (const double numberOfHours) noexcept { return RelativeTime (numberOfHours * (60.0 * 60.0)); }
  28. RelativeTime RelativeTime::days (const double numberOfDays) noexcept { return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0)); }
  29. RelativeTime RelativeTime::weeks (const double numberOfWeeks) noexcept { return RelativeTime (numberOfWeeks * (60.0 * 60.0 * 24.0 * 7.0)); }
  30. //==============================================================================
  31. int64 RelativeTime::inMilliseconds() const noexcept { return (int64) (numSeconds * 1000.0); }
  32. double RelativeTime::inMinutes() const noexcept { return numSeconds / 60.0; }
  33. double RelativeTime::inHours() const noexcept { return numSeconds / (60.0 * 60.0); }
  34. double RelativeTime::inDays() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0); }
  35. double RelativeTime::inWeeks() const noexcept { return numSeconds / (60.0 * 60.0 * 24.0 * 7.0); }
  36. //==============================================================================
  37. RelativeTime& RelativeTime::operator= (const RelativeTime& other) noexcept { numSeconds = other.numSeconds; return *this; }
  38. RelativeTime RelativeTime::operator+= (RelativeTime t) noexcept { numSeconds += t.numSeconds; return *this; }
  39. RelativeTime RelativeTime::operator-= (RelativeTime t) noexcept { numSeconds -= t.numSeconds; return *this; }
  40. RelativeTime RelativeTime::operator+= (const double secs) noexcept { numSeconds += secs; return *this; }
  41. RelativeTime RelativeTime::operator-= (const double secs) noexcept { numSeconds -= secs; return *this; }
  42. JUCE_API RelativeTime JUCE_CALLTYPE operator+ (RelativeTime t1, RelativeTime t2) noexcept { return t1 += t2; }
  43. JUCE_API RelativeTime JUCE_CALLTYPE operator- (RelativeTime t1, RelativeTime t2) noexcept { return t1 -= t2; }
  44. JUCE_API bool JUCE_CALLTYPE operator== (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() == t2.inSeconds(); }
  45. JUCE_API bool JUCE_CALLTYPE operator!= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() != t2.inSeconds(); }
  46. JUCE_API bool JUCE_CALLTYPE operator> (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() > t2.inSeconds(); }
  47. JUCE_API bool JUCE_CALLTYPE operator< (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() < t2.inSeconds(); }
  48. JUCE_API bool JUCE_CALLTYPE operator>= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() >= t2.inSeconds(); }
  49. JUCE_API bool JUCE_CALLTYPE operator<= (RelativeTime t1, RelativeTime t2) noexcept { return t1.inSeconds() <= t2.inSeconds(); }
  50. //==============================================================================
  51. static void translateTimeField (String& result, int n, const char* singular, const char* plural)
  52. {
  53. result << TRANS (n == 1 ? singular : plural)
  54. .replace (n == 1 ? "1" : "2", String (n))
  55. << ' ';
  56. }
  57. String RelativeTime::getDescription (const String& returnValueForZeroTime) const
  58. {
  59. if (numSeconds < 0.001 && numSeconds > -0.001)
  60. return returnValueForZeroTime;
  61. String result;
  62. result.preallocateBytes (32);
  63. if (numSeconds < 0)
  64. result << '-';
  65. int fieldsShown = 0;
  66. int n = std::abs ((int) inWeeks());
  67. if (n > 0)
  68. {
  69. translateTimeField (result, n, NEEDS_TRANS("1 week"), NEEDS_TRANS("2 weeks"));
  70. ++fieldsShown;
  71. }
  72. n = std::abs ((int) inDays()) % 7;
  73. if (n > 0)
  74. {
  75. translateTimeField (result, n, NEEDS_TRANS("1 day"), NEEDS_TRANS("2 days"));
  76. ++fieldsShown;
  77. }
  78. if (fieldsShown < 2)
  79. {
  80. n = std::abs ((int) inHours()) % 24;
  81. if (n > 0)
  82. {
  83. translateTimeField (result, n, NEEDS_TRANS("1 hr"), NEEDS_TRANS("2 hrs"));
  84. ++fieldsShown;
  85. }
  86. if (fieldsShown < 2)
  87. {
  88. n = std::abs ((int) inMinutes()) % 60;
  89. if (n > 0)
  90. {
  91. translateTimeField (result, n, NEEDS_TRANS("1 min"), NEEDS_TRANS("2 mins"));
  92. ++fieldsShown;
  93. }
  94. if (fieldsShown < 2)
  95. {
  96. n = std::abs ((int) inSeconds()) % 60;
  97. if (n > 0)
  98. {
  99. translateTimeField (result, n, NEEDS_TRANS("1 sec"), NEEDS_TRANS("2 secs"));
  100. ++fieldsShown;
  101. }
  102. if (fieldsShown == 0)
  103. {
  104. n = std::abs ((int) inMilliseconds()) % 1000;
  105. if (n > 0)
  106. result << n << ' ' << TRANS ("ms");
  107. }
  108. }
  109. }
  110. }
  111. return result.trimEnd();
  112. }
  113. } // namespace juce