diff --git a/headers/os/locale/DateFormat.h b/headers/os/locale/DateFormat.h index 31dbbc27f0..ec11d7fead 100644 --- a/headers/os/locale/DateFormat.h +++ b/headers/os/locale/DateFormat.h @@ -6,6 +6,7 @@ #define _B_DATE_FORMAT_H_ +#include #include #include #include @@ -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 diff --git a/src/kits/locale/DateFormat.cpp b/src/kits/locale/DateFormat.cpp index 08a80c2b9f..8521a42e9b 100644 --- a/src/kits/locale/DateFormat.cpp +++ b/src/kits/locale/DateFormat.cpp @@ -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 -// 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 dateFormatter(_CreateDateFormatter(format)); + if (dateFormatter.Get() == NULL) + return B_NO_MEMORY; + + UErrorCode err = U_ZERO_ERROR; + ObjectDeleter calendar(Calendar::createInstance(err)); + if (!U_SUCCESS(err)) + return B_NO_MEMORY; + + if (timeZone != NULL) { + ObjectDeleter 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); diff --git a/src/kits/support/DateTime.cpp b/src/kits/support/DateTime.cpp index 3d4a051751..e07e437444 100644 --- a/src/kits/support/DateTime.cpp +++ b/src/kits/support/DateTime.cpp @@ -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. diff --git a/src/kits/tracker/WidgetAttributeText.cpp b/src/kits/tracker/WidgetAttributeText.cpp index 5daff1f3ed..365fe880e7 100644 --- a/src/kits/tracker/WidgetAttributeText.cpp +++ b/src/kits/tracker/WidgetAttributeText.cpp @@ -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()); } diff --git a/src/preferences/locale/FormatSettingsView.cpp b/src/preferences/locale/FormatSettingsView.cpp index 0b58029663..d7fee35c36 100644 --- a/src/preferences/locale/FormatSettingsView.cpp +++ b/src/preferences/locale/FormatSettingsView.cpp @@ -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); diff --git a/src/preferences/time/DateTimeEdit.cpp b/src/preferences/time/DateTimeEdit.cpp index eb2dcc941f..b54af09fdd 100644 --- a/src/preferences/time/DateTimeEdit.cpp +++ b/src/preferences/time/DateTimeEdit.cpp @@ -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) { diff --git a/src/preferences/time/TimeZoneListView.cpp b/src/preferences/time/TimeZoneListView.cpp index 61ca51cc16..e690201898 100644 --- a/src/preferences/time/TimeZoneListView.cpp +++ b/src/preferences/time/TimeZoneListView.cpp @@ -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();