DeskBar: cache time and date formats

Creating BDateTimeFormat and BDateFormat objects is a costly operation
(it loads locale data from ICU, etc). So, we should do it only once when
the format changes, instead of doing it each time we format a date
or time.
This commit is contained in:
Adrien Destugues 2017-11-21 15:03:17 +01:00
parent b06e48bb2b
commit 8c82d0edd5
2 changed files with 29 additions and 13 deletions

View File

@ -74,7 +74,9 @@ TTimeView::TTimeView(float maxWidth, float height)
fShowLevel(0), fShowLevel(0),
fShowSeconds(false), fShowSeconds(false),
fShowDayOfWeek(false), fShowDayOfWeek(false),
fShowTimeZone(false) fShowTimeZone(false),
fTimeFormat(NULL),
fDateFormat(NULL)
{ {
fCurrentTime = fLastTime = time(NULL); fCurrentTime = fLastTime = time(NULL);
fSeconds = fMinute = fHour = 0; fSeconds = fMinute = fHour = 0;
@ -83,24 +85,28 @@ TTimeView::TTimeView(float maxWidth, float height)
fLastTimeStr[0] = 0; fLastTimeStr[0] = 0;
fLastDateStr[0] = 0; fLastDateStr[0] = 0;
fNeedToUpdate = true; fNeedToUpdate = true;
fLocale = *BLocale::Default(); UpdateTimeFormat();
} }
#ifdef AS_REPLICANT #ifdef AS_REPLICANT
TTimeView::TTimeView(BMessage* data) TTimeView::TTimeView(BMessage* data)
: BView(data) : BView(data),
fTimeFormat(NULL),
fDateFormat(NULL)
{ {
fCurrentTime = fLastTime = time(NULL); fCurrentTime = fLastTime = time(NULL);
data->FindBool("seconds", &fShowSeconds); data->FindBool("seconds", &fShowSeconds);
fLocale = *BLocale::Default(); UpdateTimeFormat();
} }
#endif #endif
TTimeView::~TTimeView() TTimeView::~TTimeView()
{ {
delete fTimeFormat;
delete fDateFormat;
} }
@ -322,6 +328,7 @@ void
TTimeView::SetShowSeconds(bool show) TTimeView::SetShowSeconds(bool show)
{ {
fShowSeconds = show; fShowSeconds = show;
UpdateTimeFormat();
Update(); Update();
} }
@ -337,6 +344,7 @@ void
TTimeView::SetShowDayOfWeek(bool show) TTimeView::SetShowDayOfWeek(bool show)
{ {
fShowDayOfWeek = show; fShowDayOfWeek = show;
UpdateTimeFormat();
Update(); Update();
} }
@ -352,6 +360,7 @@ void
TTimeView::SetShowTimeZone(bool show) TTimeView::SetShowTimeZone(bool show)
{ {
fShowTimeZone = show; fShowTimeZone = show;
UpdateTimeFormat();
Update(); Update();
} }
@ -386,7 +395,7 @@ TTimeView::ShowCalendar(BPoint where)
void void
TTimeView::GetCurrentTime() TTimeView::UpdateTimeFormat()
{ {
int32 fields = B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE; int32 fields = B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE;
if (fShowSeconds) if (fShowSeconds)
@ -396,10 +405,18 @@ TTimeView::GetCurrentTime()
if (fShowTimeZone) if (fShowTimeZone)
fields |= B_DATE_ELEMENT_TIMEZONE; fields |= B_DATE_ELEMENT_TIMEZONE;
BDateTimeFormat format(&fLocale); delete fTimeFormat;
format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, fields); fTimeFormat = new BDateTimeFormat(BLocale::Default());
fTimeFormat->SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, fields);
format.Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), fCurrentTime, delete fDateFormat;
fDateFormat = new BDateFormat(BLocale::Default());
}
void
TTimeView::GetCurrentTime()
{
fTimeFormat->Format(fCurrentTimeStr, sizeof(fCurrentTimeStr), fCurrentTime,
B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT); B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT);
} }
@ -409,8 +426,7 @@ TTimeView::GetCurrentDate()
{ {
char tmp[sizeof(fCurrentDateStr)]; char tmp[sizeof(fCurrentDateStr)];
BDateFormat format(&fLocale); fDateFormat->Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
format.Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
B_FULL_DATE_FORMAT); B_FULL_DATE_FORMAT);
// remove leading 0 from date when month is less than 10 (MM/DD/YY) // remove leading 0 from date when month is less than 10 (MM/DD/YY)
@ -476,8 +492,6 @@ TTimeView::ShowTimeOptions(BPoint point)
void void
TTimeView::Update() TTimeView::Update()
{ {
fLocale = *BLocale::Default();
GetCurrentTime(); GetCurrentTime();
GetCurrentDate(); GetCurrentDate();
SetToolTip(fCurrentDateStr); SetToolTip(fCurrentDateStr);

View File

@ -114,6 +114,7 @@ private:
void GetCurrentTime(); void GetCurrentTime();
void GetCurrentDate(); void GetCurrentDate();
void UpdateTimeFormat();
void CalculateTextPlacement(); void CalculateTextPlacement();
void ShowTimeOptions(BPoint); void ShowTimeOptions(BPoint);
void Update(); void Update();
@ -149,7 +150,8 @@ private:
BMessenger fCalendarWindow; BMessenger fCalendarWindow;
// For date and time localization purposes // For date and time localization purposes
BLocale fLocale; BDateTimeFormat* fTimeFormat;
BDateFormat* fDateFormat;
}; };