Author: pulkomandy Date: 2010-02-17 16:35:28 +0100 (Wed, 17 Feb 2010) New Revision: 35506 Changeset: http://dev.haiku-os.org/changeset/35506/haiku Modified: haiku/trunk/headers/os/locale/FloatFormat.h haiku/trunk/headers/os/locale/Format.h haiku/trunk/headers/os/locale/IntegerFormat.h haiku/trunk/headers/os/locale/NumberFormat.h haiku/trunk/headers/private/locale/ICUWrapper.h haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp haiku/trunk/src/kits/locale/FloatFormat.cpp haiku/trunk/src/kits/locale/Format.cpp haiku/trunk/src/kits/locale/IntegerFormat.cpp haiku/trunk/src/kits/locale/NumberFormat.cpp haiku/trunk/src/kits/locale/TimeFormat.cpp haiku/trunk/src/tests/kits/locale/formatTest.cpp Log: * Improve the test for TimeFormat (style and error checking) * Fix the TimeFormat API, there was a double free. Make it work as expected : you send it a number of seconds and it will format it properly in days, hours, minutes, seconds with proper plural. * Cleanup other parts of the Format API from useless things. They may get reintroduced later if we feel the need to do so. * AboutSystem now use TimeFormat to display the uptime in properly localized way. Modified: haiku/trunk/headers/os/locale/FloatFormat.h =================================================================== --- haiku/trunk/headers/os/locale/FloatFormat.h 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/headers/os/locale/FloatFormat.h 2010-02-17 15:35:28 UTC (rev 35506) @@ -47,9 +47,6 @@ BFloatFormat &operator=(const BFloatFormat &other); BFloatFormat(BFloatFormatImpl *impl); // conceptually private - - private: - inline BFloatFormatImpl *FloatFormatImpl() const; }; Modified: haiku/trunk/headers/os/locale/Format.h =================================================================== --- haiku/trunk/headers/os/locale/Format.h 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/headers/os/locale/Format.h 2010-02-17 15:35:28 UTC (rev 35506) @@ -40,9 +40,6 @@ BFormat &operator=(const BFormat &other); BFormat(); - - protected: - BFormatImpl *fImpl; }; #endif // _B_FORMAT_H_ Modified: haiku/trunk/headers/os/locale/IntegerFormat.h =================================================================== --- haiku/trunk/headers/os/locale/IntegerFormat.h 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/headers/os/locale/IntegerFormat.h 2010-02-17 15:35:28 UTC (rev 35506) @@ -52,9 +52,6 @@ BIntegerFormat &operator=(const BIntegerFormat &other); BIntegerFormat(BIntegerFormatImpl *impl); // conceptually private - - private: - inline BIntegerFormatImpl *IntegerFormatImpl() const; }; Modified: haiku/trunk/headers/os/locale/NumberFormat.h =================================================================== --- haiku/trunk/headers/os/locale/NumberFormat.h 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/headers/os/locale/NumberFormat.h 2010-02-17 15:35:28 UTC (rev 35506) @@ -14,9 +14,6 @@ BNumberFormat &operator=(const BNumberFormat &other); BNumberFormat(); - - private: - inline BNumberFormatImpl *NumberFormatImpl() const; }; Modified: haiku/trunk/headers/private/locale/ICUWrapper.h =================================================================== --- haiku/trunk/headers/private/locale/ICUWrapper.h 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/headers/private/locale/ICUWrapper.h 2010-02-17 15:35:28 UTC (rev 35506) @@ -13,6 +13,7 @@ #include <String.h> #include <unicode/bytestream.h> +#include <String.h> /* Convert UnicodeString to BString needs an ICU ByteSink to do the work */ Modified: haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp =================================================================== --- haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/src/apps/aboutsystem/AboutSystem.cpp 2010-02-17 15:35:28 UTC (rev 35506) @@ -34,6 +34,7 @@ #include <ScrollView.h> #include <String.h> #include <StringView.h> +#include <TimeFormat.h> #include <TranslationUtils.h> #include <TranslatorFormats.h> #include <View.h> @@ -1530,65 +1531,13 @@ static const char* UptimeToString(char string[], size_t size) { - int64 days, hours, minutes, seconds, remainder; - int64 systime = system_time(); + BTimeFormat formatter; + BString str; - days = systime / 86400000000LL; - remainder = systime % 86400000000LL; + formatter.Format(system_time() / 1000000, &str); + str.CopyInto(string,0,size); + string[str.Length()] = '\0'; - hours = remainder / 3600000000LL; - remainder = remainder % 3600000000LL; - - minutes = remainder / 60000000; - remainder = remainder % 60000000; - - seconds = remainder / 1000000; - - char* str = string; - if (days) { - if (days > 1) { - str += snprintf(str, size, TR("%lld days"), days); - } else { - str += snprintf(str, size, TR("%lld day"), days); - } - } - if (hours) { - if (hours > 1) { - str += snprintf(str, size - strlen(string), - TR("%s%lld hours"), - str != string ? ", " : "", hours); - } else { - str += snprintf(str, size - strlen(string), - TR("%s%lld hour"), - str != string ? ", " : "", hours); - } - } - if (minutes) { - if (minutes > 1) { - str += snprintf(str, size - strlen(string), - TR("%s%lld minutes"), - str != string ? ", " : "", minutes); - } else { - str += snprintf(str, size - strlen(string), - TR("%s%lld minute"), - str != string ? ", " : "", minutes); - } - } - - if (seconds || str == string) { - // Haiku would be well-known to boot very fast. - // Let's be ready to handle below minute uptime, zero second included ;-) - if (seconds > 1) { - str += snprintf(str, size - strlen(string), - TR("%s%lld seconds"), - str != string ? ", " : "", seconds); - } else { - str += snprintf(str, size - strlen(string), - TR("%s%lld second"), - str != string ? ", " : "", seconds); - } - } - return string; } Modified: haiku/trunk/src/kits/locale/FloatFormat.cpp =================================================================== --- haiku/trunk/src/kits/locale/FloatFormat.cpp 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/src/kits/locale/FloatFormat.cpp 2010-02-17 15:35:28 UTC (rev 35506) @@ -17,9 +17,7 @@ status_t BFloatFormat::Format(double number, BString *buffer) const { - if (!fImpl) - return B_NO_INIT; - return FloatFormatImpl()->Format(this, number, buffer); + return B_ERROR; } // Format @@ -28,10 +26,7 @@ format_field_position *positions, int32 positionCount, int32 *fieldCount, bool allFieldPositions) const { - if (!fImpl) - return B_NO_INIT; - return FloatFormatImpl()->Format(this,number, buffer, positions, - positionCount, fieldCount, allFieldPositions); + return B_ERROR; } // = @@ -51,11 +46,3 @@ { } -// FloatFormatImpl -inline -BFloatFormatImpl * -BFloatFormat::FloatFormatImpl() const -{ - return static_cast<BFloatFormatImpl*>(fImpl); -} - Modified: haiku/trunk/src/kits/locale/Format.cpp =================================================================== --- haiku/trunk/src/kits/locale/Format.cpp 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/src/kits/locale/Format.cpp 2010-02-17 15:35:28 UTC (rev 35506) @@ -3,7 +3,6 @@ // copy constructor BFormat::BFormat(const BFormat &other) - : fImpl(other.fImpl) { } @@ -16,7 +15,6 @@ BFormat & BFormat::operator=(const BFormat &other) { - fImpl = other.fImpl; return *this; } Modified: haiku/trunk/src/kits/locale/IntegerFormat.cpp =================================================================== --- haiku/trunk/src/kits/locale/IntegerFormat.cpp 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/src/kits/locale/IntegerFormat.cpp 2010-02-17 15:35:28 UTC (rev 35506) @@ -17,9 +17,7 @@ status_t BIntegerFormat::Format(int64 number, BString *buffer) const { - if (!fImpl) - return B_NO_INIT; - return IntegerFormatImpl()->Format(this, number, buffer); + return B_ERROR; } // Format @@ -28,10 +26,7 @@ format_field_position *positions, int32 positionCount, int32 *fieldCount, bool allFieldPositions) const { - if (!fImpl) - return B_NO_INIT; - return IntegerFormatImpl()->Format(this,number, buffer, positions, - positionCount, fieldCount, allFieldPositions); + return B_ERROR; } // = @@ -51,11 +46,3 @@ { } -// IntegerFormatImpl -inline -BIntegerFormatImpl * -BIntegerFormat::IntegerFormatImpl() const -{ - return static_cast<BIntegerFormatImpl*>(fImpl); -} - Modified: haiku/trunk/src/kits/locale/NumberFormat.cpp =================================================================== --- haiku/trunk/src/kits/locale/NumberFormat.cpp 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/src/kits/locale/NumberFormat.cpp 2010-02-17 15:35:28 UTC (rev 35506) @@ -26,11 +26,3 @@ { } -// NumberFormatImpl -inline -BNumberFormatImpl * -BNumberFormat::NumberFormatImpl() const -{ - return static_cast<BNumberFormatImpl*>(fImpl); -} - Modified: haiku/trunk/src/kits/locale/TimeFormat.cpp =================================================================== --- haiku/trunk/src/kits/locale/TimeFormat.cpp 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/src/kits/locale/TimeFormat.cpp 2010-02-17 15:35:28 UTC (rev 35506) @@ -14,24 +14,75 @@ { // create time unit amount instance - a combination of Number and time unit UErrorCode status = U_ZERO_ERROR; - TimeUnitAmount* source = new TimeUnitAmount(number/1000000, TimeUnit::UTIMEUNIT_SECOND, status); - // create time unit format instance + + int64 days, hours, minutes, seconds, remainder; + + days = number / (24 * 3600); + remainder = number % (24 * 3600); + + hours = remainder / 3600; + remainder %= 3600; + + minutes = remainder / 60; + remainder %= 60; + + seconds = remainder; + TimeUnitFormat* format = new TimeUnitFormat(status); - // format a time unit amount UnicodeString formatted; - Formattable formattable(source); + Formattable formattable; + BStringByteSink bbs(buffer); + if (!U_SUCCESS(status)) { - delete source; delete format; return B_ERROR; } - formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, status); + if (days) { + TimeUnitAmount* daysAmount = new TimeUnitAmount(days, + TimeUnit::UTIMEUNIT_DAY, status); - BStringByteSink bbs(buffer); + formattable.adoptObject(daysAmount); + formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, + status); + } + + if (hours) { + TimeUnitAmount* hoursAmount = new TimeUnitAmount(hours, + TimeUnit::UTIMEUNIT_HOUR, status); + + formattable.adoptObject(hoursAmount); + if (days) + formatted.append(", "); + formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, + status); + } + + if (minutes) { + TimeUnitAmount* minutesAmount = new TimeUnitAmount(minutes, + TimeUnit::UTIMEUNIT_MINUTE, status); + + formattable.adoptObject(minutesAmount); + if (days || hours) + formatted.append(", "); + formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, + status); + } + + + if (seconds || (minutes == 0 && hours == 0 && days == 0)) { + TimeUnitAmount* secondsAmount = new TimeUnitAmount(seconds, + TimeUnit::UTIMEUNIT_SECOND, status); + + formattable.adoptObject(secondsAmount); + if (days || hours || minutes) + formatted.append(", "); + formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, + status); + } formatted.toUTF8(bbs); - delete source; + delete format; return B_OK; } Modified: haiku/trunk/src/tests/kits/locale/formatTest.cpp =================================================================== --- haiku/trunk/src/tests/kits/locale/formatTest.cpp 2010-02-17 09:12:43 UTC (rev 35505) +++ haiku/trunk/src/tests/kits/locale/formatTest.cpp 2010-02-17 15:35:28 UTC (rev 35506) @@ -5,11 +5,17 @@ #include <String.h> #include <TimeFormat.h> -int main() { +int +main() +{ BTimeFormat timeFormatter; BString str; - timeFormatter.Format(123456, &str); + if (timeFormatter.Format(123456, &str) != B_OK) { + std::cout << "Conversion error\n"; + return -1; + } - std::cout << str.String(); + std::cout << str.String() << std::endl; + return 0; }