juce_RelativeTime.cpp 6.7 KB

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