summaryrefslogtreecommitdiffstats
path: root/intl/components/src/Calendar.cpp
blob: d44dedaaae4ef49cec0a060c6c2233f224bd6420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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/. */

#include "mozilla/intl/Calendar.h"

#include "unicode/ucal.h"
#include "unicode/uloc.h"
#include "unicode/utypes.h"

namespace mozilla::intl {

/* static */
Result<UniquePtr<Calendar>, ICUError> Calendar::TryCreate(
    const char* aLocale, Maybe<Span<const char16_t>> aTimeZoneOverride) {
  UErrorCode status = U_ZERO_ERROR;
  const UChar* zoneID = nullptr;
  int32_t zoneIDLen = 0;
  if (aTimeZoneOverride) {
    zoneIDLen = static_cast<int32_t>(aTimeZoneOverride->Length());
    zoneID = aTimeZoneOverride->Elements();
  }

  UCalendar* calendar =
      ucal_open(zoneID, zoneIDLen, aLocale, UCAL_DEFAULT, &status);

  if (U_FAILURE(status)) {
    return Err(ToICUError(status));
  }

  return MakeUnique<Calendar>(calendar);
}

Result<Span<const char>, ICUError> Calendar::GetBcp47Type() {
  UErrorCode status = U_ZERO_ERROR;
  const char* oldType = ucal_getType(mCalendar, &status);
  if (U_FAILURE(status)) {
    return Err(ToICUError(status));
  }
  const char* bcp47Type = uloc_toUnicodeLocaleType("calendar", oldType);

  if (!bcp47Type) {
    return Err(ICUError::InternalError);
  }

  return MakeStringSpan(bcp47Type);
}

static Weekday WeekdayFromDaysOfWeek(UCalendarDaysOfWeek weekday) {
  switch (weekday) {
    case UCAL_MONDAY:
      return Weekday::Monday;
    case UCAL_TUESDAY:
      return Weekday::Tuesday;
    case UCAL_WEDNESDAY:
      return Weekday::Wednesday;
    case UCAL_THURSDAY:
      return Weekday::Thursday;
    case UCAL_FRIDAY:
      return Weekday::Friday;
    case UCAL_SATURDAY:
      return Weekday::Saturday;
    case UCAL_SUNDAY:
      return Weekday::Sunday;
  }
  MOZ_CRASH("unexpected weekday value");
}

Result<EnumSet<Weekday>, ICUError> Calendar::GetWeekend() {
  static_assert(static_cast<int32_t>(UCAL_SUNDAY) == 1);
  static_assert(static_cast<int32_t>(UCAL_SATURDAY) == 7);

  UErrorCode status = U_ZERO_ERROR;

  EnumSet<Weekday> weekend;
  for (int32_t i = UCAL_SUNDAY; i <= UCAL_SATURDAY; i++) {
    auto dayOfWeek = static_cast<UCalendarDaysOfWeek>(i);
    auto type = ucal_getDayOfWeekType(mCalendar, dayOfWeek, &status);
    if (U_FAILURE(status)) {
      return Err(ToICUError(status));
    }

    switch (type) {
      case UCAL_WEEKEND_ONSET:
        // Treat days which start as a weekday as weekdays.
        [[fallthrough]];
      case UCAL_WEEKDAY:
        break;

      case UCAL_WEEKEND_CEASE:
        // Treat days which start as a weekend day as weekend days.
        [[fallthrough]];
      case UCAL_WEEKEND:
        weekend += WeekdayFromDaysOfWeek(dayOfWeek);
        break;
    }
  }

  return weekend;
}

Weekday Calendar::GetFirstDayOfWeek() {
  int32_t firstDayOfWeek = ucal_getAttribute(mCalendar, UCAL_FIRST_DAY_OF_WEEK);
  MOZ_ASSERT(UCAL_SUNDAY <= firstDayOfWeek && firstDayOfWeek <= UCAL_SATURDAY);

  return WeekdayFromDaysOfWeek(
      static_cast<UCalendarDaysOfWeek>(firstDayOfWeek));
}

int32_t Calendar::GetMinimalDaysInFirstWeek() {
  int32_t minimalDays =
      ucal_getAttribute(mCalendar, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
  MOZ_ASSERT(1 <= minimalDays && minimalDays <= 7);

  return minimalDays;
}

Result<Ok, ICUError> Calendar::SetTimeInMs(double aUnixEpoch) {
  UErrorCode status = U_ZERO_ERROR;
  ucal_setMillis(mCalendar, aUnixEpoch, &status);
  if (U_FAILURE(status)) {
    return Err(ToICUError(status));
  }
  return Ok{};
}

/* static */
Result<SpanEnumeration<char>, ICUError>
Calendar::GetLegacyKeywordValuesForLocale(const char* aLocale) {
  UErrorCode status = U_ZERO_ERROR;
  UEnumeration* enumeration = ucal_getKeywordValuesForLocale(
      "calendar", aLocale, /* commonlyUsed */ false, &status);

  if (U_SUCCESS(status)) {
    return SpanEnumeration<char>(enumeration);
  }

  return Err(ToICUError(status));
}

/* static */
SpanResult<char> Calendar::LegacyIdentifierToBcp47(const char* aIdentifier,
                                                   int32_t aLength) {
  if (aIdentifier == nullptr) {
    return Err(InternalError{});
  }
  // aLength is not needed here, as the ICU call uses the null terminated
  // string.
  return MakeStringSpan(uloc_toUnicodeLocaleType("ca", aIdentifier));
}

/* static */
Result<Calendar::Bcp47IdentifierEnumeration, ICUError>
Calendar::GetBcp47KeywordValuesForLocale(const char* aLocale,
                                         CommonlyUsed aCommonlyUsed) {
  UErrorCode status = U_ZERO_ERROR;
  UEnumeration* enumeration = ucal_getKeywordValuesForLocale(
      "calendar", aLocale, static_cast<bool>(aCommonlyUsed), &status);

  if (U_SUCCESS(status)) {
    return Bcp47IdentifierEnumeration(enumeration);
  }

  return Err(ToICUError(status));
}

Calendar::~Calendar() {
  MOZ_ASSERT(mCalendar);
  ucal_close(mCalendar);
}

}  // namespace mozilla::intl