1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_intl_AppDateTimeFormat_h
8 #define mozilla_intl_AppDateTimeFormat_h
11 #include "gtest/MozGtestFriend.h"
12 #include "nsTHashMap.h"
15 #include "mozilla/intl/DateTimeFormat.h"
17 namespace mozilla::intl
{
20 * Get a DateTimeFormat for use in Gecko. This specialized DateTimeFormat
21 * respects the user's OS and app preferences, and provides caching of the
22 * underlying mozilla::intl resources.
24 * This class is not thread-safe as it lazily initializes a cache without
25 * any type of multi-threaded protections.
27 class AppDateTimeFormat
{
30 * Format a DateTime using the applied app and OS-level preferences, with a
31 * style bag and the PRTime.
33 static nsresult
Format(const DateTimeFormat::StyleBag
& aStyle
,
34 const PRTime aPrTime
, nsAString
& aStringOut
);
37 * Format a DateTime using the applied app and OS-level preferences, with a
38 * style bag and the PRExplodedTime.
40 static nsresult
Format(const DateTimeFormat::StyleBag
& aStyle
,
41 const PRExplodedTime
* aExplodedTime
,
42 nsAString
& aStringOut
);
45 * Format a DateTime using the applied app and OS-level preferences, with a
46 * components bag and the PRExplodedTime.
48 static nsresult
Format(const DateTimeFormat::ComponentsBag
& aComponents
,
49 const PRExplodedTime
* aExplodedTime
,
50 nsAString
& aStringOut
);
53 * If the app locale changes, the cached locale needs to be reset.
55 static void ClearLocaleCache();
57 static void Shutdown();
60 AppDateTimeFormat() = delete;
62 static nsresult
Initialize();
63 static void DeleteCache();
64 static const size_t kMaxCachedFormats
= 15;
66 FRIEND_TEST(AppDateTimeFormat
, FormatPRExplodedTime
);
67 FRIEND_TEST(AppDateTimeFormat
, DateFormatSelectors
);
68 FRIEND_TEST(AppDateTimeFormat
, FormatPRExplodedTimeForeign
);
69 FRIEND_TEST(AppDateTimeFormat
, DateFormatSelectorsForeign
);
72 * Format a DateTime using the applied app and OS-level preferences, with a
73 * components bag and the PRExplodedTime.
75 static nsresult
Format(const DateTimeFormat::StyleBag
& aStyle
,
76 const double aUnixEpoch
,
77 const PRTimeParameters
* aTimeParameters
,
78 nsAString
& aStringOut
);
80 static void BuildTimeZoneString(const PRTimeParameters
& aTimeParameters
,
81 nsAString
& aStringOut
);
83 static nsCString
* sLocale
;
84 static nsTHashMap
<nsCStringHashKey
, UniquePtr
<DateTimeFormat
>>* sFormatCache
;
87 } // namespace mozilla::intl
89 #endif /* mozilla_intl_AppDateTimeFormat_h */