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.

401 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_TIME_JUCEHEADER__
  19. #define __JUCE_TIME_JUCEHEADER__
  20. #include "juce_RelativeTime.h"
  21. //==============================================================================
  22. /**
  23. Holds an absolute date and time.
  24. Internally, the time is stored at millisecond precision.
  25. @see RelativeTime
  26. */
  27. class JUCE_API Time
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a Time object.
  32. This default constructor creates a time of 1st January 1970, (which is
  33. represented internally as 0ms).
  34. To create a time object representing the current time, use getCurrentTime().
  35. @see getCurrentTime
  36. */
  37. Time() noexcept;
  38. /** Creates a time based on a number of milliseconds.
  39. The internal millisecond count is set to 0 (1st January 1970). To create a
  40. time object set to the current time, use getCurrentTime().
  41. @param millisecondsSinceEpoch the number of milliseconds since the unix
  42. 'epoch' (midnight Jan 1st 1970).
  43. @see getCurrentTime, currentTimeMillis
  44. */
  45. explicit Time (int64 millisecondsSinceEpoch) noexcept;
  46. /** Creates a time from a set of date components.
  47. The timezone is assumed to be whatever the system is using as its locale.
  48. @param year the year, in 4-digit format, e.g. 2004
  49. @param month the month, in the range 0 to 11
  50. @param day the day of the month, in the range 1 to 31
  51. @param hours hours in 24-hour clock format, 0 to 23
  52. @param minutes minutes 0 to 59
  53. @param seconds seconds 0 to 59
  54. @param milliseconds milliseconds 0 to 999
  55. @param useLocalTime if true, encode using the current machine's local time; if
  56. false, it will always work in GMT.
  57. */
  58. Time (int year,
  59. int month,
  60. int day,
  61. int hours,
  62. int minutes,
  63. int seconds = 0,
  64. int milliseconds = 0,
  65. bool useLocalTime = true) noexcept;
  66. /** Creates a copy of another Time object. */
  67. Time (const Time& other) noexcept;
  68. /** Destructor. */
  69. ~Time() noexcept;
  70. /** Copies this time from another one. */
  71. Time& operator= (const Time& other) noexcept;
  72. //==============================================================================
  73. /** Returns a Time object that is set to the current system time.
  74. @see currentTimeMillis
  75. */
  76. static Time JUCE_CALLTYPE getCurrentTime() noexcept;
  77. /** Returns the time as a number of milliseconds.
  78. @returns the number of milliseconds this Time object represents, since
  79. midnight jan 1st 1970.
  80. @see getMilliseconds
  81. */
  82. int64 toMilliseconds() const noexcept { return millisSinceEpoch; }
  83. /** Returns the year.
  84. A 4-digit format is used, e.g. 2004.
  85. */
  86. int getYear() const noexcept;
  87. /** Returns the number of the month.
  88. The value returned is in the range 0 to 11.
  89. @see getMonthName
  90. */
  91. int getMonth() const noexcept;
  92. /** Returns the name of the month.
  93. @param threeLetterVersion if true, it'll be a 3-letter abbreviation, e.g. "Jan"; if false
  94. it'll return the long form, e.g. "January"
  95. @see getMonth
  96. */
  97. String getMonthName (bool threeLetterVersion) const;
  98. /** Returns the day of the month.
  99. The value returned is in the range 1 to 31.
  100. */
  101. int getDayOfMonth() const noexcept;
  102. /** Returns the number of the day of the week.
  103. The value returned is in the range 0 to 6 (0 = sunday, 1 = monday, etc).
  104. */
  105. int getDayOfWeek() const noexcept;
  106. /** Returns the name of the weekday.
  107. @param threeLetterVersion if true, it'll return a 3-letter abbreviation, e.g. "Tue"; if
  108. false, it'll return the full version, e.g. "Tuesday".
  109. */
  110. String getWeekdayName (bool threeLetterVersion) const;
  111. /** Returns the number of hours since midnight.
  112. This is in 24-hour clock format, in the range 0 to 23.
  113. @see getHoursInAmPmFormat, isAfternoon
  114. */
  115. int getHours() const noexcept;
  116. /** Returns true if the time is in the afternoon.
  117. So it returns true for "PM", false for "AM".
  118. @see getHoursInAmPmFormat, getHours
  119. */
  120. bool isAfternoon() const noexcept;
  121. /** Returns the hours in 12-hour clock format.
  122. This will return a value 1 to 12 - use isAfternoon() to find out
  123. whether this is in the afternoon or morning.
  124. @see getHours, isAfternoon
  125. */
  126. int getHoursInAmPmFormat() const noexcept;
  127. /** Returns the number of minutes, 0 to 59. */
  128. int getMinutes() const noexcept;
  129. /** Returns the number of seconds, 0 to 59. */
  130. int getSeconds() const noexcept;
  131. /** Returns the number of milliseconds, 0 to 999.
  132. Unlike toMilliseconds(), this just returns the position within the
  133. current second rather than the total number since the epoch.
  134. @see toMilliseconds
  135. */
  136. int getMilliseconds() const noexcept;
  137. /** Returns true if the local timezone uses a daylight saving correction. */
  138. bool isDaylightSavingTime() const noexcept;
  139. /** Returns a 3-character string to indicate the local timezone. */
  140. String getTimeZone() const noexcept;
  141. //==============================================================================
  142. /** Quick way of getting a string version of a date and time.
  143. For a more powerful way of formatting the date and time, see the formatted() method.
  144. @param includeDate whether to include the date in the string
  145. @param includeTime whether to include the time in the string
  146. @param includeSeconds if the time is being included, this provides an option not to include
  147. the seconds in it
  148. @param use24HourClock if the time is being included, sets whether to use am/pm or 24
  149. hour notation.
  150. @see formatted
  151. */
  152. String toString (bool includeDate,
  153. bool includeTime,
  154. bool includeSeconds = true,
  155. bool use24HourClock = false) const noexcept;
  156. /** Converts this date/time to a string with a user-defined format.
  157. This uses the C strftime() function to format this time as a string. To save you
  158. looking it up, these are the escape codes that strftime uses (other codes might
  159. work on some platforms and not others, but these are the common ones):
  160. %a is replaced by the locale's abbreviated weekday name.
  161. %A is replaced by the locale's full weekday name.
  162. %b is replaced by the locale's abbreviated month name.
  163. %B is replaced by the locale's full month name.
  164. %c is replaced by the locale's appropriate date and time representation.
  165. %d is replaced by the day of the month as a decimal number [01,31].
  166. %H is replaced by the hour (24-hour clock) as a decimal number [00,23].
  167. %I is replaced by the hour (12-hour clock) as a decimal number [01,12].
  168. %j is replaced by the day of the year as a decimal number [001,366].
  169. %m is replaced by the month as a decimal number [01,12].
  170. %M is replaced by the minute as a decimal number [00,59].
  171. %p is replaced by the locale's equivalent of either a.m. or p.m.
  172. %S is replaced by the second as a decimal number [00,61].
  173. %U is replaced by the week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
  174. %w is replaced by the weekday as a decimal number [0,6], with 0 representing Sunday.
  175. %W is replaced by the week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
  176. %x is replaced by the locale's appropriate date representation.
  177. %X is replaced by the locale's appropriate time representation.
  178. %y is replaced by the year without century as a decimal number [00,99].
  179. %Y is replaced by the year with century as a decimal number.
  180. %Z is replaced by the timezone name or abbreviation, or by no bytes if no timezone information exists.
  181. %% is replaced by %.
  182. @see toString
  183. */
  184. String formatted (const String& format) const;
  185. //==============================================================================
  186. /** Adds a RelativeTime to this time. */
  187. Time& operator+= (const RelativeTime& delta);
  188. /** Subtracts a RelativeTime from this time. */
  189. Time& operator-= (const RelativeTime& delta);
  190. //==============================================================================
  191. /** Tries to set the computer's clock.
  192. @returns true if this succeeds, although depending on the system, the
  193. application might not have sufficient privileges to do this.
  194. */
  195. bool setSystemTimeToThisTime() const;
  196. //==============================================================================
  197. /** Returns the name of a day of the week.
  198. @param dayNumber the day, 0 to 6 (0 = sunday, 1 = monday, etc)
  199. @param threeLetterVersion if true, it'll return a 3-letter abbreviation, e.g. "Tue"; if
  200. false, it'll return the full version, e.g. "Tuesday".
  201. */
  202. static String getWeekdayName (int dayNumber,
  203. bool threeLetterVersion);
  204. /** Returns the name of one of the months.
  205. @param monthNumber the month, 0 to 11
  206. @param threeLetterVersion if true, it'll be a 3-letter abbreviation, e.g. "Jan"; if false
  207. it'll return the long form, e.g. "January"
  208. */
  209. static String getMonthName (int monthNumber,
  210. bool threeLetterVersion);
  211. //==============================================================================
  212. // Static methods for getting system timers directly..
  213. /** Returns the current system time.
  214. Returns the number of milliseconds since midnight jan 1st 1970.
  215. Should be accurate to within a few millisecs, depending on platform,
  216. hardware, etc.
  217. */
  218. static int64 currentTimeMillis() noexcept;
  219. /** Returns the number of millisecs since a fixed event (usually system startup).
  220. This returns a monotonically increasing value which it unaffected by changes to the
  221. system clock. It should be accurate to within a few millisecs, depending on platform,
  222. hardware, etc.
  223. Being a 32-bit return value, it will of course wrap back to 0 after 2^32 seconds of
  224. uptime, so be careful to take that into account. If you need a 64-bit time, you can
  225. use currentTimeMillis() instead.
  226. @see getApproximateMillisecondCounter
  227. */
  228. static uint32 getMillisecondCounter() noexcept;
  229. /** Returns the number of millisecs since a fixed event (usually system startup).
  230. This has the same function as getMillisecondCounter(), but returns a more accurate
  231. value, using a higher-resolution timer if one is available.
  232. @see getMillisecondCounter
  233. */
  234. static double getMillisecondCounterHiRes() noexcept;
  235. /** Waits until the getMillisecondCounter() reaches a given value.
  236. This will make the thread sleep as efficiently as it can while it's waiting.
  237. */
  238. static void waitForMillisecondCounter (uint32 targetTime) noexcept;
  239. /** Less-accurate but faster version of getMillisecondCounter().
  240. This will return the last value that getMillisecondCounter() returned, so doesn't
  241. need to make a system call, but is less accurate - it shouldn't be more than
  242. 100ms away from the correct time, though, so is still accurate enough for a
  243. lot of purposes.
  244. @see getMillisecondCounter
  245. */
  246. static uint32 getApproximateMillisecondCounter() noexcept;
  247. //==============================================================================
  248. // High-resolution timers..
  249. /** Returns the current high-resolution counter's tick-count.
  250. This is a similar idea to getMillisecondCounter(), but with a higher
  251. resolution.
  252. @see getHighResolutionTicksPerSecond, highResolutionTicksToSeconds,
  253. secondsToHighResolutionTicks
  254. */
  255. static int64 getHighResolutionTicks() noexcept;
  256. /** Returns the resolution of the high-resolution counter in ticks per second.
  257. @see getHighResolutionTicks, highResolutionTicksToSeconds,
  258. secondsToHighResolutionTicks
  259. */
  260. static int64 getHighResolutionTicksPerSecond() noexcept;
  261. /** Converts a number of high-resolution ticks into seconds.
  262. @see getHighResolutionTicks, getHighResolutionTicksPerSecond,
  263. secondsToHighResolutionTicks
  264. */
  265. static double highResolutionTicksToSeconds (int64 ticks) noexcept;
  266. /** Converts a number seconds into high-resolution ticks.
  267. @see getHighResolutionTicks, getHighResolutionTicksPerSecond,
  268. highResolutionTicksToSeconds
  269. */
  270. static int64 secondsToHighResolutionTicks (double seconds) noexcept;
  271. private:
  272. //==============================================================================
  273. int64 millisSinceEpoch;
  274. };
  275. //==============================================================================
  276. /** Adds a RelativeTime to a Time. */
  277. JUCE_API Time operator+ (const Time& time, const RelativeTime& delta);
  278. /** Adds a RelativeTime to a Time. */
  279. JUCE_API Time operator+ (const RelativeTime& delta, const Time& time);
  280. /** Subtracts a RelativeTime from a Time. */
  281. JUCE_API Time operator- (const Time& time, const RelativeTime& delta);
  282. /** Returns the relative time difference between two times. */
  283. JUCE_API const RelativeTime operator- (const Time& time1, const Time& time2);
  284. /** Compares two Time objects. */
  285. JUCE_API bool operator== (const Time& time1, const Time& time2);
  286. /** Compares two Time objects. */
  287. JUCE_API bool operator!= (const Time& time1, const Time& time2);
  288. /** Compares two Time objects. */
  289. JUCE_API bool operator< (const Time& time1, const Time& time2);
  290. /** Compares two Time objects. */
  291. JUCE_API bool operator<= (const Time& time1, const Time& time2);
  292. /** Compares two Time objects. */
  293. JUCE_API bool operator> (const Time& time1, const Time& time2);
  294. /** Compares two Time objects. */
  295. JUCE_API bool operator>= (const Time& time1, const Time& time2);
  296. #endif // __JUCE_TIME_JUCEHEADER__