30 #if JUCE_WINDOWS && JUCE_MINGW
47 if (localtime_r (&now, &result) ==
nullptr)
56 #if JUCE_WINDOWS && JUCE_MINGW
73 if (gmtime_r (&now, &result) ==
nullptr)
80 static int getUTCOffsetSeconds (
const int64 millis)
noexcept
88 static int extendedModulo (
const int64 value,
const int modulo)
noexcept
90 return (
int) (value >= 0 ? (value % modulo)
91 : (value - ((value / modulo) + 1) * modulo));
94 static String formatString (
const String& format,
const std::tm*
const tm)
105 if (tm->tm_year < -1900 || tm->tm_year > 8099)
109 for (
size_t bufferSize = 256; ; bufferSize += 256)
117 wcsftime (buffer, bufferSize - 1,
format.toWideCharPointer(), tm);
122 if (numChars > 0 ||
format.isEmpty())
129 static bool isLeapYear (
int year)
noexcept
131 return (year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0));
134 static int daysFromJan1 (
int year,
int month)
noexcept
136 const short dayOfYear[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334,
137 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
139 return dayOfYear [(isLeapYear (year) ? 12 : 0) + month];
142 static int64 daysFromYear0 (
int year)
noexcept
145 return 365 * year + (year / 400) - (year / 100) + (year / 4);
148 static int64 daysFrom1970 (
int year)
noexcept
150 return daysFromYear0 (year) - daysFromYear0 (1970);
153 static int64 daysFrom1970 (
int year,
int month)
noexcept
167 return daysFrom1970 (year) + daysFromJan1 (year, month);
174 return 24 * 3600 * (daysFrom1970 (t.tm_year + 1900, t.tm_mon) + (t.tm_mday - 1))
186 auto minutes = seconds / 60;
202 int hours,
int minutes,
int seconds,
int milliseconds,
206 t.tm_year = year - 1900;
215 : TimeHelpers::mktime_utc (t))
222 #if JUCE_WINDOWS && ! JUCE_MINGW
225 return ((
int64) t.time) * 1000 + t.millitm;
229 return ((
int64)
tv.tv_sec) * 1000 +
tv.tv_usec / 1000;
239uint32 juce_millisecondsSinceStartup() noexcept;
243 auto now = juce_millisecondsSinceStartup();
245 if (now < TimeHelpers::lastMSCounterValue.get())
250 if (now < TimeHelpers::lastMSCounterValue.get() - (
uint32) 1000)
251 TimeHelpers::lastMSCounterValue = now;
255 TimeHelpers::lastMSCounterValue = now;
263 auto t = TimeHelpers::lastMSCounterValue.get();
271 auto now = getMillisecondCounter();
286 for (
int i = 10; --i >= 0;)
295 return (
double)
ticks / (
double) getHighResolutionTicksPerSecond();
300 return (
int64) (seconds * (
double) getHighResolutionTicksPerSecond());
326 << (
mins < 10 ?
":0" :
":") <<
mins;
331 result << (
secs < 10 ?
":0" :
":") <<
secs;
343 std::tm t (TimeHelpers::millisToLocal (millisSinceEpoch));
344 return TimeHelpers::formatString (format, &t);
348int Time::getYear()
const noexcept {
return TimeHelpers::millisToLocal (millisSinceEpoch).tm_year + 1900; }
362 if (hours == 0)
return 12;
363 if (hours <= 12)
return hours;
375 return TimeHelpers::millisToLocal (millisSinceEpoch).tm_isdst != 0;
382 #if JUCE_WINDOWS && (JUCE_MSVC || JUCE_CLANG)
385 for (
int i = 0; i < 2; ++i)
387 char name[128] = { 0 };
404 if (zone[0].length() > 3
415 return TimeHelpers::getUTCOffsetSeconds (millisSinceEpoch);
426 :
"%04d%02d%02dT%02d%02d%06.03f",
440 for (
int i = numChars; --i >= 0;)
460 auto year = parseFixedSizeIntAndSkip (t, 4,
'-');
465 auto month = parseFixedSizeIntAndSkip (t, 2,
'-');
470 auto day = parseFixedSizeIntAndSkip (t, 2, 0);
475 int hours = 0, minutes = 0, milliseconds = 0;
480 hours = parseFixedSizeIntAndSkip (t, 2,
':');
485 minutes = parseFixedSizeIntAndSkip (t, 2,
':');
490 auto seconds = parseFixedSizeIntAndSkip (t, 2, 0);
495 if (*t ==
'.' || *t ==
',')
498 milliseconds = parseFixedSizeIntAndSkip (t, 3, 0);
500 if (milliseconds < 0)
504 milliseconds += 1000 * seconds;
507 auto nextChar = t.getAndAdvance();
509 if (nextChar ==
'-' || nextChar ==
'+')
511 auto offsetHours = parseFixedSizeIntAndSkip (t, 2,
':');
524 else if (nextChar != 0 && nextChar !=
'Z')
529 return Time (year, month - 1, day, hours, minutes, 0, milliseconds,
false);
542static const char*
const shortMonthNames[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
543static const char*
const longMonthNames[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };
555 static const char*
const shortDayNames[] = {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
556 static const char*
const longDayNames[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday" };
581static int getMonthNumberForCompileDate (
const String& m)
583 for (
int i = 0; i < 12; ++i)
584 if (m.equalsIgnoreCase (shortMonthNames[i]))
621 void runTest()
override
631 expect (t.getTimeZone().isNotEmpty());
632 expect (t.getUTCOffsetString (
true) ==
"Z" || t.getUTCOffsetString (
true).length() == 6);
633 expect (t.getUTCOffsetString (
false) ==
"Z" || t.getUTCOffsetString (
false).length() == 5);
635 expect (TimeHelpers::getUTCOffsetString (-(3 * 60 + 15) * 60,
true) ==
"-03:15");
636 expect (TimeHelpers::getUTCOffsetString (-(3 * 60 + 30) * 60,
true) ==
"-03:30");
637 expect (TimeHelpers::getUTCOffsetString (-(3 * 60 + 45) * 60,
true) ==
"-03:45");
639 expect (TimeHelpers::getUTCOffsetString ((3 * 60 + 15) * 60,
true) ==
"+03:15");
644 expect (
Time::fromISO8601 (
"2016-02-16") == Time (2016, 1, 16, 0, 0, 0, 0,
false));
645 expect (
Time::fromISO8601 (
"20160216Z") == Time (2016, 1, 16, 0, 0, 0, 0,
false));
647 expect (
Time::fromISO8601 (
"2016-02-16T15:03:57+00:00") == Time (2016, 1, 16, 15, 3, 57, 0,
false));
648 expect (
Time::fromISO8601 (
"20160216T150357+0000") == Time (2016, 1, 16, 15, 3, 57, 0,
false));
650 expect (
Time::fromISO8601 (
"2016-02-16T15:03:57.999+00:00") == Time (2016, 1, 16, 15, 3, 57, 999,
false));
651 expect (
Time::fromISO8601 (
"20160216T150357.999+0000") == Time (2016, 1, 16, 15, 3, 57, 999,
false));
652 expect (
Time::fromISO8601 (
"2016-02-16T15:03:57.999Z") == Time (2016, 1, 16, 15, 3, 57, 999,
false));
653 expect (
Time::fromISO8601 (
"2016-02-16T15:03:57,999Z") == Time (2016, 1, 16, 15, 3, 57, 999,
false));
654 expect (
Time::fromISO8601 (
"20160216T150357.999Z") == Time (2016, 1, 16, 15, 3, 57, 999,
false));
655 expect (
Time::fromISO8601 (
"20160216T150357,999Z") == Time (2016, 1, 16, 15, 3, 57, 999,
false));
657 expect (
Time::fromISO8601 (
"2016-02-16T15:03:57.999-02:30") == Time (2016, 1, 16, 17, 33, 57, 999,
false));
658 expect (
Time::fromISO8601 (
"2016-02-16T15:03:57,999-02:30") == Time (2016, 1, 16, 17, 33, 57, 999,
false));
659 expect (
Time::fromISO8601 (
"20160216T150357.999-0230") == Time (2016, 1, 16, 17, 33, 57, 999,
false));
660 expect (
Time::fromISO8601 (
"20160216T150357,999-0230") == Time (2016, 1, 16, 17, 33, 57, 999,
false));
662 expect (Time (1970, 0, 1, 0, 0, 0, 0,
false) == Time (0));
663 expect (Time (2106, 1, 7, 6, 28, 15, 0,
false) == Time (4294967295000));
664 expect (Time (2007, 10, 7, 1, 7, 20, 0,
false) == Time (1194397640000));
665 expect (Time (2038, 0, 19, 3, 14, 7, 0,
false) == Time (2147483647000));
666 expect (Time (2016, 2, 7, 11, 20, 8, 0,
false) == Time (1457349608000));
667 expect (Time (1969, 11, 31, 23, 59, 59, 0,
false) == Time (-1000));
668 expect (Time (1901, 11, 13, 20, 45, 53, 0,
false) == Time (-2147483647000));
670 expect (Time (1982, 1, 1, 12, 0, 0, 0,
true) +
RelativeTime::days (365) == Time (1983, 1, 1, 12, 0, 0, 0,
true));
671 expect (Time (1970, 1, 1, 12, 0, 0, 0,
true) +
RelativeTime::days (365) == Time (1971, 1, 1, 12, 0, 0, 0,
true));
672 expect (Time (2038, 1, 1, 12, 0, 0, 0,
true) +
RelativeTime::days (365) == Time (2039, 1, 1, 12, 0, 0, 0,
true));
674 expect (Time (1982, 1, 1, 12, 0, 0, 0,
false) +
RelativeTime::days (365) == Time (1983, 1, 1, 12, 0, 0, 0,
false));
675 expect (Time (1970, 1, 1, 12, 0, 0, 0,
false) +
RelativeTime::days (365) == Time (1971, 1, 1, 12, 0, 0, 0,
false));
676 expect (Time (2038, 1, 1, 12, 0, 0, 0,
false) +
RelativeTime::days (365) == Time (2039, 1, 1, 12, 0, 0, 0,
false));
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
A relative measure of time.
static RelativeTime milliseconds(int milliseconds) noexcept
Creates a new RelativeTime object representing a number of milliseconds.
static RelativeTime days(double numberOfDays) noexcept
Creates a new RelativeTime object representing a number of days.
A special array for holding a list of strings.
int addTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Breaks up a string into tokens and adds them to this array.
A simple class for holding temporary references to a string literal or String.
bool containsIgnoreCase(StringRef text) const noexcept
Tests whether the string contains another substring.
static String formatted(const String &formatStr, Args... args)
Creates a String from a printf-style parameter list.
bool contains(StringRef text) const noexcept
Tests whether the string contains another substring.
String trimEnd() const
Returns a copy of this string with any whitespace characters removed from the end.
String substring(int startIndex, int endIndex) const
Returns a subsection of the string.
static void JUCE_CALLTYPE yield()
Yields the current thread's CPU time-slot and allows a new thread to run.
static void JUCE_CALLTYPE sleep(int milliseconds)
Suspends the execution of the current thread until the specified timeout period has elapsed (note tha...
Holds an absolute date and time.
bool isDaylightSavingTime() const noexcept
Returns true if the local timezone uses a daylight saving correction.
static uint32 getApproximateMillisecondCounter() noexcept
Less-accurate but faster version of getMillisecondCounter().
String getTimeZone() const
Returns a 3-character string to indicate the local timezone.
Time()=default
Creates a Time object.
String getUTCOffsetString(bool includeDividerCharacters) const
Returns a string to indicate the offset of the local timezone from UTC.
int getHoursInAmPmFormat() const noexcept
Returns the hours in 12-hour clock format (in this machine's local timezone).
static Time fromISO8601(StringRef iso8601)
Parses an ISO-8601 string and returns it as a Time.
int getMilliseconds() const noexcept
Returns the number of milliseconds, 0 to 999.
String getWeekdayName(bool threeLetterVersion) const
Returns the name of the weekday (in this machine's local timezone).
int getDayOfMonth() const noexcept
Returns the day of the month (in this machine's local timezone).
static int64 currentTimeMillis() noexcept
Returns the current system time.
int getUTCOffsetSeconds() const noexcept
Returns the local timezone offset from UTC in seconds.
int getMonth() const noexcept
Returns the number of the month (in this machine's local timezone).
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
String toString(bool includeDate, bool includeTime, bool includeSeconds=true, bool use24HourClock=false) const
Returns a string version of this date and time, using this machine's local timezone.
bool isAfternoon() const noexcept
Returns true if the time is in the afternoon (in this machine's local timezone).
Time & operator-=(RelativeTime delta) noexcept
Subtracts a RelativeTime from this time.
int getYear() const noexcept
Returns the year (in this machine's local timezone).
static Time getCompilationDate()
Returns a Time based on the value of the DATE macro when this module was compiled.
static double highResolutionTicksToSeconds(int64 ticks) noexcept
Converts a number of high-resolution ticks into seconds.
int getDayOfYear() const noexcept
Returns the number of the day of the year (in this machine's local timezone).
static int64 secondsToHighResolutionTicks(double seconds) noexcept
Converts a number seconds into high-resolution ticks.
String getMonthName(bool threeLetterVersion) const
Returns the name of the month (in this machine's local timezone).
int getMinutes() const noexcept
Returns the number of minutes, 0 to 59 (in this machine's local timezone).
Time & operator+=(RelativeTime delta) noexcept
Adds a RelativeTime to this time.
static void waitForMillisecondCounter(uint32 targetTime) noexcept
Waits until the getMillisecondCounter() reaches a given value.
String formatted(const String &format) const
Converts this date/time to a string with a user-defined format.
String toISO8601(bool includeDividerCharacters) const
Returns a fully described string of this date and time in ISO-8601 format (using the local timezone).
int getHours() const noexcept
Returns the number of hours since midnight (in this machine's local timezone).
static uint32 getMillisecondCounter() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
int getSeconds() const noexcept
Returns the number of seconds, 0 to 59.
int getDayOfWeek() const noexcept
Returns the number of the day of the week (in this machine's local timezone).
This is a base class for classes that perform a unit test.
#define TRANS(stringLiteral)
Uses the LocalisedStrings class to translate the given string literal.
void zerostruct(Type &structure) noexcept
Overwrites a structure or object with zeros.
bool operator>(const var &v1, const var &v2)
Compares the values of two var objects, using the var::equals() comparison.
bool operator<=(const var &v1, const var &v2)
Compares the values of two var objects, using the var::equals() comparison.
wchar_t juce_wchar
A platform-independent 32-bit unicode character type.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
bool operator>=(const var &v1, const var &v2)
Compares the values of two var objects, using the var::equals() comparison.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
bool isPositiveAndBelow(Type1 valueToTest, Type2 upperLimit) noexcept
Returns true if a value is at least zero, and also below a specified upper limit.
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
JUCE_API RelativeTime JUCE_CALLTYPE operator-(RelativeTime t1, RelativeTime t2) noexcept
Subtracts two RelativeTimes.
long long int64
A platform-independent 64-bit integer type.