juce_Time.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_TIME_H_INCLUDED
  22. #define JUCE_TIME_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Holds an absolute date and time.
  26. Internally, the time is stored at millisecond precision.
  27. @see RelativeTime
  28. */
  29. class JUCE_API Time
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a Time object.
  34. This default constructor creates a time of midnight Jan 1st 1970 UTC, (which is
  35. represented internally as 0ms).
  36. To create a time object representing the current time, use getCurrentTime().
  37. @see getCurrentTime
  38. */
  39. Time() noexcept;
  40. /** Creates a time based on a number of milliseconds.
  41. To create a time object set to the current time, use getCurrentTime().
  42. @param millisecondsSinceEpoch the number of milliseconds since the unix
  43. 'epoch' (midnight Jan 1st 1970 UTC).
  44. @see getCurrentTime, currentTimeMillis
  45. */
  46. explicit Time (int64 millisecondsSinceEpoch) noexcept;
  47. /** Creates a time from a set of date components.
  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, assume input is in this machine's local timezone
  56. if false, assume input is in UTC.
  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. This may not be monotonic, as the system time can change at any moment.
  75. You should therefore not use this method for measuring time intervals.
  76. @see currentTimeMillis
  77. */
  78. static Time JUCE_CALLTYPE getCurrentTime() noexcept;
  79. /** Returns the time as a number of milliseconds.
  80. @returns the number of milliseconds this Time object represents, since
  81. midnight Jan 1st 1970 UTC.
  82. @see getMilliseconds
  83. */
  84. int64 toMilliseconds() const noexcept { return millisSinceEpoch; }
  85. /** Returns the year (in this machine's local timezone).
  86. A 4-digit format is used, e.g. 2004.
  87. */
  88. int getYear() const noexcept;
  89. /** Returns the number of the month (in this machine's local timezone).
  90. The value returned is in the range 0 to 11.
  91. @see getMonthName
  92. */
  93. int getMonth() const noexcept;
  94. /** Returns the name of the month (in this machine's local timezone).
  95. @param threeLetterVersion if true, it'll be a 3-letter abbreviation, e.g. "Jan"; if false
  96. it'll return the long form, e.g. "January"
  97. @see getMonth
  98. */
  99. String getMonthName (bool threeLetterVersion) const;
  100. /** Returns the day of the month (in this machine's local timezone).
  101. The value returned is in the range 1 to 31.
  102. */
  103. int getDayOfMonth() const noexcept;
  104. /** Returns the number of the day of the week (in this machine's local timezone).
  105. The value returned is in the range 0 to 6 (0 = sunday, 1 = monday, etc).
  106. */
  107. int getDayOfWeek() const noexcept;
  108. /** Returns the number of the day of the year (in this machine's local timezone).
  109. The value returned is in the range 0 to 365.
  110. */
  111. int getDayOfYear() const noexcept;
  112. /** Returns the name of the weekday (in this machine's local timezone).
  113. @param threeLetterVersion if true, it'll return a 3-letter abbreviation, e.g. "Tue"; if
  114. false, it'll return the full version, e.g. "Tuesday".
  115. */
  116. String getWeekdayName (bool threeLetterVersion) const;
  117. /** Returns the number of hours since midnight (in this machine's local timezone).
  118. This is in 24-hour clock format, in the range 0 to 23.
  119. @see getHoursInAmPmFormat, isAfternoon
  120. */
  121. int getHours() const noexcept;
  122. /** Returns true if the time is in the afternoon (in this machine's local timezone).
  123. @returns true for "PM", false for "AM".
  124. @see getHoursInAmPmFormat, getHours
  125. */
  126. bool isAfternoon() const noexcept;
  127. /** Returns the hours in 12-hour clock format (in this machine's local timezone).
  128. This will return a value 1 to 12 - use isAfternoon() to find out
  129. whether this is in the afternoon or morning.
  130. @see getHours, isAfternoon
  131. */
  132. int getHoursInAmPmFormat() const noexcept;
  133. /** Returns the number of minutes, 0 to 59 (in this machine's local timezone). */
  134. int getMinutes() const noexcept;
  135. /** Returns the number of seconds, 0 to 59. */
  136. int getSeconds() const noexcept;
  137. /** Returns the number of milliseconds, 0 to 999.
  138. Unlike toMilliseconds(), this just returns the position within the
  139. current second rather than the total number since the epoch.
  140. @see toMilliseconds
  141. */
  142. int getMilliseconds() const noexcept;
  143. /** Returns true if the local timezone uses a daylight saving correction. */
  144. bool isDaylightSavingTime() const noexcept;
  145. //==============================================================================
  146. /** Returns a 3-character string to indicate the local timezone. */
  147. String getTimeZone() const noexcept;
  148. /** Returns the local timezone offset from UTC in seconds. */
  149. int getUTCOffsetSeconds() const noexcept;
  150. /** Returns a string to indicate the offset of the local timezone from UTC.
  151. @returns "+XX:XX", "-XX:XX" or "Z"
  152. @param includeDividerCharacters whether to include or omit the ":" divider in the string
  153. */
  154. String getUTCOffsetString (bool includeDividerCharacters) const;
  155. //==============================================================================
  156. /** Returns a string version of this date and time, using this machine's local timezone.
  157. For a more powerful way of formatting the date and time, see the formatted() method.
  158. @param includeDate whether to include the date in the string
  159. @param includeTime whether to include the time in the string
  160. @param includeSeconds if the time is being included, this provides an option not to include
  161. the seconds in it
  162. @param use24HourClock if the time is being included, sets whether to use am/pm or 24
  163. hour notation.
  164. @see formatted
  165. */
  166. String toString (bool includeDate,
  167. bool includeTime,
  168. bool includeSeconds = true,
  169. bool use24HourClock = false) const noexcept;
  170. /** Converts this date/time to a string with a user-defined format.
  171. This uses the C strftime() function to format this time as a string. To save you
  172. looking it up, these are the escape codes that strftime uses (other codes might
  173. work on some platforms and not others, but these are the common ones):
  174. - %a is replaced by the locale's abbreviated weekday name.
  175. - %A is replaced by the locale's full weekday name.
  176. - %b is replaced by the locale's abbreviated month name.
  177. - %B is replaced by the locale's full month name.
  178. - %c is replaced by the locale's appropriate date and time representation.
  179. - %d is replaced by the day of the month as a decimal number [01,31].
  180. - %H is replaced by the hour (24-hour clock) as a decimal number [00,23].
  181. - %I is replaced by the hour (12-hour clock) as a decimal number [01,12].
  182. - %j is replaced by the day of the year as a decimal number [001,366].
  183. - %m is replaced by the month as a decimal number [01,12].
  184. - %M is replaced by the minute as a decimal number [00,59].
  185. - %p is replaced by the locale's equivalent of either a.m. or p.m.
  186. - %S is replaced by the second as a decimal number [00,61].
  187. - %U is replaced by the week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
  188. - %w is replaced by the weekday as a decimal number [0,6], with 0 representing Sunday.
  189. - %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.
  190. - %x is replaced by the locale's appropriate date representation.
  191. - %X is replaced by the locale's appropriate time representation.
  192. - %y is replaced by the year without century as a decimal number [00,99].
  193. - %Y is replaced by the year with century as a decimal number.
  194. - %Z is replaced by the timezone name or abbreviation, or by no bytes if no timezone information exists.
  195. - %% is replaced by %.
  196. @see toString
  197. */
  198. String formatted (const String& format) const;
  199. //==============================================================================
  200. /** Returns a fully described string of this date and time in ISO-8601 format
  201. (using the local timezone).
  202. @param includeDividerCharacters whether to include or omit the "-" and ":"
  203. dividers in the string
  204. */
  205. String toISO8601 (bool includeDividerCharacters) const;
  206. /** Parses an ISO-8601 string and returns it as a Time. */
  207. static Time fromISO8601 (StringRef iso8601) noexcept;
  208. //==============================================================================
  209. /** Adds a RelativeTime to this time. */
  210. Time& operator+= (RelativeTime delta) noexcept;
  211. /** Subtracts a RelativeTime from this time. */
  212. Time& operator-= (RelativeTime delta) noexcept;
  213. //==============================================================================
  214. /** Tries to set the computer's clock.
  215. @returns true if this succeeds, although depending on the system, the
  216. application might not have sufficient privileges to do this.
  217. */
  218. bool setSystemTimeToThisTime() const;
  219. //==============================================================================
  220. /** Returns the name of a day of the week.
  221. @param dayNumber the day, 0 to 6 (0 = sunday, 1 = monday, etc)
  222. @param threeLetterVersion if true, it'll return a 3-letter abbreviation, e.g. "Tue"; if
  223. false, it'll return the full version, e.g. "Tuesday".
  224. */
  225. static String getWeekdayName (int dayNumber, bool threeLetterVersion);
  226. /** Returns the name of one of the months.
  227. @param monthNumber the month, 0 to 11
  228. @param threeLetterVersion if true, it'll be a 3-letter abbreviation, e.g. "Jan"; if false
  229. it'll return the long form, e.g. "January"
  230. */
  231. static String getMonthName (int monthNumber, bool threeLetterVersion);
  232. //==============================================================================
  233. // Static methods for getting system timers directly..
  234. /** Returns the current system time.
  235. Returns the number of milliseconds since midnight Jan 1st 1970 UTC.
  236. Should be accurate to within a few millisecs, depending on platform,
  237. hardware, etc.
  238. */
  239. static int64 currentTimeMillis() noexcept;
  240. /** Returns the number of millisecs since a fixed event (usually system startup).
  241. This returns a monotonically increasing value which it unaffected by changes to the
  242. system clock. It should be accurate to within a few millisecs, depending on platform,
  243. hardware, etc.
  244. Being a 32-bit return value, it will of course wrap back to 0 after 2^32 seconds of
  245. uptime, so be careful to take that into account. If you need a 64-bit time, you can
  246. use currentTimeMillis() instead.
  247. @see getApproximateMillisecondCounter
  248. */
  249. static uint32 getMillisecondCounter() noexcept;
  250. /** Returns the number of millisecs since a fixed event (usually system startup).
  251. This has the same function as getMillisecondCounter(), but returns a more accurate
  252. value, using a higher-resolution timer if one is available.
  253. @see getMillisecondCounter
  254. */
  255. static double getMillisecondCounterHiRes() noexcept;
  256. /** Waits until the getMillisecondCounter() reaches a given value.
  257. This will make the thread sleep as efficiently as it can while it's waiting.
  258. */
  259. static void waitForMillisecondCounter (uint32 targetTime) noexcept;
  260. /** Less-accurate but faster version of getMillisecondCounter().
  261. This will return the last value that getMillisecondCounter() returned, so doesn't
  262. need to make a system call, but is less accurate - it shouldn't be more than
  263. 100ms away from the correct time, though, so is still accurate enough for a
  264. lot of purposes.
  265. @see getMillisecondCounter
  266. */
  267. static uint32 getApproximateMillisecondCounter() noexcept;
  268. //==============================================================================
  269. // High-resolution timers..
  270. /** Returns the current high-resolution counter's tick-count.
  271. This is a similar idea to getMillisecondCounter(), but with a higher
  272. resolution.
  273. @see getHighResolutionTicksPerSecond, highResolutionTicksToSeconds,
  274. secondsToHighResolutionTicks
  275. */
  276. static int64 getHighResolutionTicks() noexcept;
  277. /** Returns the resolution of the high-resolution counter in ticks per second.
  278. @see getHighResolutionTicks, highResolutionTicksToSeconds,
  279. secondsToHighResolutionTicks
  280. */
  281. static int64 getHighResolutionTicksPerSecond() noexcept;
  282. /** Converts a number of high-resolution ticks into seconds.
  283. @see getHighResolutionTicks, getHighResolutionTicksPerSecond,
  284. secondsToHighResolutionTicks
  285. */
  286. static double highResolutionTicksToSeconds (int64 ticks) noexcept;
  287. /** Converts a number seconds into high-resolution ticks.
  288. @see getHighResolutionTicks, getHighResolutionTicksPerSecond,
  289. highResolutionTicksToSeconds
  290. */
  291. static int64 secondsToHighResolutionTicks (double seconds) noexcept;
  292. /** Returns a Time based on the value of the __DATE__ macro when this module was compiled */
  293. static Time getCompilationDate();
  294. private:
  295. //==============================================================================
  296. int64 millisSinceEpoch;
  297. };
  298. //==============================================================================
  299. /** Adds a RelativeTime to a Time. */
  300. JUCE_API Time operator+ (Time time, RelativeTime delta) noexcept;
  301. /** Adds a RelativeTime to a Time. */
  302. JUCE_API Time operator+ (RelativeTime delta, Time time) noexcept;
  303. /** Subtracts a RelativeTime from a Time. */
  304. JUCE_API Time operator- (Time time, RelativeTime delta) noexcept;
  305. /** Returns the relative time difference between two times. */
  306. JUCE_API const RelativeTime operator- (Time time1, Time time2) noexcept;
  307. /** Compares two Time objects. */
  308. JUCE_API bool operator== (Time time1, Time time2) noexcept;
  309. /** Compares two Time objects. */
  310. JUCE_API bool operator!= (Time time1, Time time2) noexcept;
  311. /** Compares two Time objects. */
  312. JUCE_API bool operator< (Time time1, Time time2) noexcept;
  313. /** Compares two Time objects. */
  314. JUCE_API bool operator<= (Time time1, Time time2) noexcept;
  315. /** Compares two Time objects. */
  316. JUCE_API bool operator> (Time time1, Time time2) noexcept;
  317. /** Compares two Time objects. */
  318. JUCE_API bool operator>= (Time time1, Time time2) noexcept;
  319. #endif // JUCE_TIME_H_INCLUDED