Skip to content

Commit

Permalink
Move date formatting from BLocale to BDateFormat
Browse files Browse the repository at this point in the history
* There is a little code duplication. This will be moved to BFormat once
the time and datetime formatting is also moved out of BLocale
* The way to create a BDateFormat from a BLocale is still open for
discussion. I'm undecided between making BDateFormat a member of
BLocale, or adding a BDateFormat(const BLocale&) constructor.
* Adjust all users of the API.
  • Loading branch information
pulkomandy committed Sep 26, 2014
1 parent a122146 commit 2a5e33a
Show file tree
Hide file tree
Showing 16 changed files with 365 additions and 288 deletions.
54 changes: 46 additions & 8 deletions headers/os/locale/DateFormat.h
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
/*
* Copyright 2010, Haiku, Inc.
* Copyright 2010-2014, Haiku, Inc.
* Distributed under the terms of the MIT Licence.
*/
#ifndef _B_DATE_FORMAT_H_
#define _B_DATE_FORMAT_H_


#include <DateTimeFormat.h>
#include <FormattingConventions.h>
#include <Language.h>
#include <Locker.h>


class BString;
class BTimeZone;


enum BWeekday {
B_WEEKDAY_MONDAY = 1,
B_WEEKDAY_TUESDAY,
B_WEEKDAY_WEDNESDAY,
B_WEEKDAY_THURSDAY,
B_WEEKDAY_FRIDAY,
B_WEEKDAY_SATURDAY,
B_WEEKDAY_SUNDAY,
};


class BDateFormat : public BDateTimeFormat {
class BDateFormat {
public:
BDateFormat();
BDateFormat(const BLanguage* const,
const BFormattingConventions* const);
BDateFormat(const BDateFormat &other);
virtual ~BDateFormat();

static const BDateFormat* Default();

// formatting

// no-frills version: Simply appends the
// formatted date to the string buffer.
// Can fail only with B_NO_MEMORY or B_BAD_VALUE.
virtual status_t Format(bigtime_t value, BString* buffer) const;
ssize_t Format(char* string, size_t maxSize,
time_t time, BDateFormatStyle style) const;
status_t Format(BString* string, time_t time,
BDateFormatStyle style,
const BTimeZone* timeZone = NULL) const;
status_t Format(BString* string,
int*& fieldPositions, int& fieldCount,
time_t time, BDateFormatStyle style) const;

status_t GetFields(BDateElement*& fields,
int& fieldCount, BDateFormatStyle style
) const;

status_t GetStartOfWeek(BWeekday* weekday) const;

// TODO parsing

private:
icu::DateFormat* _CreateDateFormatter(
const BString& format) const;

// TODO: ... all, basically!
mutable BLocker fLock;
BFormattingConventions fConventions;
BLanguage fLanguage;
};


Expand Down
16 changes: 15 additions & 1 deletion headers/os/locale/DateTimeFormat.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
/*
* Copyright 2010, Haiku, Inc.
* Copyright 2010-2014, Haiku, Inc.
* Distributed under the terms of the MIT Licence.
*/
#ifndef _B_DATE_TIME_FORMAT_H_
#define _B_DATE_TIME_FORMAT_H_


#include <Format.h>
#include <FormatParameters.h>


class BString;


enum BDateElement {
B_DATE_ELEMENT_INVALID = B_BAD_DATA,
B_DATE_ELEMENT_YEAR = 0,
B_DATE_ELEMENT_MONTH,
B_DATE_ELEMENT_DAY,
B_DATE_ELEMENT_AM_PM,
B_DATE_ELEMENT_HOUR,
B_DATE_ELEMENT_MINUTE,
B_DATE_ELEMENT_SECOND
};


class BDateTimeFormat : public BFormat {
public:
BDateTimeFormat();
Expand Down
48 changes: 2 additions & 46 deletions headers/os/locale/Locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


#include <Collator.h>
#include <DateTimeFormat.h>
#include <FormattingConventions.h>
#include <Language.h>
#include <Locker.h>
Expand All @@ -18,21 +19,11 @@ namespace icu {


class BCatalog;
class BDateFormat;
class BString;
class BTimeZone;


enum BDateElement {
B_DATE_ELEMENT_INVALID = B_BAD_DATA,
B_DATE_ELEMENT_YEAR = 0,
B_DATE_ELEMENT_MONTH,
B_DATE_ELEMENT_DAY,
B_DATE_ELEMENT_AM_PM,
B_DATE_ELEMENT_HOUR,
B_DATE_ELEMENT_MINUTE,
B_DATE_ELEMENT_SECOND
};

enum BNumberElement {
B_NUMBER_ELEMENT_INVALID = B_BAD_DATA,
B_NUMBER_ELEMENT_INTEGER = 0,
Expand All @@ -41,18 +32,6 @@ enum BNumberElement {
};


// TODO: move this to BCalendar (should we ever have that) or BDate
enum BWeekday {
B_WEEKDAY_MONDAY = 1,
B_WEEKDAY_TUESDAY,
B_WEEKDAY_WEDNESDAY,
B_WEEKDAY_THURSDAY,
B_WEEKDAY_FRIDAY,
B_WEEKDAY_SATURDAY,
B_WEEKDAY_SUNDAY,
};


class BLocale {
public:
BLocale(const BLanguage* language = NULL,
Expand All @@ -78,11 +57,6 @@ class BLocale {
// see definitions in LocaleStrings.h
const char* GetString(uint32 id) const;

void FormatString(char* target, size_t maxSize,
char* fmt, ...) const;
void FormatString(BString* buffer, char* fmt,
...) const;

// DateTime

// TODO: drop some of these once BDateTimeFormat
Expand All @@ -95,24 +69,6 @@ class BLocale {
BTimeFormatStyle timeStyle,
const BTimeZone* timeZone = NULL) const;

// Date

// TODO: drop some of these once BDateFormat
// has been implemented!
ssize_t FormatDate(char* string, size_t maxSize,
time_t time, BDateFormatStyle style) const;
status_t FormatDate(BString* string, time_t time,
BDateFormatStyle style,
const BTimeZone* timeZone = NULL) const;
status_t FormatDate(BString* string,
int*& fieldPositions, int& fieldCount,
time_t time, BDateFormatStyle style) const;
status_t GetDateFields(BDateElement*& fields,
int& fieldCount, BDateFormatStyle style
) const;

status_t GetStartOfWeek(BWeekday* weekday) const;

// Time

// TODO: drop some of these once BTimeFormat
Expand Down
2 changes: 2 additions & 0 deletions headers/os/locale/LocaleRoster.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BBitmap;
class BCatalog;
class BCollator;
class BCountry;
class BDateFormat;
class BFormattingConventions;
class BLanguage;
class BLocale;
Expand Down Expand Up @@ -78,6 +79,7 @@ class BLocaleRoster {
// (that needs to link with liblocalestub.a)

const BLocale* GetDefaultLocale() const;
const BDateFormat* GetDefaultDateFormat() const;

bool IsFilesystemTranslationPreferred() const;

Expand Down
2 changes: 2 additions & 0 deletions headers/private/locale/LocaleRosterData.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


#include <Collator.h>
#include <DateFormat.h>
#include <FormattingConventions.h>
#include <image.h>
#include <Language.h>
Expand Down Expand Up @@ -37,6 +38,7 @@ struct LocaleRosterData {

BLocale fDefaultLocale;
BTimeZone fDefaultTimeZone;
BDateFormat fDefaultDateFormat;

bool fIsFilesystemTranslationPreferred;

Expand Down
1 change: 1 addition & 0 deletions headers/private/shared/CalendarView.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "DateTime.h"


#include <DateFormat.h>
#include <Invoker.h>
#include <List.h>
#include <Locale.h>
Expand Down
3 changes: 2 additions & 1 deletion src/apps/deskbar/TimeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ All rights reserved.

#include <Application.h>
#include <Catalog.h>
#include <DateFormat.h>
#include <Debug.h>
#include <Locale.h>
#include <MenuItem.h>
Expand Down Expand Up @@ -421,7 +422,7 @@ TTimeView::GetCurrentDate()
{
char tmp[sizeof(fCurrentDateStr)];

fLocale.FormatDate(tmp, sizeof(fCurrentDateStr), fCurrentTime,
BDateFormat::Default()->Format(tmp, sizeof(fCurrentDateStr), fCurrentTime,
B_FULL_DATE_FORMAT);

// remove leading 0 from date when month is less than 10 (MM/DD/YY)
Expand Down

0 comments on commit 2a5e33a

Please sign in to comment.