| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 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 builtin_temporal_Era_h |
| 8 |
#define builtin_temporal_Era_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/MathAlgorithms.h" |
| 12 |
|
| 13 |
#include <initializer_list> |
| 14 |
#include <stddef.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <string_view> |
| 17 |
|
| 18 |
#include "jstypes.h" |
| 19 |
|
| 20 |
#include "builtin/temporal/Calendar.h" |
| 21 |
#include "builtin/temporal/Crash.h" |
| 22 |
|
| 23 |
namespace js::temporal { |
| 24 |
|
| 25 |
enum class EraCode { |
| 26 |
// The standard era of a calendar. |
| 27 |
Standard, |
| 28 |
|
| 29 |
// The era before the standard era of a calendar. |
| 30 |
Inverse, |
| 31 |
|
| 32 |
// Named Japanese eras. |
| 33 |
Meiji, |
| 34 |
Taisho, |
| 35 |
Showa, |
| 36 |
Heisei, |
| 37 |
Reiwa, |
| 38 |
}; |
| 39 |
|
| 40 |
// static variables in constexpr functions requires C++23 support, so we can't |
| 41 |
// declare the eras directly in CalendarEras. |
| 42 |
namespace eras { |
| 43 |
inline constexpr auto Standard = {EraCode::Standard}; |
| 44 |
|
| 45 |
inline constexpr auto StandardInverse = {EraCode::Standard, EraCode::Inverse}; |
| 46 |
|
| 47 |
inline constexpr auto Japanese = { |
| 48 |
EraCode::Standard, EraCode::Inverse, |
| 49 |
|
| 50 |
EraCode::Meiji, EraCode::Taisho, EraCode::Showa, |
| 51 |
EraCode::Heisei, EraCode::Reiwa, |
| 52 |
}; |
| 53 |
|
| 54 |
// https://tc39.es/proposal-intl-era-monthcode/#table-eras |
| 55 |
// |
| 56 |
// Calendars which don't use eras were omitted. |
| 57 |
namespace names { |
| 58 |
using namespace std::literals; |
| 59 |
|
| 60 |
// Empty placeholder. |
| 61 |
inline constexpr auto Empty = { |
| 62 |
""sv, |
| 63 |
}; |
| 64 |
|
| 65 |
inline constexpr auto Coptic = { |
| 66 |
"coptic"sv, |
| 67 |
}; |
| 68 |
|
| 69 |
inline constexpr auto CopticInverse = { |
| 70 |
"coptic-inverse"sv, |
| 71 |
}; |
| 72 |
|
| 73 |
inline constexpr auto Ethiopian = { |
| 74 |
"ethiopic"sv, |
| 75 |
"incar"sv, |
| 76 |
}; |
| 77 |
|
| 78 |
inline constexpr auto EthiopianInverse = { |
| 79 |
"ethioaa"sv, |
| 80 |
"ethiopic-amete-alem"sv, |
| 81 |
"mundi"sv, |
| 82 |
}; |
| 83 |
|
| 84 |
inline constexpr auto Gregorian = { |
| 85 |
"gregory"sv, |
| 86 |
"ce"sv, |
| 87 |
"ad"sv, |
| 88 |
}; |
| 89 |
|
| 90 |
inline constexpr auto GregorianInverse = { |
| 91 |
"gregory-inverse"sv, |
| 92 |
"bc"sv, |
| 93 |
"bce"sv, |
| 94 |
}; |
| 95 |
|
| 96 |
inline constexpr auto Japanese = { |
| 97 |
"japanese"sv, |
| 98 |
"gregory"sv, |
| 99 |
"ad"sv, |
| 100 |
"ce"sv, |
| 101 |
}; |
| 102 |
|
| 103 |
inline constexpr auto JapaneseInverse = { |
| 104 |
"japanese-inverse"sv, |
| 105 |
"gregory-inverse"sv, |
| 106 |
"bc"sv, |
| 107 |
"bce"sv, |
| 108 |
}; |
| 109 |
|
| 110 |
inline constexpr auto JapaneseMeiji = { |
| 111 |
"meiji"sv, |
| 112 |
}; |
| 113 |
|
| 114 |
inline constexpr auto JapaneseTaisho = { |
| 115 |
"taisho"sv, |
| 116 |
}; |
| 117 |
|
| 118 |
inline constexpr auto JapaneseShowa = { |
| 119 |
"showa"sv, |
| 120 |
}; |
| 121 |
|
| 122 |
inline constexpr auto JapaneseHeisei = { |
| 123 |
"heisei"sv, |
| 124 |
}; |
| 125 |
|
| 126 |
inline constexpr auto JapaneseReiwa = { |
| 127 |
"reiwa"sv, |
| 128 |
}; |
| 129 |
|
| 130 |
inline constexpr auto ROC = { |
| 131 |
"roc"sv, |
| 132 |
"minguo"sv, |
| 133 |
}; |
| 134 |
|
| 135 |
inline constexpr auto ROCInverse = { |
| 136 |
"roc-inverse"sv, |
| 137 |
"before-roc"sv, |
| 138 |
}; |
| 139 |
} // namespace names |
| 140 |
} // namespace eras |
| 141 |
|
| 142 |
constexpr auto& CalendarEras(CalendarId id) { |
| 143 |
switch (id) { |
| 144 |
case CalendarId::ISO8601: |
| 145 |
case CalendarId::Buddhist: |
| 146 |
case CalendarId::Chinese: |
| 147 |
case CalendarId::Dangi: |
| 148 |
case CalendarId::EthiopianAmeteAlem: |
| 149 |
case CalendarId::Hebrew: |
| 150 |
case CalendarId::Indian: |
| 151 |
case CalendarId::Islamic: |
| 152 |
case CalendarId::IslamicCivil: |
| 153 |
case CalendarId::IslamicRGSA: |
| 154 |
case CalendarId::IslamicTabular: |
| 155 |
case CalendarId::IslamicUmmAlQura: |
| 156 |
case CalendarId::Persian: |
| 157 |
return eras::Standard; |
| 158 |
|
| 159 |
case CalendarId::Coptic: |
| 160 |
case CalendarId::Ethiopian: |
| 161 |
case CalendarId::Gregorian: |
| 162 |
case CalendarId::ROC: |
| 163 |
return eras::StandardInverse; |
| 164 |
|
| 165 |
case CalendarId::Japanese: |
| 166 |
return eras::Japanese; |
| 167 |
} |
| 168 |
JS_CONSTEXPR_CRASH("invalid calendar id"); |
| 169 |
} |
| 170 |
|
| 171 |
constexpr bool CalendarEraRelevant(CalendarId calendar) { |
| 172 |
return CalendarEras(calendar).size() > 1; |
| 173 |
} |
| 174 |
|
| 175 |
constexpr auto& CalendarEraNames(CalendarId calendar, EraCode era) { |
| 176 |
switch (calendar) { |
| 177 |
case CalendarId::ISO8601: |
| 178 |
case CalendarId::Buddhist: |
| 179 |
case CalendarId::Chinese: |
| 180 |
case CalendarId::Dangi: |
| 181 |
case CalendarId::EthiopianAmeteAlem: |
| 182 |
case CalendarId::Hebrew: |
| 183 |
case CalendarId::Indian: |
| 184 |
case CalendarId::Islamic: |
| 185 |
case CalendarId::IslamicCivil: |
| 186 |
case CalendarId::IslamicRGSA: |
| 187 |
case CalendarId::IslamicTabular: |
| 188 |
case CalendarId::IslamicUmmAlQura: |
| 189 |
case CalendarId::Persian: |
| 190 |
return eras::names::Empty; |
| 191 |
|
| 192 |
case CalendarId::Coptic: { |
| 193 |
MOZ_ASSERT(era == EraCode::Standard || era == EraCode::Inverse); |
| 194 |
return era == EraCode::Standard ? eras::names::Coptic |
| 195 |
: eras::names::CopticInverse; |
| 196 |
} |
| 197 |
|
| 198 |
case CalendarId::Ethiopian: { |
| 199 |
MOZ_ASSERT(era == EraCode::Standard || era == EraCode::Inverse); |
| 200 |
return era == EraCode::Standard ? eras::names::Ethiopian |
| 201 |
: eras::names::EthiopianInverse; |
| 202 |
} |
| 203 |
|
| 204 |
case CalendarId::Gregorian: { |
| 205 |
MOZ_ASSERT(era == EraCode::Standard || era == EraCode::Inverse); |
| 206 |
return era == EraCode::Standard ? eras::names::Gregorian |
| 207 |
: eras::names::GregorianInverse; |
| 208 |
} |
| 209 |
|
| 210 |
case CalendarId::Japanese: { |
| 211 |
switch (era) { |
| 212 |
case EraCode::Standard: |
| 213 |
return eras::names::Japanese; |
| 214 |
case EraCode::Inverse: |
| 215 |
return eras::names::JapaneseInverse; |
| 216 |
case EraCode::Meiji: |
| 217 |
return eras::names::JapaneseMeiji; |
| 218 |
case EraCode::Taisho: |
| 219 |
return eras::names::JapaneseTaisho; |
| 220 |
case EraCode::Showa: |
| 221 |
return eras::names::JapaneseShowa; |
| 222 |
case EraCode::Heisei: |
| 223 |
return eras::names::JapaneseHeisei; |
| 224 |
case EraCode::Reiwa: |
| 225 |
return eras::names::JapaneseReiwa; |
| 226 |
} |
| 227 |
break; |
| 228 |
} |
| 229 |
|
| 230 |
case CalendarId::ROC: { |
| 231 |
MOZ_ASSERT(era == EraCode::Standard || era == EraCode::Inverse); |
| 232 |
return era == EraCode::Standard ? eras::names::ROC |
| 233 |
: eras::names::ROCInverse; |
| 234 |
} |
| 235 |
} |
| 236 |
JS_CONSTEXPR_CRASH("invalid era"); |
| 237 |
} |
| 238 |
|
| 239 |
constexpr auto CalendarEraName(CalendarId calendar, EraCode era) { |
| 240 |
auto& names = CalendarEraNames(calendar, era); |
| 241 |
MOZ_ASSERT(names.size() > 0); |
| 242 |
return *names.begin(); |
| 243 |
} |
| 244 |
|
| 245 |
constexpr bool CalendarEraStartsAtYearBoundary(CalendarId id) { |
| 246 |
switch (id) { |
| 247 |
// Calendar system which use a single era. |
| 248 |
case CalendarId::ISO8601: |
| 249 |
case CalendarId::Buddhist: |
| 250 |
case CalendarId::Chinese: |
| 251 |
case CalendarId::Dangi: |
| 252 |
case CalendarId::EthiopianAmeteAlem: |
| 253 |
case CalendarId::Hebrew: |
| 254 |
case CalendarId::Indian: |
| 255 |
case CalendarId::Islamic: |
| 256 |
case CalendarId::IslamicCivil: |
| 257 |
case CalendarId::IslamicRGSA: |
| 258 |
case CalendarId::IslamicTabular: |
| 259 |
case CalendarId::IslamicUmmAlQura: |
| 260 |
case CalendarId::Persian: |
| 261 |
return true; |
| 262 |
|
| 263 |
// Calendar system which use multiple eras, but each era starts at a year |
| 264 |
// boundary. |
| 265 |
case CalendarId::Coptic: |
| 266 |
case CalendarId::Ethiopian: |
| 267 |
case CalendarId::Gregorian: |
| 268 |
case CalendarId::ROC: |
| 269 |
return true; |
| 270 |
|
| 271 |
// Calendar system which use multiple eras and eras can start within a year. |
| 272 |
case CalendarId::Japanese: |
| 273 |
return false; |
| 274 |
} |
| 275 |
JS_CONSTEXPR_CRASH("invalid calendar id"); |
| 276 |
} |
| 277 |
|
| 278 |
constexpr bool CalendarEraStartsAtYearBoundary(CalendarId id, EraCode era) { |
| 279 |
MOZ_ASSERT_IF(id != CalendarId::Japanese, |
| 280 |
CalendarEraStartsAtYearBoundary(id)); |
| 281 |
return era == EraCode::Standard || era == EraCode::Inverse; |
| 282 |
} |
| 283 |
|
| 284 |
struct EraYear { |
| 285 |
EraCode era = EraCode::Standard; |
| 286 |
int32_t year = 0; |
| 287 |
}; |
| 288 |
|
| 289 |
constexpr EraYear CalendarEraYear(CalendarId id, int32_t year) { |
| 290 |
if (year > 0 || !CalendarEraRelevant(id)) { |
| 291 |
return EraYear{EraCode::Standard, year}; |
| 292 |
} |
| 293 |
return EraYear{EraCode::Inverse, int32_t(mozilla::Abs(year) + 1)}; |
| 294 |
} |
| 295 |
|
| 296 |
} // namespace js::temporal |
| 297 |
|
| 298 |
#endif /* builtin_temporal_Era_h */ |