Skip to content

Commit

Permalink
Bug 1715892 - Unify locale/DateTimeFormat to mozilla::intl::AppDateTi…
Browse files Browse the repository at this point in the history
…meFormat; r=platform-i18n-reviewers,dminor

I considered removing this class initially, but it's actually a pretty
useful abstraction over the DateTimeFormat interface when used
specifically with Gecko. It applies the OS preferences and provides some
caching behavior.

Differential Revision: https://phabricator.services.mozilla.com/D131671
  • Loading branch information
gregtatum committed Dec 1, 2021
1 parent 7562365 commit d642b72
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 439 deletions.
347 changes: 109 additions & 238 deletions intl/locale/AppDateTimeFormat.cpp

Large diffs are not rendered by default.

113 changes: 48 additions & 65 deletions intl/locale/AppDateTimeFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,78 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_DateTimeFormat_h
#define mozilla_DateTimeFormat_h
#ifndef mozilla_intl_AppDateTimeFormat_h
#define mozilla_intl_AppDateTimeFormat_h

#include <time.h>
#include "gtest/MozGtestFriend.h"
#include "nsTHashMap.h"
#include "nsString.h"
#include "prtime.h"
#include "unicode/udat.h"
#include "mozilla/intl/DateTimeFormat.h"

namespace mozilla {
namespace mozilla::intl {

enum nsDateFormatSelector : long {
// Do not change the order of the values below (see bug 1225696).
kDateFormatNone = 0, // do not include the date in the format string
kDateFormatLong, // provides the long date format for the given locale
kDateFormatShort, // provides the short date format for the given locale
};

enum nsTimeFormatSelector : long {
kTimeFormatNone = 0, // don't include the time in the format string
kTimeFormatLong, // provides the time format with seconds in the given
// locale
kTimeFormatShort // provides the time format without seconds in the given
// locale
};

class DateTimeFormat {
/**
* Get a DateTimeFormat for use in Gecko. This specialized DateTimeFormat
* respects the user's OS and app preferences, and provides caching of the
* underlying mozilla::intl resources.
*/
class AppDateTimeFormat {
public:
enum class Field { Month, Weekday };

enum class Style { Wide, Abbreviated };

// Weekday (E, EEEE) only used in Thunderbird.
enum class Skeleton { yyyyMM, yyyyMMMM, E, EEEE };

// performs a locale sensitive date formatting operation on the PRTime
// parameter
static nsresult FormatPRTime(const nsDateFormatSelector aDateFormatSelector,
const nsTimeFormatSelector aTimeFormatSelector,
const PRTime aPrTime, nsAString& aStringOut);

// performs a locale sensitive date formatting operation on the PRExplodedTime
// parameter
static nsresult FormatPRExplodedTime(
const nsDateFormatSelector aDateFormatSelector,
const nsTimeFormatSelector aTimeFormatSelector,
const PRExplodedTime* aExplodedTime, nsAString& aStringOut);

// performs a locale sensitive date formatting operation on the PRExplodedTime
// parameter, using the specified options.
static nsresult FormatDateTime(const PRExplodedTime* aExplodedTime,
const Skeleton aSkeleton,
nsAString& aStringOut);

// finds the locale sensitive display name for the specified field on the
// PRExplodedTime parameter
static nsresult GetCalendarSymbol(const Field aField, const Style aStyle,
const PRExplodedTime* aExplodedTime,
nsAString& aStringOut);
/**
* Format a DateTime using the applied app and OS-level preferences, with a
* style bag and the PRTime.
*/
static nsresult Format(const DateTimeFormat::StyleBag& aStyle,
const PRTime aPrTime, nsAString& aStringOut);

/**
* Format a DateTime using the applied app and OS-level preferences, with a
* style bag and the PRExplodedTime.
*/
static nsresult Format(const DateTimeFormat::StyleBag& aStyle,
const PRExplodedTime* aExplodedTime,
nsAString& aStringOut);

/**
* Format a DateTime using the applied app and OS-level preferences, with a
* components bag and the PRExplodedTime.
*/
static nsresult Format(const DateTimeFormat::ComponentsBag& aComponents,
const PRExplodedTime* aExplodedTime,
nsAString& aStringOut);

static void Shutdown();

private:
DateTimeFormat() = delete;
AppDateTimeFormat() = delete;

static nsresult Initialize();
static void DeleteCache();
static const size_t kMaxCachedFormats = 15;

FRIEND_TEST(DateTimeFormat, FormatPRExplodedTime);
FRIEND_TEST(DateTimeFormat, DateFormatSelectors);
FRIEND_TEST(DateTimeFormat, FormatPRExplodedTimeForeign);
FRIEND_TEST(DateTimeFormat, DateFormatSelectorsForeign);
FRIEND_TEST(AppDateTimeFormat, FormatPRExplodedTime);
FRIEND_TEST(AppDateTimeFormat, DateFormatSelectors);
FRIEND_TEST(AppDateTimeFormat, FormatPRExplodedTimeForeign);
FRIEND_TEST(AppDateTimeFormat, DateFormatSelectorsForeign);

// performs a locale sensitive date formatting operation on the UDate
// parameter
static nsresult FormatUDateTime(
const nsDateFormatSelector aDateFormatSelector,
const nsTimeFormatSelector aTimeFormatSelector, const UDate aUDateTime,
const PRTimeParameters* aTimeParameters, nsAString& aStringOut);
/**
* Format a DateTime using the applied app and OS-level preferences, with a
* components bag and the PRExplodedTime.
*/
static nsresult Format(const DateTimeFormat::StyleBag& aStyle,
const double aUnixEpoch,
const PRTimeParameters* aTimeParameters,
nsAString& aStringOut);

static void BuildTimeZoneString(const PRTimeParameters& aTimeParameters,
nsAString& aStringOut);

static nsCString* mLocale;
static nsTHashMap<nsCStringHashKey, UDateFormat*>* mFormatCache;
static nsTHashMap<nsCStringHashKey, UniquePtr<DateTimeFormat>>* mFormatCache;
};

} // namespace mozilla
} // namespace mozilla::intl

#endif /* mozilla_DateTimeFormat_h */
#endif /* mozilla_intl_AppDateTimeFormat_h */
4 changes: 2 additions & 2 deletions intl/locale/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ XPIDL_SOURCES += [
XPIDL_MODULE = "locale"

EXPORTS += [
"DateTimeFormat.h",
"nsLanguageAtomService.h",
"nsUConvPropertySearch.h",
]

EXPORTS.mozilla.intl += [
"AppDateTimeFormat.h",
"LocaleService.h",
"MozLocaleBindings.h",
"OSPreferences.h",
"Quotes.h",
]

UNIFIED_SOURCES += [
"DateTimeFormat.cpp",
"AppDateTimeFormat.cpp",
"LocaleService.cpp",
"nsLanguageAtomService.cpp",
"nsUConvPropertySearch.cpp",
Expand Down
Loading

0 comments on commit d642b72

Please sign in to comment.