* 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. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35506 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -47,9 +47,6 @@ class BFloatFormat : public BNumberFormat, public BFloatFormatParameters {
|
||||
BFloatFormat &operator=(const BFloatFormat &other);
|
||||
|
||||
BFloatFormat(BFloatFormatImpl *impl); // conceptually private
|
||||
|
||||
private:
|
||||
inline BFloatFormatImpl *FloatFormatImpl() const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -40,9 +40,6 @@ class BFormat {
|
||||
BFormat &operator=(const BFormat &other);
|
||||
|
||||
BFormat();
|
||||
|
||||
protected:
|
||||
BFormatImpl *fImpl;
|
||||
};
|
||||
|
||||
#endif // _B_FORMAT_H_
|
||||
|
||||
@@ -52,9 +52,6 @@ class BIntegerFormat : public BNumberFormat, public BIntegerFormatParameters {
|
||||
BIntegerFormat &operator=(const BIntegerFormat &other);
|
||||
|
||||
BIntegerFormat(BIntegerFormatImpl *impl); // conceptually private
|
||||
|
||||
private:
|
||||
inline BIntegerFormatImpl *IntegerFormatImpl() const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -14,9 +14,6 @@ class BNumberFormat : public BFormat {
|
||||
BNumberFormat &operator=(const BNumberFormat &other);
|
||||
|
||||
BNumberFormat();
|
||||
|
||||
private:
|
||||
inline BNumberFormatImpl *NumberFormatImpl() const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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,64 +1531,12 @@ MemUsageToString(char string[], size_t size, system_info* info)
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
formatter.Format(system_time() / 1000000, &str);
|
||||
str.CopyInto(string,0,size);
|
||||
string[str.Length()] = '\0';
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
@@ -17,9 +17,7 @@ BFloatFormat::~BFloatFormat()
|
||||
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 @@ BFloatFormat::Format(double number, BString *buffer,
|
||||
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 @@ BFloatFormat::BFloatFormat(BFloatFormatImpl *impl)
|
||||
{
|
||||
}
|
||||
|
||||
// FloatFormatImpl
|
||||
inline
|
||||
BFloatFormatImpl *
|
||||
BFloatFormat::FloatFormatImpl() const
|
||||
{
|
||||
return static_cast<BFloatFormatImpl*>(fImpl);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
// copy constructor
|
||||
BFormat::BFormat(const BFormat &other)
|
||||
: fImpl(other.fImpl)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -16,7 +15,6 @@ BFormat::~BFormat()
|
||||
BFormat &
|
||||
BFormat::operator=(const BFormat &other)
|
||||
{
|
||||
fImpl = other.fImpl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,9 +17,7 @@ BIntegerFormat::~BIntegerFormat()
|
||||
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 @@ BIntegerFormat::Format(int64 number, BString *buffer,
|
||||
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 @@ BIntegerFormat::BIntegerFormat(BIntegerFormatImpl *impl)
|
||||
{
|
||||
}
|
||||
|
||||
// IntegerFormatImpl
|
||||
inline
|
||||
BIntegerFormatImpl *
|
||||
BIntegerFormat::IntegerFormatImpl() const
|
||||
{
|
||||
return static_cast<BIntegerFormatImpl*>(fImpl);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,11 +26,3 @@ BNumberFormat::BNumberFormat()
|
||||
{
|
||||
}
|
||||
|
||||
// NumberFormatImpl
|
||||
inline
|
||||
BNumberFormatImpl *
|
||||
BNumberFormat::NumberFormatImpl() const
|
||||
{
|
||||
return static_cast<BNumberFormatImpl*>(fImpl);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,24 +14,75 @@ status_t BTimeFormat::Format(int64 number, BString* buffer) const
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user