tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Calendar.h (3556B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 #ifndef intl_components_Calendar_h_
      5 #define intl_components_Calendar_h_
      6 
      7 #include "mozilla/Assertions.h"
      8 #include "mozilla/EnumSet.h"
      9 #include "mozilla/intl/ICU4CGlue.h"
     10 #include "mozilla/intl/ICUError.h"
     11 #include "mozilla/Maybe.h"
     12 #include "mozilla/Result.h"
     13 #include "mozilla/Span.h"
     14 #include "mozilla/UniquePtr.h"
     15 
     16 using UCalendar = void*;
     17 
     18 namespace mozilla::intl {
     19 
     20 /**
     21 * Weekdays in the ISO-8601 calendar.
     22 */
     23 enum class Weekday : uint8_t {
     24  Monday = 1,
     25  Tuesday,
     26  Wednesday,
     27  Thursday,
     28  Friday,
     29  Saturday,
     30  Sunday,
     31 };
     32 
     33 /**
     34 * This component is a Mozilla-focused API for working with calendar systems in
     35 * internationalization code. It is used in coordination with other operations
     36 * such as datetime formatting.
     37 */
     38 class Calendar final {
     39 public:
     40  explicit Calendar(UCalendar* aCalendar) : mCalendar(aCalendar) {
     41    MOZ_ASSERT(aCalendar);
     42  };
     43 
     44  // Do not allow copy as this class owns the ICU resource. Move is not
     45  // currently implemented, but a custom move operator could be created if
     46  // needed.
     47  Calendar(const Calendar&) = delete;
     48  Calendar& operator=(const Calendar&) = delete;
     49 
     50  /**
     51   * Create a Calendar.
     52   */
     53  static Result<UniquePtr<Calendar>, ICUError> TryCreate(
     54      const char* aLocale,
     55      Maybe<Span<const char16_t>> aTimeZoneOverride = Nothing{});
     56 
     57  /**
     58   * Get the BCP 47 keyword value string designating the calendar type. For
     59   * instance "gregory", "chinese", "islamic-civil", etc.
     60   */
     61  Result<Span<const char>, ICUError> GetBcp47Type();
     62 
     63  /**
     64   * Return the set of weekdays which are considered as part of the weekend.
     65   */
     66  Result<EnumSet<Weekday>, ICUError> GetWeekend();
     67 
     68  /**
     69   * Return the weekday which is considered the first day of the week.
     70   */
     71  Weekday GetFirstDayOfWeek();
     72 
     73  /**
     74   * Return the minimal number of days in the first week of a year.
     75   */
     76  int32_t GetMinimalDaysInFirstWeek();
     77 
     78  /**
     79   * Set the time for the calendar relative to the number of milliseconds since
     80   * 1 January 1970, UTC.
     81   */
     82  Result<Ok, ICUError> SetTimeInMs(double aUnixEpoch);
     83 
     84  /**
     85   * Return ICU legacy keywords, such as "gregorian", "islamic",
     86   * "islamic-civil", "hebrew", etc.
     87   */
     88  static Result<SpanEnumeration<char>, ICUError>
     89  GetLegacyKeywordValuesForLocale(const char* aLocale);
     90 
     91 private:
     92  /**
     93   * Internal function to convert a legacy calendar identifier to the newer
     94   * BCP 47 identifier.
     95   */
     96  static SpanResult<char> LegacyIdentifierToBcp47(const char* aIdentifier,
     97                                                  int32_t aLength);
     98 
     99 public:
    100  enum class CommonlyUsed : bool {
    101    /**
    102     * Select all possible values, even when not commonly used by a locale.
    103     */
    104    No,
    105 
    106    /**
    107     * Only select the values which are commonly used by a locale.
    108     */
    109    Yes,
    110  };
    111 
    112  using Bcp47IdentifierEnumeration =
    113      Enumeration<char, SpanResult<char>, Calendar::LegacyIdentifierToBcp47>;
    114 
    115  /**
    116   * Return BCP 47 Unicode locale extension type keywords.
    117   */
    118  static Result<Bcp47IdentifierEnumeration, ICUError>
    119  GetBcp47KeywordValuesForLocale(const char* aLocale,
    120                                 CommonlyUsed aCommonlyUsed = CommonlyUsed::No);
    121 
    122  ~Calendar();
    123 
    124 private:
    125  friend class DateIntervalFormat;
    126  UCalendar* GetUCalendar() const { return mCalendar; }
    127 
    128  UCalendar* mCalendar = nullptr;
    129 };
    130 
    131 }  // namespace mozilla::intl
    132 
    133 #endif