* Add FormatDateTime function to BLocale

* Use the function to localize the date/time in tracker.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37949 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Adrien Destugues 2010-08-07 11:22:16 +00:00
parent abfa93e2e6
commit 4a7b2e0484
5 changed files with 47 additions and 192 deletions

View File

@ -39,10 +39,10 @@ public:
void FormatString(char* target, size_t maxSize,
char* fmt, ...);
void FormatString(BString* buffer, char* fmt, ...);
void FormatDateTime(char* target, size_t maxSize,
const char* fmt, time_t value);
void FormatDateTime(BString* buffer, const char* fmt,
time_t value);
status_t FormatDateTime(char* target, size_t maxSize,
time_t time, bool longFormat);
status_t FormatDateTime(BString* buffer, time_t time,
bool longFormat);
// Date

View File

@ -336,6 +336,37 @@ BLocale::StartOfWeek() const
}
status_t
BLocale::FormatDateTime(char* target, size_t maxSize, time_t time,
bool longFormat)
{
ObjectDeleter<DateFormat> dateFormatter = CreateDateFormat(longFormat,
*fICULocale, longFormat ? fLongDateFormat : fShortDateFormat);
if (dateFormatter.Get() == NULL)
return B_NO_MEMORY;
ObjectDeleter<DateFormat> timeFormatter = CreateTimeFormat(longFormat,
*fICULocale, longFormat ? fLongTimeFormat : fShortTimeFormat);
if (timeFormatter.Get() == NULL)
return B_NO_MEMORY;
UnicodeString ICUString;
ICUString = dateFormatter->format((UDate)time * 1000, ICUString);
ICUString.append(UnicodeString::fromUTF8(", "));
ICUString = timeFormatter->format((UDate)time * 1000, ICUString);
CheckedArrayByteSink stringConverter(target, maxSize);
ICUString.toUTF8(stringConverter);
if (stringConverter.Overflowed())
return B_BAD_VALUE;
return B_OK;
}
// #pragma mark - Time

View File

@ -9528,21 +9528,6 @@ BPoseView::UpdateDateColumns(BMessage *message)
BRect columnRect(Bounds());
if (IsFilePanel()) {
FormatSeparator separator;
DateOrder format;
bool clock;
message->FindInt32("TimeFormatSeparator", (int32*)&separator);
message->FindInt32("DateOrderFormat", (int32*)&format);
message->FindBool("24HrClock", &clock);
TrackerSettings settings;
settings.SetTimeFormatSeparator(separator);
settings.SetDateOrderFormat(format);
settings.SetClockTo24Hr(clock);
}
for (int32 i = 0; i < columnCount; i++) {
BColumn *col = ColumnAt(i);
if (col && col->AttrType() == B_TIME_TYPE) {

View File

@ -72,13 +72,9 @@ class TTrackerState : public Settings {
BooleanValueSetting *fHideDotFiles;
BooleanValueSetting *fTypeAheadFiltering;
BooleanValueSetting *f24HrClock;
ScalarValueSetting *fRecentApplicationsCount;
ScalarValueSetting *fRecentDocumentsCount;
ScalarValueSetting *fRecentFoldersCount;
ScalarValueSetting *fTimeFormatSeparator;
ScalarValueSetting *fDateOrderFormat;
BooleanValueSetting *fShowVolumeSpaceBar;
HexScalarValueSetting *fUsedSpaceColor;
@ -183,10 +179,6 @@ TTrackerState::LoadSettingsIfNeeded()
Add(fRecentDocumentsCount = new ScalarValueSetting("RecentDocuments", 10, "", ""));
Add(fRecentFoldersCount = new ScalarValueSetting("RecentFolders", 10, "", ""));
Add(fTimeFormatSeparator = new ScalarValueSetting("TimeFormatSeparator", 3, "", ""));
Add(fDateOrderFormat = new ScalarValueSetting("DateOrderFormat", 2, "", ""));
Add(f24HrClock = new BooleanValueSetting("24HrClock", false));
Add(fShowVolumeSpaceBar = new BooleanValueSetting("ShowVolumeSpaceBar", true));
Add(fUsedSpaceColor = new HexScalarValueSetting("UsedSpaceColor", 0xc000cb00, "", ""));
@ -492,48 +484,6 @@ TrackerSettings::SetRecentFoldersCount(int32 count)
}
FormatSeparator
TrackerSettings::TimeFormatSeparator()
{
return (FormatSeparator)gTrackerState.fTimeFormatSeparator->Value();
}
void
TrackerSettings::SetTimeFormatSeparator(FormatSeparator separator)
{
gTrackerState.fTimeFormatSeparator->ValueChanged((int32)separator);
}
DateOrder
TrackerSettings::DateOrderFormat()
{
return (DateOrder)gTrackerState.fDateOrderFormat->Value();
}
void
TrackerSettings::SetDateOrderFormat(DateOrder order)
{
gTrackerState.fDateOrderFormat->ValueChanged((int32)order);
}
bool
TrackerSettings::ClockIs24Hr()
{
return gTrackerState.f24HrClock->Value();
}
void
TrackerSettings::SetClockTo24Hr(bool enabled)
{
gTrackerState.f24HrClock->SetValue(enabled);
}
bool
TrackerSettings::DontMoveFilesToTrash()
{

View File

@ -283,129 +283,27 @@ WidgetAttributeText::TruncFileSize(BString *result, int64 value,
return TruncFileSizeBase(result, value, view, width);
}
/*
const char *kTimeFormats[] = {
"%A, %B %d %Y, %I:%M:%S %p", // Monday, July 09 1997, 05:08:15 PM
"%a, %b %d %Y, %I:%M:%S %p", // Mon, Jul 09 1997, 05:08:15 PM
"%a, %b %d %Y, %I:%M %p", // Mon, Jul 09 1997, 05:08 PM
"%b %d %Y, %I:%M %p", // Jul 09 1997, 05:08 PM
"%m/%d/%y, %I:%M %p", // 07/09/97, 05:08 PM
"%m/%d/%y", // 07/09/97
NULL
};
*/
status_t
TimeFormat(BString &string, int32 index, FormatSeparator separator,
DateOrder order, bool clockIs24Hour)
{
if (index >= 6)
return B_ERROR;
BString clockString;
BString dateString;
if (index <= 1)
if (clockIs24Hour)
clockString = "%H:%M:%S";
else
clockString = "%I:%M:%S %p";
else
if (clockIs24Hour)
clockString = "%H:%M";
else
clockString = "%I:%M %p";
if (index <= 3) {
switch (order) {
case kYMDFormat:
dateString = "%Y %! %d";
break;
case kDMYFormat:
dateString = "%d %! %Y";
break;
case kMDYFormat:
// Fall through
case kDateFormatEnd:
// Fall through
default:
dateString = "%! %d %Y";
break;
}
if (index == 0)
dateString.Replace('!', 'B', 1);
else
dateString.Replace('!', 'b', 1);
} else {
switch (order) {
case kYMDFormat:
dateString = "%y!%m!%d";
break;
case kDMYFormat:
dateString = "%d!%m!%y";
break;
case kMDYFormat:
// Fall through
case kDateFormatEnd:
// Fall through
default:
dateString = "%m!%d!%y";
break;
}
char separatorArray[] = {' ', '-', '/', '\\', '.'};
if (separator == kNoSeparator)
dateString.ReplaceAll("!", "");
else
dateString.ReplaceAll('!', separatorArray[separator-1]);
}
if (index == 0)
string = "%A, ";
else if (index < 3)
string = "%a, ";
else
string = "";
string << dateString;
if (index < 5)
string << ", " << clockString;
return B_OK;
}
template <class View>
float
TruncTimeBase(BString *result, int64 value, const View *view, float width)
{
TrackerSettings settings;
FormatSeparator separator = settings.TimeFormatSeparator();
DateOrder order = settings.DateOrderFormat();
bool clockIs24hr = settings.ClockIs24Hr();
float resultWidth = 0;
char buffer[256];
time_t timeValue = (time_t)value;
tm timeData;
// use reentrant version of localtime to avoid having to have a semaphore
// (localtime uses a global structure to do it's conversion)
localtime_r(&timeValue, &timeData);
BString timeFormat;
BLocale here;
be_locale_roster->GetDefaultLocale(&here);
for (int32 index = 0; ; index++) {
if (TimeFormat(timeFormat, index, separator, order, clockIs24hr)
!= B_OK)
break;
strftime(buffer, 256, timeFormat.String(), &timeData);
if (here.FormatDateTime(buffer, 256, timeValue, true) == B_OK) {
resultWidth = view->StringWidth(buffer);
if (resultWidth <= width)
break;
}
if (resultWidth > width && here.FormatDateTime(buffer, 256, timeValue,
false) == B_OK) {
resultWidth = view->StringWidth(buffer);
}
if (resultWidth > width) {
// even the shortest format string didn't do it, insert ellipsis
resultWidth = TruncStringBase(result, buffer, (ssize_t)strlen(buffer),
@ -1115,19 +1013,10 @@ TimeAttributeText::FitValue(BString *result, const BPoseView *view)
bool
TimeAttributeText::CheckSettingsChanged()
TimeAttributeText::CheckSettingsChanged(void)
{
bool changed = fLastClockIs24 != fSettings.ClockIs24Hr()
|| fLastDateOrder != fSettings.DateOrderFormat()
|| fLastTimeFormatSeparator != fSettings.TimeFormatSeparator();
if (changed) {
fLastClockIs24 = fSettings.ClockIs24Hr();
fLastDateOrder = fSettings.DateOrderFormat();
fLastTimeFormatSeparator = fSettings.TimeFormatSeparator();
}
return changed;
// TODO : check against the actual locale settings
return false;
}