juce_RelativeTime.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifndef JUCE_RELATIVETIME_H_INCLUDED
  22. #define JUCE_RELATIVETIME_H_INCLUDED
  23. //==============================================================================
  24. /** A relative measure of time.
  25. The time is stored as a number of seconds, at double-precision floating
  26. point accuracy, and may be positive or negative.
  27. If you need an absolute time, (i.e. a date + time), see the Time class.
  28. */
  29. class JUCE_API RelativeTime
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a RelativeTime.
  34. @param seconds the number of seconds, which may be +ve or -ve.
  35. @see milliseconds, minutes, hours, days, weeks
  36. */
  37. explicit RelativeTime (double seconds = 0.0) noexcept;
  38. /** Copies another relative time. */
  39. RelativeTime (const RelativeTime& other) noexcept;
  40. /** Copies another relative time. */
  41. RelativeTime& operator= (const RelativeTime& other) noexcept;
  42. /** Destructor. */
  43. ~RelativeTime() noexcept;
  44. //==============================================================================
  45. /** Creates a new RelativeTime object representing a number of milliseconds.
  46. @see seconds, minutes, hours, days, weeks
  47. */
  48. static RelativeTime milliseconds (int milliseconds) noexcept;
  49. /** Creates a new RelativeTime object representing a number of milliseconds.
  50. @see seconds, minutes, hours, days, weeks
  51. */
  52. static RelativeTime milliseconds (int64 milliseconds) noexcept;
  53. /** Creates a new RelativeTime object representing a number of seconds.
  54. @see milliseconds, minutes, hours, days, weeks
  55. */
  56. static RelativeTime seconds (double seconds) noexcept;
  57. /** Creates a new RelativeTime object representing a number of minutes.
  58. @see milliseconds, hours, days, weeks
  59. */
  60. static RelativeTime minutes (double numberOfMinutes) noexcept;
  61. /** Creates a new RelativeTime object representing a number of hours.
  62. @see milliseconds, minutes, days, weeks
  63. */
  64. static RelativeTime hours (double numberOfHours) noexcept;
  65. /** Creates a new RelativeTime object representing a number of days.
  66. @see milliseconds, minutes, hours, weeks
  67. */
  68. static RelativeTime days (double numberOfDays) noexcept;
  69. /** Creates a new RelativeTime object representing a number of weeks.
  70. @see milliseconds, minutes, hours, days
  71. */
  72. static RelativeTime weeks (double numberOfWeeks) noexcept;
  73. //==============================================================================
  74. /** Returns the number of milliseconds this time represents.
  75. @see milliseconds, inSeconds, inMinutes, inHours, inDays, inWeeks
  76. */
  77. int64 inMilliseconds() const noexcept;
  78. /** Returns the number of seconds this time represents.
  79. @see inMilliseconds, inMinutes, inHours, inDays, inWeeks
  80. */
  81. double inSeconds() const noexcept { return numSeconds; }
  82. /** Returns the number of minutes this time represents.
  83. @see inMilliseconds, inSeconds, inHours, inDays, inWeeks
  84. */
  85. double inMinutes() const noexcept;
  86. /** Returns the number of hours this time represents.
  87. @see inMilliseconds, inSeconds, inMinutes, inDays, inWeeks
  88. */
  89. double inHours() const noexcept;
  90. /** Returns the number of days this time represents.
  91. @see inMilliseconds, inSeconds, inMinutes, inHours, inWeeks
  92. */
  93. double inDays() const noexcept;
  94. /** Returns the number of weeks this time represents.
  95. @see inMilliseconds, inSeconds, inMinutes, inHours, inDays
  96. */
  97. double inWeeks() const noexcept;
  98. /** Returns a readable textual description of the time.
  99. The exact format of the string returned will depend on
  100. the magnitude of the time - e.g.
  101. "1 min 4 secs", "1 hr 45 mins", "2 weeks 5 days", "140 ms"
  102. so that only the two most significant units are printed.
  103. The returnValueForZeroTime value is the result that is returned if the
  104. length is zero. Depending on your application you might want to use this
  105. to return something more relevant like "empty" or "0 secs", etc.
  106. @see inMilliseconds, inSeconds, inMinutes, inHours, inDays, inWeeks
  107. */
  108. String getDescription (const String& returnValueForZeroTime = "0") const;
  109. //==============================================================================
  110. /** Adds another RelativeTime to this one. */
  111. RelativeTime operator+= (RelativeTime timeToAdd) noexcept;
  112. /** Subtracts another RelativeTime from this one. */
  113. RelativeTime operator-= (RelativeTime timeToSubtract) noexcept;
  114. /** Adds a number of seconds to this time. */
  115. RelativeTime operator+= (double secondsToAdd) noexcept;
  116. /** Subtracts a number of seconds from this time. */
  117. RelativeTime operator-= (double secondsToSubtract) noexcept;
  118. private:
  119. //==============================================================================
  120. double numSeconds;
  121. };
  122. //==============================================================================
  123. /** Compares two RelativeTimes. */
  124. bool operator== (RelativeTime t1, RelativeTime t2) noexcept;
  125. /** Compares two RelativeTimes. */
  126. bool operator!= (RelativeTime t1, RelativeTime t2) noexcept;
  127. /** Compares two RelativeTimes. */
  128. bool operator> (RelativeTime t1, RelativeTime t2) noexcept;
  129. /** Compares two RelativeTimes. */
  130. bool operator< (RelativeTime t1, RelativeTime t2) noexcept;
  131. /** Compares two RelativeTimes. */
  132. bool operator>= (RelativeTime t1, RelativeTime t2) noexcept;
  133. /** Compares two RelativeTimes. */
  134. bool operator<= (RelativeTime t1, RelativeTime t2) noexcept;
  135. //==============================================================================
  136. /** Adds two RelativeTimes together. */
  137. RelativeTime operator+ (RelativeTime t1, RelativeTime t2) noexcept;
  138. /** Subtracts two RelativeTimes. */
  139. RelativeTime operator- (RelativeTime t1, RelativeTime t2) noexcept;
  140. #endif // JUCE_RELATIVETIME_H_INCLUDED