Move time formatting to BTimeformat.

* Harmonize API for all B*Format to take an output BString by reference
as the first parameter,
* Move the FormatTime methods from BLocale to BTimeFormat
* Adjust all callers for BTimeFormat, BTimeUnitFormat and
BDurationFormat.
This commit is contained in:
Adrien Destugues
2014-10-01 18:13:35 +02:00
parent 0a925409bc
commit 03b2550ef1
22 changed files with 364 additions and 317 deletions
+3 -2
View File
@@ -35,8 +35,9 @@ public:
virtual status_t SetLanguage(const BLanguage& language); virtual status_t SetLanguage(const BLanguage& language);
status_t SetTimeZone(const BTimeZone* timeZone); status_t SetTimeZone(const BTimeZone* timeZone);
status_t Format(bigtime_t startValue, status_t Format(BString& buffer,
bigtime_t stopValue, BString* buffer, const bigtime_t startValue,
const bigtime_t stopValue,
time_unit_style style = B_TIME_UNIT_FULL time_unit_style style = B_TIME_UNIT_FULL
) const; ) const;
-21
View File
@@ -69,27 +69,6 @@ public:
BTimeFormatStyle timeStyle, BTimeFormatStyle timeStyle,
const BTimeZone* timeZone = NULL) const; const BTimeZone* timeZone = NULL) const;
// Time
// TODO: drop some of these once BTimeFormat
// has been implemented!
ssize_t FormatTime(char* string, size_t maxSize,
time_t time, BTimeFormatStyle style) const;
ssize_t FormatTime(char* string, size_t maxSize,
time_t time, BString format) const;
status_t FormatTime(BString* string, time_t time,
BTimeFormatStyle style,
const BTimeZone* timeZone = NULL) const;
status_t FormatTime(BString* string, time_t time,
BString format,
const BTimeZone* timeZone) const;
status_t FormatTime(BString* string,
int*& fieldPositions, int& fieldCount,
time_t time, BTimeFormatStyle style) const;
status_t GetTimeFields(BDateElement*& fields,
int& fieldCount, BTimeFormatStyle style
) const;
// numbers // numbers
ssize_t FormatNumber(char* string, size_t maxSize, ssize_t FormatNumber(char* string, size_t maxSize,
+28 -7
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010, Haiku, Inc. * Copyright 2010-2014, Haiku, Inc.
* Distributed under the terms of the MIT Licence. * Distributed under the terms of the MIT Licence.
*/ */
#ifndef _B_TIME_FORMAT_H_ #ifndef _B_TIME_FORMAT_H_
@@ -9,20 +9,41 @@
class BString; class BString;
class BTimeZone;
class BTimeFormat : public BDateTimeFormat { class BTimeFormat : public BFormat {
public: public:
BTimeFormat(
const BLanguage* const language = NULL,
const BFormattingConventions* const
format = NULL);
BTimeFormat(const BTimeFormat &other); BTimeFormat(const BTimeFormat &other);
virtual ~BTimeFormat(); virtual ~BTimeFormat();
// formatting // formatting
// no-frills version: Simply appends the ssize_t Format(char* string, size_t maxSize,
// formatted date to the string buffer. time_t time, BTimeFormatStyle style) const;
// Can fail only with B_NO_MEMORY or B_BAD_VALUE. ssize_t Format(char* string, size_t maxSize,
virtual status_t Format(bigtime_t value, BString* buffer) const; time_t time, BString format) const;
status_t Format(BString& string, const time_t time,
const BTimeFormatStyle style,
const BTimeZone* timeZone = NULL) const;
status_t Format(BString& string, const time_t time,
const BString format,
const BTimeZone* timeZone) const;
status_t Format(BString& string,
int*& fieldPositions, int& fieldCount,
time_t time, BTimeFormatStyle style) const;
// TODO: ... basically, all of it! status_t GetTimeFields(BDateElement*& fields,
int& fieldCount, BTimeFormatStyle style
) const;
// TODO parsing
private:
icu::DateFormat* _CreateTimeFormatter(
const BString& format) const;
}; };
+3 -2
View File
@@ -47,8 +47,9 @@ public:
BTimeUnitFormat& operator=(const BTimeUnitFormat& other); BTimeUnitFormat& operator=(const BTimeUnitFormat& other);
virtual status_t SetLanguage(const BLanguage& locale); virtual status_t SetLanguage(const BLanguage& locale);
status_t Format(int32 value, time_unit_element unit, status_t Format(BString& buffer,
BString* buffer, const int32 value,
const time_unit_element unit,
time_unit_style style = B_TIME_UNIT_FULL time_unit_style style = B_TIME_UNIT_FULL
) const; ) const;
+1 -1
View File
@@ -1612,7 +1612,7 @@ UptimeToString(char string[], size_t size)
bigtime_t uptime = system_time(); bigtime_t uptime = system_time();
bigtime_t now = (bigtime_t)time(NULL) * 1000000; bigtime_t now = (bigtime_t)time(NULL) * 1000000;
formatter.Format(now - uptime, now, &str); formatter.Format(str, now - uptime, now);
str.CopyInto(string, 0, size); str.CopyInto(string, 0, size);
string[std::min((size_t)str.Length(), size)] = '\0'; string[std::min((size_t)str.Length(), size)] = '\0';
+5 -5
View File
@@ -40,7 +40,6 @@ All rights reserved.
#include <Application.h> #include <Application.h>
#include <Catalog.h> #include <Catalog.h>
#include <DateFormat.h>
#include <Debug.h> #include <Debug.h>
#include <Locale.h> #include <Locale.h>
#include <MenuItem.h> #include <MenuItem.h>
@@ -391,27 +390,27 @@ TTimeView::GetCurrentTime()
if (fShowDayOfWeek) { if (fShowDayOfWeek) {
BString timeFormat("eee "); BString timeFormat("eee ");
offset_dow = fLocale.FormatTime(fCurrentTimeStr, offset_dow = fTimeFormat.Format(fCurrentTimeStr,
sizeof(fCurrentTimeStr), fCurrentTime, timeFormat); sizeof(fCurrentTimeStr), fCurrentTime, timeFormat);
if (offset_dow < 0) { if (offset_dow < 0) {
// error occured, attempt to overwrite with current time // error occured, attempt to overwrite with current time
// (this should not ever happen) // (this should not ever happen)
fLocale.FormatTime(fCurrentTimeStr, sizeof(fCurrentTimeStr), fTimeFormat.Format(fCurrentTimeStr, sizeof(fCurrentTimeStr),
fCurrentTime, fCurrentTime,
fShowSeconds ? B_MEDIUM_TIME_FORMAT : B_SHORT_TIME_FORMAT); fShowSeconds ? B_MEDIUM_TIME_FORMAT : B_SHORT_TIME_FORMAT);
return; return;
} }
} }
offset_time = fLocale.FormatTime(fCurrentTimeStr + offset_dow, offset_time = fTimeFormat.Format(fCurrentTimeStr + offset_dow,
sizeof(fCurrentTimeStr) - offset_dow, fCurrentTime, sizeof(fCurrentTimeStr) - offset_dow, fCurrentTime,
fShowSeconds ? B_MEDIUM_TIME_FORMAT : B_SHORT_TIME_FORMAT); fShowSeconds ? B_MEDIUM_TIME_FORMAT : B_SHORT_TIME_FORMAT);
if (fShowTimeZone) { if (fShowTimeZone) {
BString timeFormat(" V"); BString timeFormat(" V");
ssize_t offset = offset_dow + offset_time; ssize_t offset = offset_dow + offset_time;
fLocale.FormatTime(fCurrentTimeStr + offset, fTimeFormat.Format(fCurrentTimeStr + offset,
sizeof(fCurrentTimeStr) - offset, fCurrentTime, timeFormat); sizeof(fCurrentTimeStr) - offset, fCurrentTime, timeFormat);
} }
} }
@@ -490,6 +489,7 @@ TTimeView::Update()
{ {
fLocale = *BLocale::Default(); fLocale = *BLocale::Default();
fDateFormat.SetLocale(fLocale); fDateFormat.SetLocale(fLocale);
fTimeFormat.SetLocale(fLocale);
GetCurrentTime(); GetCurrentTime();
GetCurrentDate(); GetCurrentDate();
+2
View File
@@ -40,6 +40,7 @@ All rights reserved.
#include <Locale.h> #include <Locale.h>
#include <Messenger.h> #include <Messenger.h>
#include <OS.h> #include <OS.h>
#include <TimeFormat.h>
#include <View.h> #include <View.h>
@@ -150,6 +151,7 @@ private:
// For date and time localization purposes // For date and time localization purposes
BLocale fLocale; BLocale fLocale;
BDateFormat fDateFormat; BDateFormat fDateFormat;
BTimeFormat fTimeFormat;
}; };
+5 -6
View File
@@ -13,6 +13,7 @@
#include <Roster.h> #include <Roster.h>
#include <String.h> #include <String.h>
#include <TextView.h> #include <TextView.h>
#include <TimeFormat.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -79,7 +80,8 @@ TimedAlert::GetLabel(BString &string)
{ {
string = B_TRANSLATE("Attention!\n\nBecause of the switch from daylight " string = B_TRANSLATE("Attention!\n\nBecause of the switch from daylight "
"saving time, your computer's clock may be an hour off.\n" "saving time, your computer's clock may be an hour off.\n"
"Your computer thinks it is"); "Your computer thinks it is %current time%.\n\nIs this the correct "
"time?");
time_t t; time_t t;
struct tm tm; struct tm tm;
@@ -87,12 +89,9 @@ TimedAlert::GetLabel(BString &string)
time(&t); time(&t);
localtime_r(&t, &tm); localtime_r(&t, &tm);
BLocale::Default()->FormatTime(timestring, 15, t, B_SHORT_TIME_FORMAT); BTimeFormat().Format(timestring, 15, t, B_SHORT_TIME_FORMAT);
string += " "; string.ReplaceFirst("%current time%", timestring);
string += timestring;
string += B_TRANSLATE(".\n\nIs this the correct time?");
} }
+5 -8
View File
@@ -130,12 +130,9 @@ BDurationFormat::SetTimeZone(const BTimeZone* timeZone)
status_t status_t
BDurationFormat::Format(bigtime_t startValue, bigtime_t stopValue, BDurationFormat::Format(BString& buffer, const bigtime_t startValue,
BString* buffer, time_unit_style style) const const bigtime_t stopValue, time_unit_style style) const
{ {
if (buffer == NULL)
return B_BAD_VALUE;
UErrorCode icuStatus = U_ZERO_ERROR; UErrorCode icuStatus = U_ZERO_ERROR;
fCalendar->setTime((UDate)startValue / 1000, icuStatus); fCalendar->setTime((UDate)startValue / 1000, icuStatus);
if (!U_SUCCESS(icuStatus)) if (!U_SUCCESS(icuStatus))
@@ -151,11 +148,11 @@ BDurationFormat::Format(bigtime_t startValue, bigtime_t stopValue,
if (delta != 0) { if (delta != 0) {
if (needSeparator) if (needSeparator)
buffer->Append(fSeparator); buffer.Append(fSeparator);
else else
needSeparator = true; needSeparator = true;
status_t status = fTimeUnitFormat.Format(delta, status_t status = fTimeUnitFormat.Format(buffer, delta,
(time_unit_element)unit, buffer, style); (time_unit_element)unit, style);
if (status != B_OK) if (status != B_OK)
return status; return status;
} }
-233
View File
@@ -265,239 +265,6 @@ BLocale::FormatDateTime(BString* target, time_t time,
} }
// #pragma mark - Time
ssize_t
BLocale::FormatTime(char* string, size_t maxSize, time_t time,
BTimeFormatStyle style) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
CheckedArrayByteSink stringConverter(string, maxSize);
icuString.toUTF8(stringConverter);
if (stringConverter.Overflowed())
return B_BAD_VALUE;
return stringConverter.NumberOfBytesWritten();
}
ssize_t
BLocale::FormatTime(char* string, size_t maxSize, time_t time,
BString format) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
if (format == NULL || format.CountChars() <= 0)
return B_BAD_VALUE;
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
CheckedArrayByteSink stringConverter(string, maxSize);
icuString.toUTF8(stringConverter);
if (stringConverter.Overflowed())
return B_BAD_VALUE;
return stringConverter.NumberOfBytesWritten();
}
status_t
BLocale::FormatTime(BString* string, time_t time, BTimeFormatStyle style,
const BTimeZone* timeZone) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
if (timeZone != NULL) {
ObjectDeleter<TimeZone> icuTimeZone(
TimeZone::createTimeZone(timeZone->ID().String()));
if (icuTimeZone.Get() == NULL)
return B_NO_MEMORY;
timeFormatter->setTimeZone(*icuTimeZone.Get());
}
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
string->Truncate(0);
BStringByteSink stringConverter(string);
icuString.toUTF8(stringConverter);
return B_OK;
}
status_t
BLocale::FormatTime(BString* string, time_t time, BString format,
const BTimeZone* timeZone) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
if (format == NULL || format.CountChars() <= 0)
return B_BAD_VALUE;
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
if (timeZone != NULL) {
ObjectDeleter<TimeZone> icuTimeZone(
TimeZone::createTimeZone(timeZone->ID().String()));
if (icuTimeZone.Get() == NULL)
return B_NO_MEMORY;
timeFormatter->setTimeZone(*icuTimeZone.Get());
}
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
string->Truncate(0);
BStringByteSink stringConverter(string);
icuString.toUTF8(stringConverter);
return B_OK;
}
status_t
BLocale::FormatTime(BString* string, int*& fieldPositions, int& fieldCount,
time_t time, BTimeFormatStyle style) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
fieldPositions = NULL;
UErrorCode error = U_ZERO_ERROR;
icu::FieldPositionIterator positionIterator;
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString, &positionIterator,
error);
if (error != U_ZERO_ERROR)
return B_BAD_VALUE;
icu::FieldPosition field;
std::vector<int> fieldPosStorage;
fieldCount = 0;
while (positionIterator.next(field)) {
fieldPosStorage.push_back(field.getBeginIndex());
fieldPosStorage.push_back(field.getEndIndex());
fieldCount += 2;
}
fieldPositions = (int*) malloc(fieldCount * sizeof(int));
for (int i = 0 ; i < fieldCount ; i++ )
fieldPositions[i] = fieldPosStorage[i];
string->Truncate(0);
BStringByteSink stringConverter(string);
icuString.toUTF8(stringConverter);
return B_OK;
}
status_t
BLocale::GetTimeFields(BDateElement*& fields, int& fieldCount,
BTimeFormatStyle style) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
fields = NULL;
UErrorCode error = U_ZERO_ERROR;
icu::FieldPositionIterator positionIterator;
UnicodeString icuString;
time_t now;
timeFormatter->format((UDate)time(&now) * 1000, icuString,
&positionIterator, error);
if (error != U_ZERO_ERROR)
return B_BAD_VALUE;
icu::FieldPosition field;
std::vector<int> fieldPosStorage;
fieldCount = 0;
while (positionIterator.next(field)) {
fieldPosStorage.push_back(field.getField());
fieldCount ++;
}
fields = (BDateElement*) malloc(fieldCount * sizeof(BDateElement));
for (int i = 0 ; i < fieldCount ; i++ ) {
switch (fieldPosStorage[i]) {
case UDAT_HOUR_OF_DAY1_FIELD:
case UDAT_HOUR_OF_DAY0_FIELD:
case UDAT_HOUR1_FIELD:
case UDAT_HOUR0_FIELD:
fields[i] = B_DATE_ELEMENT_HOUR;
break;
case UDAT_MINUTE_FIELD:
fields[i] = B_DATE_ELEMENT_MINUTE;
break;
case UDAT_SECOND_FIELD:
fields[i] = B_DATE_ELEMENT_SECOND;
break;
case UDAT_AM_PM_FIELD:
fields[i] = B_DATE_ELEMENT_AM_PM;
break;
default:
fields[i] = B_DATE_ELEMENT_INVALID;
break;
}
}
return B_OK;
}
// #pragma mark - Numbers // #pragma mark - Numbers
+280 -8
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010, Haiku, Inc. All Rights Reserved. * Copyright 2010-2014, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -8,21 +8,293 @@
#include <TimeFormat.h> #include <TimeFormat.h>
#include <AutoDeleter.h>
#include <Autolock.h>
#include <FormattingConventionsPrivate.h>
#include <LanguagePrivate.h>
#include <TimeZone.h>
#include <ICUWrapper.h>
#include <unicode/datefmt.h>
#include <unicode/smpdtfmt.h>
#include <vector>
BTimeFormat::BTimeFormat(const BLanguage* const language,
const BFormattingConventions* const conventions)
{
if (conventions != NULL)
fConventions = *conventions;
if (language != NULL)
fLanguage = *language;
}
// copy constructor
BTimeFormat::BTimeFormat(const BTimeFormat &other) BTimeFormat::BTimeFormat(const BTimeFormat &other)
: BDateTimeFormat(other) : BFormat(other)
{ {
} }
// destructor
BTimeFormat::~BTimeFormat() BTimeFormat::~BTimeFormat()
{ {
} }
// Format
status_t // #pragma mark - Formatting
BTimeFormat::Format(bigtime_t value, BString* buffer) const
ssize_t
BTimeFormat::Format(char* string, size_t maxSize, time_t time,
BTimeFormatStyle style) const
{ {
return B_ERROR; BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
CheckedArrayByteSink stringConverter(string, maxSize);
icuString.toUTF8(stringConverter);
if (stringConverter.Overflowed())
return B_BAD_VALUE;
return stringConverter.NumberOfBytesWritten();
}
ssize_t
BTimeFormat::Format(char* string, size_t maxSize, time_t time,
BString format) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
if (format == NULL || format.CountChars() <= 0)
return B_BAD_VALUE;
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
CheckedArrayByteSink stringConverter(string, maxSize);
icuString.toUTF8(stringConverter);
if (stringConverter.Overflowed())
return B_BAD_VALUE;
return stringConverter.NumberOfBytesWritten();
}
status_t
BTimeFormat::Format(BString& string, const time_t time,
const BTimeFormatStyle style, const BTimeZone* timeZone) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
if (timeZone != NULL) {
ObjectDeleter<TimeZone> icuTimeZone(
TimeZone::createTimeZone(timeZone->ID().String()));
if (icuTimeZone.Get() == NULL)
return B_NO_MEMORY;
timeFormatter->setTimeZone(*icuTimeZone.Get());
}
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
string.Truncate(0);
BStringByteSink stringConverter(&string);
icuString.toUTF8(stringConverter);
return B_OK;
}
status_t
BTimeFormat::Format(BString& string, const time_t time,
const BString format, const BTimeZone* timeZone) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
if (format == NULL || format.CountChars() <= 0)
return B_BAD_VALUE;
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
if (timeZone != NULL) {
ObjectDeleter<TimeZone> icuTimeZone(
TimeZone::createTimeZone(timeZone->ID().String()));
if (icuTimeZone.Get() == NULL)
return B_NO_MEMORY;
timeFormatter->setTimeZone(*icuTimeZone.Get());
}
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString);
string.Truncate(0);
BStringByteSink stringConverter(&string);
icuString.toUTF8(stringConverter);
return B_OK;
}
status_t
BTimeFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
time_t time, BTimeFormatStyle style) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
fieldPositions = NULL;
UErrorCode error = U_ZERO_ERROR;
icu::FieldPositionIterator positionIterator;
UnicodeString icuString;
timeFormatter->format((UDate)time * 1000, icuString, &positionIterator,
error);
if (error != U_ZERO_ERROR)
return B_BAD_VALUE;
icu::FieldPosition field;
std::vector<int> fieldPosStorage;
fieldCount = 0;
while (positionIterator.next(field)) {
fieldPosStorage.push_back(field.getBeginIndex());
fieldPosStorage.push_back(field.getEndIndex());
fieldCount += 2;
}
fieldPositions = (int*) malloc(fieldCount * sizeof(int));
for (int i = 0 ; i < fieldCount ; i++ )
fieldPositions[i] = fieldPosStorage[i];
string.Truncate(0);
BStringByteSink stringConverter(&string);
icuString.toUTF8(stringConverter);
return B_OK;
}
status_t
BTimeFormat::GetTimeFields(BDateElement*& fields, int& fieldCount,
BTimeFormatStyle style) const
{
BAutolock lock(fLock);
if (!lock.IsLocked())
return B_ERROR;
BString format;
fConventions.GetTimeFormat(style, format);
ObjectDeleter<DateFormat> timeFormatter(_CreateTimeFormatter(format));
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
fields = NULL;
UErrorCode error = U_ZERO_ERROR;
icu::FieldPositionIterator positionIterator;
UnicodeString icuString;
time_t now;
timeFormatter->format((UDate)time(&now) * 1000, icuString,
&positionIterator, error);
if (error != U_ZERO_ERROR)
return B_BAD_VALUE;
icu::FieldPosition field;
std::vector<int> fieldPosStorage;
fieldCount = 0;
while (positionIterator.next(field)) {
fieldPosStorage.push_back(field.getField());
fieldCount ++;
}
fields = (BDateElement*) malloc(fieldCount * sizeof(BDateElement));
for (int i = 0 ; i < fieldCount ; i++ ) {
switch (fieldPosStorage[i]) {
case UDAT_HOUR_OF_DAY1_FIELD:
case UDAT_HOUR_OF_DAY0_FIELD:
case UDAT_HOUR1_FIELD:
case UDAT_HOUR0_FIELD:
fields[i] = B_DATE_ELEMENT_HOUR;
break;
case UDAT_MINUTE_FIELD:
fields[i] = B_DATE_ELEMENT_MINUTE;
break;
case UDAT_SECOND_FIELD:
fields[i] = B_DATE_ELEMENT_SECOND;
break;
case UDAT_AM_PM_FIELD:
fields[i] = B_DATE_ELEMENT_AM_PM;
break;
default:
fields[i] = B_DATE_ELEMENT_INVALID;
break;
}
}
return B_OK;
}
DateFormat*
BTimeFormat::_CreateTimeFormatter(const BString& format) const
{
Locale* icuLocale
= fConventions.UseStringsFromPreferredLanguage()
? BLanguage::Private(&fLanguage).ICULocale()
: BFormattingConventions::Private(&fConventions).ICULocale();
icu::DateFormat* timeFormatter
= icu::DateFormat::createTimeInstance(DateFormat::kShort, *icuLocale);
if (timeFormatter == NULL)
return NULL;
SimpleDateFormat* timeFormatterImpl
= static_cast<SimpleDateFormat*>(timeFormatter);
UnicodeString pattern(format.String());
timeFormatterImpl->applyPattern(pattern);
return timeFormatter;
} }
+4 -4
View File
@@ -101,10 +101,10 @@ BTimeUnitFormat::SetLanguage(const BLanguage& language)
status_t status_t
BTimeUnitFormat::Format(int32 value, time_unit_element unit, BTimeUnitFormat::Format(BString& buffer, const int32 value,
BString* buffer, time_unit_style style) const const time_unit_element unit, time_unit_style style) const
{ {
if (buffer == NULL || unit < 0 || unit > B_TIME_UNIT_LAST if (unit < 0 || unit > B_TIME_UNIT_LAST
|| (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL)) || (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL))
return B_BAD_VALUE; return B_BAD_VALUE;
@@ -127,7 +127,7 @@ BTimeUnitFormat::Format(int32 value, time_unit_element unit,
if (!U_SUCCESS(icuStatus)) if (!U_SUCCESS(icuStatus))
return B_ERROR; return B_ERROR;
BStringByteSink byteSink(buffer); BStringByteSink byteSink(&buffer);
unicodeResult.toUTF8(byteSink); unicodeResult.toUTF8(byteSink);
return B_OK; return B_OK;
+4 -3
View File
@@ -47,6 +47,7 @@ All rights reserved.
#include <MessageFilter.h> #include <MessageFilter.h>
#include <StringView.h> #include <StringView.h>
#include <String.h> #include <String.h>
#include <TimeFormat.h>
#include <string.h> #include <string.h>
@@ -800,7 +801,7 @@ BStatusView::_TimeStatusString(float availableSpace, float* _width)
locale->FormatDateTime(timeText, sizeof(timeText), finishTime, locale->FormatDateTime(timeText, sizeof(timeText), finishTime,
B_MEDIUM_DATE_FORMAT, B_MEDIUM_TIME_FORMAT); B_MEDIUM_DATE_FORMAT, B_MEDIUM_TIME_FORMAT);
} else { } else {
locale->FormatTime(timeText, sizeof(timeText), finishTime, BTimeFormat().Format(timeText, sizeof(timeText), finishTime,
B_MEDIUM_TIME_FORMAT); B_MEDIUM_TIME_FORMAT);
} }
@@ -840,10 +841,10 @@ BStatusView::_FullTimeRemainingString(time_t now, time_t finishTime,
BString finishStr; BString finishStr;
if (finishTime - now > 60 * 60) { if (finishTime - now > 60 * 60) {
buffer.SetTo(B_TRANSLATE("Finish: %time - Over %finishtime left")); buffer.SetTo(B_TRANSLATE("Finish: %time - Over %finishtime left"));
formatter.Format(now * 1000000LL, finishTime * 1000000LL, &finishStr); formatter.Format(finishStr, now * 1000000LL, finishTime * 1000000LL);
} else { } else {
buffer.SetTo(B_TRANSLATE("Finish: %time - %finishtime left")); buffer.SetTo(B_TRANSLATE("Finish: %time - %finishtime left"));
formatter.Format(now * 1000000LL, finishTime * 1000000LL, &finishStr); formatter.Format(finishStr, now * 1000000LL, finishTime * 1000000LL);
} }
buffer.ReplaceFirst("%time", timeText); buffer.ReplaceFirst("%time", timeText);
@@ -34,6 +34,7 @@
#include <String.h> #include <String.h>
#include <StringView.h> #include <StringView.h>
#include <TextControl.h> #include <TextControl.h>
#include <TimeFormat.h>
#include <Window.h> #include <Window.h>
#include "LocalePreflet.h" #include "LocalePreflet.h"
@@ -361,9 +362,10 @@ FormatSettingsView::_UpdateExamples()
time_t timeValue = (time_t)time(NULL); time_t timeValue = (time_t)time(NULL);
BString result; BString result;
// Do NOT make this a class member. We do want to recreate it everytime, as // Do NOT make these class members. We do want to recreate it everytime, as
// to get the updated settings from the locale roster. // to get the updated settings from the locale roster.
BDateFormat dateFormat; BDateFormat dateFormat;
BTimeFormat timeFormat;
dateFormat.Format(result, timeValue, B_FULL_DATE_FORMAT); dateFormat.Format(result, timeValue, B_FULL_DATE_FORMAT);
fFullDateExampleView->SetText(result); fFullDateExampleView->SetText(result);
@@ -377,16 +379,16 @@ FormatSettingsView::_UpdateExamples()
dateFormat.Format(result, timeValue, B_SHORT_DATE_FORMAT); dateFormat.Format(result, timeValue, B_SHORT_DATE_FORMAT);
fShortDateExampleView->SetText(result); fShortDateExampleView->SetText(result);
BLocale::Default()->FormatTime(&result, timeValue, B_FULL_TIME_FORMAT); timeFormat.Format(result, timeValue, B_FULL_TIME_FORMAT);
fFullTimeExampleView->SetText(result); fFullTimeExampleView->SetText(result);
BLocale::Default()->FormatTime(&result, timeValue, B_LONG_TIME_FORMAT); timeFormat.Format(result, timeValue, B_LONG_TIME_FORMAT);
fLongTimeExampleView->SetText(result); fLongTimeExampleView->SetText(result);
BLocale::Default()->FormatTime(&result, timeValue, B_MEDIUM_TIME_FORMAT); timeFormat.Format(result, timeValue, B_MEDIUM_TIME_FORMAT);
fMediumTimeExampleView->SetText(result); fMediumTimeExampleView->SetText(result);
BLocale::Default()->FormatTime(&result, timeValue, B_SHORT_TIME_FORMAT); timeFormat.Format(result, timeValue, B_SHORT_TIME_FORMAT);
fShortTimeExampleView->SetText(result); fShortTimeExampleView->SetText(result);
status_t status = BLocale::Default()->FormatNumber(&result, 1234.5678); status_t status = BLocale::Default()->FormatNumber(&result, 1234.5678);
@@ -256,7 +256,7 @@ void
TimeSlider::_TimeToString(bigtime_t useconds, BString& string) TimeSlider::_TimeToString(bigtime_t useconds, BString& string)
{ {
BDurationFormat formatter; BDurationFormat formatter;
formatter.Format(0, useconds, &string); formatter.Format(string, 0, useconds);
} }
+3 -4
View File
@@ -248,15 +248,14 @@ TTimeEdit::_UpdateFields()
free(fFieldPositions); free(fFieldPositions);
fFieldPositions = NULL; fFieldPositions = NULL;
} }
BLocale::Default()->FormatTime(&fText, fFieldPositions, fTimeFormat.Format(fText, fFieldPositions, fFieldPosCount, time,
fFieldPosCount, time, B_MEDIUM_TIME_FORMAT); B_MEDIUM_TIME_FORMAT);
if (fFields != NULL) { if (fFields != NULL) {
free(fFields); free(fFields);
fFields = NULL; fFields = NULL;
} }
BLocale::Default()->GetTimeFields(fFields, fFieldCount, fTimeFormat.GetTimeFields(fFields, fFieldCount, B_MEDIUM_TIME_FORMAT);
B_MEDIUM_TIME_FORMAT);
} }
+2
View File
@@ -16,6 +16,7 @@
#include <DateTime.h> #include <DateTime.h>
#include <Locale.h> #include <Locale.h>
#include <String.h> #include <String.h>
#include <TimeFormat.h>
#include "SectionEdit.h" #include "SectionEdit.h"
@@ -51,6 +52,7 @@ private:
int32 _SectionValue(int32 index) const; int32 _SectionValue(int32 index) const;
BDateTime fTime; BDateTime fTime;
BTimeFormat fTimeFormat;
bigtime_t fLastKeyDownTime; bigtime_t fLastKeyDownTime;
int32 fLastKeyDownInt; int32 fLastKeyDownInt;
+1 -1
View File
@@ -47,7 +47,7 @@ TimeZoneListView::GetToolTipAt(BPoint point, BToolTip** _tip)
BString nowInTimeZone; BString nowInTimeZone;
time_t now = time(NULL); time_t now = time(NULL);
BLocale::Default()->FormatTime(&nowInTimeZone, now, B_SHORT_TIME_FORMAT, fTimeFormat.Format(nowInTimeZone, now, B_SHORT_TIME_FORMAT,
&item->TimeZone()); &item->TimeZone());
BString dateInTimeZone; BString dateInTimeZone;
+2
View File
@@ -11,6 +11,7 @@
#include <DateFormat.h> #include <DateFormat.h>
#include <OutlineListView.h> #include <OutlineListView.h>
#include <TimeFormat.h>
class TimeZoneListView : public BOutlineListView { class TimeZoneListView : public BOutlineListView {
@@ -23,6 +24,7 @@ protected:
private: private:
BDateFormat fDateFormat; BDateFormat fDateFormat;
BTimeFormat fTimeFormat;
}; };
+1 -2
View File
@@ -567,8 +567,7 @@ TimeZoneView::_FormatTime(const BTimeZone& timeZone)
: 0; : 0;
now -= timeZone.OffsetFromGMT() - currentOffset; now -= timeZone.OffsetFromGMT() - currentOffset;
} }
BLocale::Default()->FormatTime(&result, now, B_SHORT_TIME_FORMAT, fTimeFormat.Format(result, now, B_SHORT_TIME_FORMAT, &timeZone);
&timeZone);
return result; return result;
} }
+3
View File
@@ -12,6 +12,7 @@
#include <LayoutBuilder.h> #include <LayoutBuilder.h>
#include <TimeFormat.h>
#include <TimeZone.h> #include <TimeZone.h>
@@ -71,6 +72,8 @@ private:
TimeZoneListItem* fCurrentZoneItem; TimeZoneListItem* fCurrentZoneItem;
TimeZoneListItem* fOldZoneItem; TimeZoneListItem* fOldZoneItem;
bool fInitialized; bool fInitialized;
BTimeFormat fTimeFormat;
}; };
+4 -4
View File
@@ -38,7 +38,7 @@ DurationFormatTest::TestDuration()
format.SetFormattingConventions(englishFormat); format.SetFormattingConventions(englishFormat);
format.SetLanguage(englishLanguage); format.SetLanguage(englishLanguage);
status_t result = format.Format(0, 800000000000ll, &buffer); status_t result = format.Format(buffer, 0, 800000000000ll);
expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds"; expected << "1 week, 2 days, 6 hours, 13 minutes, 20 seconds";
CPPUNIT_ASSERT_EQUAL(B_OK, result); CPPUNIT_ASSERT_EQUAL(B_OK, result);
@@ -46,7 +46,7 @@ DurationFormatTest::TestDuration()
format.SetFormattingConventions(frenchFormat); format.SetFormattingConventions(frenchFormat);
format.SetLanguage(frenchLanguage); format.SetLanguage(frenchLanguage);
result = format.Format(0, 800000000000ll, &buffer); result = format.Format(buffer, 0, 800000000000ll);
// We check that the passed BString is not truncated. // We check that the passed BString is not truncated.
expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes"; expected << "1 semaine, 2 jours, 6 heures, 13 minutes, 20 secondes";
@@ -69,14 +69,14 @@ DurationFormatTest::TestTimeUnit()
format.SetFormattingConventions(englishFormat); format.SetFormattingConventions(englishFormat);
format.SetLanguage(englishLanguage); format.SetLanguage(englishLanguage);
status_t result = format.Format(5, B_TIME_UNIT_HOUR, &buffer); status_t result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
CPPUNIT_ASSERT_EQUAL(B_OK, result); CPPUNIT_ASSERT_EQUAL(B_OK, result);
CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer); CPPUNIT_ASSERT_EQUAL(BString("5 hours"), buffer);
format.SetFormattingConventions(frenchFormat); format.SetFormattingConventions(frenchFormat);
format.SetLanguage(frenchLanguage); format.SetLanguage(frenchLanguage);
result = format.Format(5, B_TIME_UNIT_HOUR, &buffer); result = format.Format(buffer, 5, B_TIME_UNIT_HOUR);
CPPUNIT_ASSERT_EQUAL(B_OK, result); CPPUNIT_ASSERT_EQUAL(B_OK, result);
// We check that the passed BString is not truncated. This makes it easy // We check that the passed BString is not truncated. This makes it easy