BDateFormat: improve API
* Use a reference rather than a pointer for the output string, removing the need for NULL checks (which were missing, anyway) * Adjust callers to that change * Add new Format variant taking a BDate argument
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#define _B_DATE_FORMAT_H_
|
||||
|
||||
|
||||
#include <DateTime.h>
|
||||
#include <DateTimeFormat.h>
|
||||
#include <FormattingConventions.h>
|
||||
#include <Language.h>
|
||||
@@ -38,14 +39,19 @@ public:
|
||||
|
||||
// formatting
|
||||
|
||||
ssize_t Format(char* string, size_t maxSize,
|
||||
time_t time, BDateFormatStyle style) const;
|
||||
status_t Format(BString* string, time_t time,
|
||||
BDateFormatStyle style,
|
||||
ssize_t Format(char* string, const size_t maxSize,
|
||||
const time_t time,
|
||||
const BDateFormatStyle style) const;
|
||||
status_t Format(BString& string, const time_t time,
|
||||
const BDateFormatStyle style,
|
||||
const BTimeZone* timeZone = NULL) const;
|
||||
status_t Format(BString* string,
|
||||
status_t Format(BString& string, const BDate& time,
|
||||
const BDateFormatStyle style,
|
||||
const BTimeZone* timeZone = NULL) const;
|
||||
status_t Format(BString& string,
|
||||
int*& fieldPositions, int& fieldCount,
|
||||
time_t time, BDateFormatStyle style) const;
|
||||
const time_t time,
|
||||
const BDateFormatStyle style) const;
|
||||
|
||||
status_t GetFields(BDateElement*& fields,
|
||||
int& fieldCount, BDateFormatStyle style
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* Authors:
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <vector>
|
||||
|
||||
|
||||
// default constructor
|
||||
BDateFormat::BDateFormat(const BLanguage* const language,
|
||||
const BFormattingConventions* const conventions)
|
||||
{
|
||||
@@ -39,13 +38,14 @@ BDateFormat::BDateFormat(const BLanguage* const language,
|
||||
BLocale::Default()->GetLanguage(&fLanguage);
|
||||
}
|
||||
|
||||
// copy constructor
|
||||
|
||||
BDateFormat::BDateFormat(const BDateFormat &other)
|
||||
: fConventions(other.fConventions),
|
||||
fLanguage(other.fLanguage)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/*static*/ const BDateFormat*
|
||||
BDateFormat::Default()
|
||||
{
|
||||
@@ -53,15 +53,14 @@ BDateFormat::Default()
|
||||
}
|
||||
|
||||
|
||||
// destructor
|
||||
BDateFormat::~BDateFormat()
|
||||
{
|
||||
}
|
||||
|
||||
// Format
|
||||
|
||||
ssize_t
|
||||
BDateFormat::Format(char* string, size_t maxSize, time_t time,
|
||||
BDateFormatStyle style) const
|
||||
BDateFormat::Format(char* string, const size_t maxSize, const time_t time,
|
||||
const BDateFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
@@ -87,8 +86,8 @@ BDateFormat::Format(char* string, size_t maxSize, time_t time,
|
||||
|
||||
|
||||
status_t
|
||||
BDateFormat::Format(BString *string, time_t time, BDateFormatStyle style,
|
||||
const BTimeZone* timeZone) const
|
||||
BDateFormat::Format(BString& string, const time_t time,
|
||||
const BDateFormatStyle style, const BTimeZone* timeZone) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
@@ -111,8 +110,8 @@ BDateFormat::Format(BString *string, time_t time, BDateFormatStyle style,
|
||||
UnicodeString icuString;
|
||||
dateFormatter->format((UDate)time * 1000, icuString);
|
||||
|
||||
string->Truncate(0);
|
||||
BStringByteSink stringConverter(string);
|
||||
string.Truncate(0);
|
||||
BStringByteSink stringConverter(&string);
|
||||
icuString.toUTF8(stringConverter);
|
||||
|
||||
return B_OK;
|
||||
@@ -120,8 +119,55 @@ BDateFormat::Format(BString *string, time_t time, BDateFormatStyle style,
|
||||
|
||||
|
||||
status_t
|
||||
BDateFormat::Format(BString* string, int*& fieldPositions, int& fieldCount,
|
||||
time_t time, BDateFormatStyle style) const
|
||||
BDateFormat::Format(BString& string, const BDate& time,
|
||||
const BDateFormatStyle style, const BTimeZone* timeZone) const
|
||||
{
|
||||
if (!time.IsValid())
|
||||
return B_BAD_DATA;
|
||||
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
return B_ERROR;
|
||||
|
||||
BString format;
|
||||
fConventions.GetDateFormat(style, format);
|
||||
ObjectDeleter<DateFormat> dateFormatter(_CreateDateFormatter(format));
|
||||
if (dateFormatter.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
UErrorCode err = U_ZERO_ERROR;
|
||||
ObjectDeleter<Calendar> calendar(Calendar::createInstance(err));
|
||||
if (!U_SUCCESS(err))
|
||||
return B_NO_MEMORY;
|
||||
|
||||
if (timeZone != NULL) {
|
||||
ObjectDeleter<TimeZone> icuTimeZone(
|
||||
TimeZone::createTimeZone(timeZone->ID().String()));
|
||||
if (icuTimeZone.Get() == NULL)
|
||||
return B_NO_MEMORY;
|
||||
dateFormatter->setTimeZone(*icuTimeZone.Get());
|
||||
calendar->setTimeZone(*icuTimeZone.Get());
|
||||
}
|
||||
|
||||
// Note ICU calendar uses months in range 0..11, while we use the more
|
||||
// natural 1..12 in BDate.
|
||||
calendar->set(time.Year(), time.Month() - 1, time.Day());
|
||||
|
||||
UnicodeString icuString;
|
||||
FieldPosition p;
|
||||
dateFormatter->format(*calendar.Get(), icuString, p);
|
||||
|
||||
string.Truncate(0);
|
||||
BStringByteSink stringConverter(&string);
|
||||
icuString.toUTF8(stringConverter);
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
BDateFormat::Format(BString& string, int*& fieldPositions, int& fieldCount,
|
||||
const time_t time, const BDateFormatStyle style) const
|
||||
{
|
||||
BAutolock lock(fLock);
|
||||
if (!lock.IsLocked())
|
||||
@@ -157,8 +203,8 @@ BDateFormat::Format(BString* string, int*& fieldPositions, int& fieldCount,
|
||||
for (int i = 0 ; i < fieldCount ; i++ )
|
||||
fieldPositions[i] = fieldPosStorage[i];
|
||||
|
||||
string->Truncate(0);
|
||||
BStringByteSink stringConverter(string);
|
||||
string.Truncate(0);
|
||||
BStringByteSink stringConverter(&string);
|
||||
|
||||
icuString.toUTF8(stringConverter);
|
||||
|
||||
|
||||
@@ -202,7 +202,6 @@ BTime::SetTime(int32 hour, int32 minute, int32 second, int32 microsecond)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
Adds \c hours to the current time. If the passed value is negative it
|
||||
will become earlier. Note: The time will wrap if it passes midnight.
|
||||
|
||||
@@ -214,7 +214,7 @@ TruncTimeBase(BString* outString, int64 value, const View* view, float width)
|
||||
}
|
||||
|
||||
if (resultWidth > width
|
||||
&& BDateFormat::Default()->Format(&date, timeValue,
|
||||
&& BDateFormat::Default()->Format(date, timeValue,
|
||||
B_SHORT_DATE_FORMAT) == B_OK) {
|
||||
resultWidth = view->StringWidth(date.String(), date.Length());
|
||||
}
|
||||
|
||||
@@ -361,16 +361,16 @@ FormatSettingsView::_UpdateExamples()
|
||||
time_t timeValue = (time_t)time(NULL);
|
||||
BString result;
|
||||
|
||||
BDateFormat::Default()->Format(&result, timeValue, B_FULL_DATE_FORMAT);
|
||||
BDateFormat::Default()->Format(result, timeValue, B_FULL_DATE_FORMAT);
|
||||
fFullDateExampleView->SetText(result);
|
||||
|
||||
BDateFormat::Default()->Format(&result, timeValue, B_LONG_DATE_FORMAT);
|
||||
BDateFormat::Default()->Format(result, timeValue, B_LONG_DATE_FORMAT);
|
||||
fLongDateExampleView->SetText(result);
|
||||
|
||||
BDateFormat::Default()->Format(&result, timeValue, B_MEDIUM_DATE_FORMAT);
|
||||
BDateFormat::Default()->Format(result, timeValue, B_MEDIUM_DATE_FORMAT);
|
||||
fMediumDateExampleView->SetText(result);
|
||||
|
||||
BDateFormat::Default()->Format(&result, timeValue, B_SHORT_DATE_FORMAT);
|
||||
BDateFormat::Default()->Format(result, timeValue, B_SHORT_DATE_FORMAT);
|
||||
fShortDateExampleView->SetText(result);
|
||||
|
||||
BLocale::Default()->FormatTime(&result, timeValue, B_FULL_TIME_FORMAT);
|
||||
|
||||
@@ -623,7 +623,7 @@ TDateEdit::_UpdateFields()
|
||||
free(fFieldPositions);
|
||||
fFieldPositions = NULL;
|
||||
}
|
||||
BDateFormat::Default()->Format(&fText, fFieldPositions, fFieldPosCount,
|
||||
BDateFormat::Default()->Format(fText, fFieldPositions, fFieldPosCount,
|
||||
time, B_SHORT_DATE_FORMAT);
|
||||
|
||||
if (fFields != NULL) {
|
||||
|
||||
@@ -51,7 +51,7 @@ TimeZoneListView::GetToolTipAt(BPoint point, BToolTip** _tip)
|
||||
&item->TimeZone());
|
||||
|
||||
BString dateInTimeZone;
|
||||
BDateFormat::Default()->Format(&dateInTimeZone, now, B_SHORT_DATE_FORMAT,
|
||||
BDateFormat::Default()->Format(dateInTimeZone, now, B_SHORT_DATE_FORMAT,
|
||||
&item->TimeZone());
|
||||
|
||||
BString toolTip = item->Text();
|
||||
|
||||
Reference in New Issue
Block a user