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.

158 lines
7.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 (int milliseconds) noexcept { return RelativeTime ((double) milliseconds * 0.001); }
  24. RelativeTime RelativeTime::milliseconds (int64 milliseconds) noexcept { return RelativeTime ((double) milliseconds * 0.001); }
  25. RelativeTime RelativeTime::seconds (double s) noexcept { return RelativeTime (s); }
  26. RelativeTime RelativeTime::minutes (double numberOfMinutes) noexcept { return RelativeTime (numberOfMinutes * 60.0); }
  27. RelativeTime RelativeTime::hours (double numberOfHours) noexcept { return RelativeTime (numberOfHours * (60.0 * 60.0)); }
  28. RelativeTime RelativeTime::days (double numberOfDays) noexcept { return RelativeTime (numberOfDays * (60.0 * 60.0 * 24.0)); }
  29. RelativeTime RelativeTime::weeks (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+= (double secs) noexcept { numSeconds += secs; return *this; }
  41. RelativeTime RelativeTime::operator-= (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 String translateTimeField (int n, const char* singular, const char* plural)
  52. {
  53. return TRANS (n == 1 ? singular : plural).replace (n == 1 ? "1" : "2", String (n));
  54. }
  55. static String describeYears (int n) { return translateTimeField (n, NEEDS_TRANS("1 year"), NEEDS_TRANS("2 years")); }
  56. static String describeMonths (int n) { return translateTimeField (n, NEEDS_TRANS("1 month"), NEEDS_TRANS("2 months")); }
  57. static String describeWeeks (int n) { return translateTimeField (n, NEEDS_TRANS("1 week"), NEEDS_TRANS("2 weeks")); }
  58. static String describeDays (int n) { return translateTimeField (n, NEEDS_TRANS("1 day"), NEEDS_TRANS("2 days")); }
  59. static String describeHours (int n) { return translateTimeField (n, NEEDS_TRANS("1 hr"), NEEDS_TRANS("2 hrs")); }
  60. static String describeMinutes (int n) { return translateTimeField (n, NEEDS_TRANS("1 min"), NEEDS_TRANS("2 mins")); }
  61. static String describeSeconds (int n) { return translateTimeField (n, NEEDS_TRANS("1 sec"), NEEDS_TRANS("2 secs")); }
  62. String RelativeTime::getApproximateDescription() const
  63. {
  64. if (numSeconds <= 1.0)
  65. return "< 1 sec";
  66. auto weeks = (int) inWeeks();
  67. if (weeks > 52) return describeYears (weeks / 52);
  68. if (weeks > 8) return describeMonths ((weeks * 12) / 52);
  69. if (weeks > 1) return describeWeeks (weeks);
  70. auto days = (int) inWeeks();
  71. if (days > 1)
  72. return describeDays (days);
  73. auto hours = (int) inHours();
  74. if (hours > 0)
  75. return describeHours (hours);
  76. auto minutes = (int) inMinutes();
  77. if (minutes > 0)
  78. return describeMinutes (minutes);
  79. return describeSeconds ((int) numSeconds);
  80. }
  81. String RelativeTime::getDescription (const String& returnValueForZeroTime) const
  82. {
  83. if (std::abs (numSeconds) < 0.001)
  84. return returnValueForZeroTime;
  85. if (numSeconds < 0)
  86. return "-" + RelativeTime (-numSeconds).getDescription();
  87. StringArray fields;
  88. auto n = (int) inWeeks();
  89. if (n > 0)
  90. fields.add (describeWeeks (n));
  91. n = ((int) inDays()) % 7;
  92. if (n > 0)
  93. fields.add (describeDays (n));
  94. if (fields.size() < 2)
  95. {
  96. n = ((int) inHours()) % 24;
  97. if (n > 0)
  98. fields.add (describeHours (n));
  99. if (fields.size() < 2)
  100. {
  101. n = ((int) inMinutes()) % 60;
  102. if (n > 0)
  103. fields.add (describeMinutes (n));
  104. if (fields.size() < 2)
  105. {
  106. n = ((int) inSeconds()) % 60;
  107. if (n > 0)
  108. fields.add (describeSeconds (n));
  109. if (fields.isEmpty())
  110. fields.add (String (((int) inMilliseconds()) % 1000) + " " + TRANS ("ms"));
  111. }
  112. }
  113. }
  114. return fields.joinIntoString (" ");
  115. }
  116. } // namespace juce