AppDateTimeFormat.h (3012B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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/. */ 6 7 #ifndef mozilla_intl_AppDateTimeFormat_h 8 #define mozilla_intl_AppDateTimeFormat_h 9 10 #include <time.h> 11 #include "gtest/MozGtestFriend.h" 12 #include "nsTHashMap.h" 13 #include "nsString.h" 14 #include "prtime.h" 15 #include "mozilla/intl/DateTimeFormat.h" 16 17 namespace mozilla::intl { 18 19 /** 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. 23 * 24 * This class is not thread-safe as it lazily initializes a cache without 25 * any type of multi-threaded protections. 26 */ 27 class AppDateTimeFormat { 28 public: 29 /** 30 * Format a DateTime using the applied app and OS-level preferences, with a 31 * style bag and the PRTime. 32 */ 33 static nsresult Format(const DateTimeFormat::StyleBag& aStyle, 34 const PRTime aPrTime, nsAString& aStringOut); 35 36 /** 37 * Format a DateTime using the applied app and OS-level preferences, with a 38 * style bag and the PRExplodedTime. 39 */ 40 static nsresult Format(const DateTimeFormat::StyleBag& aStyle, 41 const PRExplodedTime* aExplodedTime, 42 nsAString& aStringOut); 43 44 /** 45 * Format a DateTime using the applied app and OS-level preferences, with a 46 * components bag and the PRExplodedTime. 47 */ 48 static nsresult Format(const DateTimeFormat::ComponentsBag& aComponents, 49 const PRExplodedTime* aExplodedTime, 50 nsAString& aStringOut); 51 52 /** 53 * If the app locale changes, the cached locale needs to be reset. 54 */ 55 static void ClearLocaleCache(); 56 57 static void Shutdown(); 58 59 private: 60 AppDateTimeFormat() = delete; 61 62 static nsresult Initialize(); 63 static void DeleteCache(); 64 static const size_t kMaxCachedFormats = 15; 65 66 FRIEND_TEST(AppDateTimeFormat, FormatPRExplodedTime); 67 FRIEND_TEST(AppDateTimeFormat, DateFormatSelectors); 68 FRIEND_TEST(AppDateTimeFormat, FormatPRExplodedTimeForeign); 69 FRIEND_TEST(AppDateTimeFormat, DateFormatSelectorsForeign); 70 71 /** 72 * Format a DateTime using the applied app and OS-level preferences, with a 73 * components bag and the PRExplodedTime. 74 */ 75 static nsresult Format(const DateTimeFormat::StyleBag& aStyle, 76 const double aUnixEpoch, 77 const PRTimeParameters* aTimeParameters, 78 nsAString& aStringOut); 79 80 static void BuildTimeZoneString(const PRTimeParameters& aTimeParameters, 81 nsAString& aStringOut); 82 83 static nsCString* sLocale; 84 static nsTHashMap<nsCStringHashKey, UniquePtr<DateTimeFormat>>* sFormatCache; 85 }; 86 87 } // namespace mozilla::intl 88 89 #endif /* mozilla_intl_AppDateTimeFormat_h */