From ab774b814e48442f99f837859db0b804cbb09bbb Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 30 Jun 2014 09:48:26 +0100 Subject: [PATCH] Added a method Time::getCompilationDate() --- extras/Demo/Source/Demos/SystemInfoDemo.cpp | 1 + modules/juce_core/time/juce_Time.cpp | 53 +++++++++++++++------ modules/juce_core/time/juce_Time.h | 32 ++++++------- 3 files changed, 55 insertions(+), 31 deletions(-) diff --git a/extras/Demo/Source/Demos/SystemInfoDemo.cpp b/extras/Demo/Source/Demos/SystemInfoDemo.cpp index d006446556..d02b610663 100644 --- a/extras/Demo/Source/Demos/SystemInfoDemo.cpp +++ b/extras/Demo/Source/Demos/SystemInfoDemo.cpp @@ -97,6 +97,7 @@ static String getAllSystemInfo() << newLine << "Time and date: " << Time::getCurrentTime().toString (true, true) << newLine << "System up-time: " << RelativeTime::milliseconds ((int64) Time::getMillisecondCounterHiRes()).getDescription() << newLine + << "Compilation date: " << Time::getCompilationDate().toString (true, false) << newLine << newLine << "Operating system: " << SystemStats::getOperatingSystemName() << newLine << "Host name: " << SystemStats::getComputerName() << newLine diff --git a/modules/juce_core/time/juce_Time.cpp b/modules/juce_core/time/juce_Time.cpp index 959275be1f..fabcfde2ef 100644 --- a/modules/juce_core/time/juce_Time.cpp +++ b/modules/juce_core/time/juce_Time.cpp @@ -407,11 +407,11 @@ String Time::getWeekdayName (const bool threeLetterVersion) const return getWeekdayName (getDayOfWeek(), threeLetterVersion); } +static const char* const shortMonthNames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; +static const char* const longMonthNames[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; + String Time::getMonthName (int monthNumber, const bool threeLetterVersion) { - static const char* const shortMonthNames[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - static const char* const longMonthNames[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; - monthNumber %= 12; return TRANS (threeLetterVersion ? shortMonthNames [monthNumber] @@ -430,17 +430,40 @@ String Time::getWeekdayName (int day, const bool threeLetterVersion) } //============================================================================== -Time& Time::operator+= (RelativeTime delta) { millisSinceEpoch += delta.inMilliseconds(); return *this; } -Time& Time::operator-= (RelativeTime delta) { millisSinceEpoch -= delta.inMilliseconds(); return *this; } +Time& Time::operator+= (RelativeTime delta) noexcept { millisSinceEpoch += delta.inMilliseconds(); return *this; } +Time& Time::operator-= (RelativeTime delta) noexcept { millisSinceEpoch -= delta.inMilliseconds(); return *this; } -Time operator+ (Time time, RelativeTime delta) { Time t (time); return t += delta; } -Time operator- (Time time, RelativeTime delta) { Time t (time); return t -= delta; } -Time operator+ (RelativeTime delta, Time time) { Time t (time); return t += delta; } -const RelativeTime operator- (Time time1, Time time2) { return RelativeTime::milliseconds (time1.toMilliseconds() - time2.toMilliseconds()); } +Time operator+ (Time time, RelativeTime delta) noexcept { Time t (time); return t += delta; } +Time operator- (Time time, RelativeTime delta) noexcept { Time t (time); return t -= delta; } +Time operator+ (RelativeTime delta, Time time) noexcept { Time t (time); return t += delta; } +const RelativeTime operator- (Time time1, Time time2) noexcept { return RelativeTime::milliseconds (time1.toMilliseconds() - time2.toMilliseconds()); } -bool operator== (Time time1, Time time2) { return time1.toMilliseconds() == time2.toMilliseconds(); } -bool operator!= (Time time1, Time time2) { return time1.toMilliseconds() != time2.toMilliseconds(); } -bool operator< (Time time1, Time time2) { return time1.toMilliseconds() < time2.toMilliseconds(); } -bool operator> (Time time1, Time time2) { return time1.toMilliseconds() > time2.toMilliseconds(); } -bool operator<= (Time time1, Time time2) { return time1.toMilliseconds() <= time2.toMilliseconds(); } -bool operator>= (Time time1, Time time2) { return time1.toMilliseconds() >= time2.toMilliseconds(); } +bool operator== (Time time1, Time time2) noexcept { return time1.toMilliseconds() == time2.toMilliseconds(); } +bool operator!= (Time time1, Time time2) noexcept { return time1.toMilliseconds() != time2.toMilliseconds(); } +bool operator< (Time time1, Time time2) noexcept { return time1.toMilliseconds() < time2.toMilliseconds(); } +bool operator> (Time time1, Time time2) noexcept { return time1.toMilliseconds() > time2.toMilliseconds(); } +bool operator<= (Time time1, Time time2) noexcept { return time1.toMilliseconds() <= time2.toMilliseconds(); } +bool operator>= (Time time1, Time time2) noexcept { return time1.toMilliseconds() >= time2.toMilliseconds(); } + +static int getMonthNumberForCompileDate (const String& m) noexcept +{ + for (int i = 0; i < 12; ++i) + if (m.equalsIgnoreCase (shortMonthNames[i])) + return i; + + // If you hit this because your compiler has a non-standard __DATE__ format, + // let me know so we can add support for it! + jassertfalse; + return 0; +} + +Time Time::getCompilationDate() +{ + StringArray dateTokens; + dateTokens.addTokens (__DATE__, true); + dateTokens.removeEmptyStrings (true); + + return Time (dateTokens[2].getIntValue(), + getMonthNumberForCompileDate (dateTokens[0]), + dateTokens[1].getIntValue(), 12, 0); +} diff --git a/modules/juce_core/time/juce_Time.h b/modules/juce_core/time/juce_Time.h index f72fb2ce0f..4a35e0878e 100644 --- a/modules/juce_core/time/juce_Time.h +++ b/modules/juce_core/time/juce_Time.h @@ -253,9 +253,9 @@ public: //============================================================================== /** Adds a RelativeTime to this time. */ - Time& operator+= (RelativeTime delta); + Time& operator+= (RelativeTime delta) noexcept; /** Subtracts a RelativeTime from this time. */ - Time& operator-= (RelativeTime delta); + Time& operator-= (RelativeTime delta) noexcept; //============================================================================== /** Tries to set the computer's clock. @@ -272,8 +272,7 @@ public: @param threeLetterVersion if true, it'll return a 3-letter abbreviation, e.g. "Tue"; if false, it'll return the full version, e.g. "Tuesday". */ - static String getWeekdayName (int dayNumber, - bool threeLetterVersion); + static String getWeekdayName (int dayNumber, bool threeLetterVersion); /** Returns the name of one of the months. @@ -281,8 +280,7 @@ public: @param threeLetterVersion if true, it'll be a 3-letter abbreviation, e.g. "Jan"; if false it'll return the long form, e.g. "January" */ - static String getMonthName (int monthNumber, - bool threeLetterVersion); + static String getMonthName (int monthNumber, bool threeLetterVersion); //============================================================================== // Static methods for getting system timers directly.. @@ -370,6 +368,8 @@ public: */ static int64 secondsToHighResolutionTicks (double seconds) noexcept; + /** Returns a Time based on the value of the __DATE__ macro when this module was compiled */ + static Time getCompilationDate(); private: //============================================================================== @@ -378,27 +378,27 @@ private: //============================================================================== /** Adds a RelativeTime to a Time. */ -JUCE_API Time operator+ (Time time, RelativeTime delta); +JUCE_API Time operator+ (Time time, RelativeTime delta) noexcept; /** Adds a RelativeTime to a Time. */ -JUCE_API Time operator+ (RelativeTime delta, Time time); +JUCE_API Time operator+ (RelativeTime delta, Time time) noexcept; /** Subtracts a RelativeTime from a Time. */ -JUCE_API Time operator- (Time time, RelativeTime delta); +JUCE_API Time operator- (Time time, RelativeTime delta) noexcept; /** Returns the relative time difference between two times. */ -JUCE_API const RelativeTime operator- (Time time1, Time time2); +JUCE_API const RelativeTime operator- (Time time1, Time time2) noexcept; /** Compares two Time objects. */ -JUCE_API bool operator== (Time time1, Time time2); +JUCE_API bool operator== (Time time1, Time time2) noexcept; /** Compares two Time objects. */ -JUCE_API bool operator!= (Time time1, Time time2); +JUCE_API bool operator!= (Time time1, Time time2) noexcept; /** Compares two Time objects. */ -JUCE_API bool operator< (Time time1, Time time2); +JUCE_API bool operator< (Time time1, Time time2) noexcept; /** Compares two Time objects. */ -JUCE_API bool operator<= (Time time1, Time time2); +JUCE_API bool operator<= (Time time1, Time time2) noexcept; /** Compares two Time objects. */ -JUCE_API bool operator> (Time time1, Time time2); +JUCE_API bool operator> (Time time1, Time time2) noexcept; /** Compares two Time objects. */ -JUCE_API bool operator>= (Time time1, Time time2); +JUCE_API bool operator>= (Time time1, Time time2) noexcept; #endif // JUCE_TIME_H_INCLUDED