| 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 |
|
| 5 |
/* Portions Copyright Norbert Lindenberg 2011-2012. */ |
| 6 |
|
| 7 |
/** |
| 8 |
* 11.1.2 CreateDateTimeFormat ( newTarget, locales, options, required, defaults [ , toLocaleStringTimeZone ] ) |
| 9 |
* |
| 10 |
* Compute an internal properties object from |lazyDateTimeFormatData|. |
| 11 |
*/ |
| 12 |
function resolveDateTimeFormatInternals(lazyDateTimeFormatData) { |
| 13 |
assert(IsObject(lazyDateTimeFormatData), "lazy data not an object?"); |
| 14 |
|
| 15 |
// Lazy DateTimeFormat data has the following structure: |
| 16 |
// |
| 17 |
// { |
| 18 |
// requestedLocales: List of locales, |
| 19 |
// |
| 20 |
// localeOpt: // *first* opt computed in InitializeDateTimeFormat |
| 21 |
// { |
| 22 |
// localeMatcher: "lookup" / "best fit", |
| 23 |
// |
| 24 |
// ca: string matching a Unicode extension type, // optional |
| 25 |
// |
| 26 |
// nu: string matching a Unicode extension type, // optional |
| 27 |
// |
| 28 |
// hc: "h11" / "h12" / "h23" / "h24", // optional |
| 29 |
// } |
| 30 |
// |
| 31 |
// timeZone: IANA time zone name or a normalized time zone offset string, |
| 32 |
// |
| 33 |
// formatOptions: // *second* opt computed in InitializeDateTimeFormat |
| 34 |
// { |
| 35 |
// // all the properties/values listed in Table 3 |
| 36 |
// // (weekday, era, year, month, day, &c.) |
| 37 |
// |
| 38 |
// hour12: true / false, // optional |
| 39 |
// } |
| 40 |
// |
| 41 |
// formatMatcher: "basic" / "best fit", |
| 42 |
// |
| 43 |
// dateStyle: "full" / "long" / "medium" / "short" / undefined, |
| 44 |
// |
| 45 |
// timeStyle: "full" / "long" / "medium" / "short" / undefined, |
| 46 |
// |
| 47 |
// patternOption: |
| 48 |
// String representing LDML Date Format pattern or undefined |
| 49 |
// } |
| 50 |
// |
| 51 |
// Note that lazy data is only installed as a final step of initialization, |
| 52 |
// so every DateTimeFormat lazy data object has *all* these properties, |
| 53 |
// never a subset of them. |
| 54 |
|
| 55 |
var internalProps = std_Object_create(null); |
| 56 |
|
| 57 |
var DateTimeFormat = dateTimeFormatInternalProperties; |
| 58 |
|
| 59 |
// Compute effective locale. |
| 60 |
|
| 61 |
// Step 17. |
| 62 |
var localeData = DateTimeFormat.localeData; |
| 63 |
|
| 64 |
// Step 18. |
| 65 |
var r = ResolveLocale( |
| 66 |
"DateTimeFormat", |
| 67 |
lazyDateTimeFormatData.requestedLocales, |
| 68 |
lazyDateTimeFormatData.localeOpt, |
| 69 |
DateTimeFormat.relevantExtensionKeys, |
| 70 |
localeData |
| 71 |
); |
| 72 |
|
| 73 |
// Steps 19-22. |
| 74 |
internalProps.locale = r.locale; |
| 75 |
internalProps.calendar = r.ca; |
| 76 |
internalProps.numberingSystem = r.nu; |
| 77 |
|
| 78 |
// Step 34. (Reordered) |
| 79 |
var formatOptions = lazyDateTimeFormatData.formatOptions; |
| 80 |
|
| 81 |
// Steps 23-29. |
| 82 |
// |
| 83 |
// Copy the hourCycle setting, if present, to the format options. But |
| 84 |
// only do this if no hour12 option is present, because the latter takes |
| 85 |
// precedence over hourCycle. |
| 86 |
if (r.hc !== null && formatOptions.hour12 === undefined) { |
| 87 |
formatOptions.hourCycle = r.hc; |
| 88 |
} |
| 89 |
|
| 90 |
// Step 33. |
| 91 |
internalProps.timeZone = lazyDateTimeFormatData.timeZone; |
| 92 |
|
| 93 |
// Steps 45-50, more or less. |
| 94 |
if (lazyDateTimeFormatData.patternOption !== undefined) { |
| 95 |
internalProps.pattern = lazyDateTimeFormatData.patternOption; |
| 96 |
} else if ( |
| 97 |
lazyDateTimeFormatData.dateStyle !== undefined || |
| 98 |
lazyDateTimeFormatData.timeStyle !== undefined |
| 99 |
) { |
| 100 |
internalProps.hourCycle = formatOptions.hourCycle; |
| 101 |
internalProps.hour12 = formatOptions.hour12; |
| 102 |
internalProps.dateStyle = lazyDateTimeFormatData.dateStyle; |
| 103 |
internalProps.timeStyle = lazyDateTimeFormatData.timeStyle; |
| 104 |
} else { |
| 105 |
internalProps.required = lazyDateTimeFormatData.required; |
| 106 |
internalProps.defaults = lazyDateTimeFormatData.defaults; |
| 107 |
internalProps.hourCycle = formatOptions.hourCycle; |
| 108 |
internalProps.hour12 = formatOptions.hour12; |
| 109 |
internalProps.weekday = formatOptions.weekday; |
| 110 |
internalProps.era = formatOptions.era; |
| 111 |
internalProps.year = formatOptions.year; |
| 112 |
internalProps.month = formatOptions.month; |
| 113 |
internalProps.day = formatOptions.day; |
| 114 |
internalProps.dayPeriod = formatOptions.dayPeriod; |
| 115 |
internalProps.hour = formatOptions.hour; |
| 116 |
internalProps.minute = formatOptions.minute; |
| 117 |
internalProps.second = formatOptions.second; |
| 118 |
internalProps.fractionalSecondDigits = formatOptions.fractionalSecondDigits; |
| 119 |
internalProps.timeZoneName = formatOptions.timeZoneName; |
| 120 |
} |
| 121 |
|
| 122 |
// The caller is responsible for associating |internalProps| with the right |
| 123 |
// object using |setInternalProperties|. |
| 124 |
return internalProps; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns an object containing the DateTimeFormat internal properties of |obj|. |
| 129 |
*/ |
| 130 |
function getDateTimeFormatInternals(obj) { |
| 131 |
assert(IsObject(obj), "getDateTimeFormatInternals called with non-object"); |
| 132 |
assert( |
| 133 |
intl_GuardToDateTimeFormat(obj) !== null, |
| 134 |
"getDateTimeFormatInternals called with non-DateTimeFormat" |
| 135 |
); |
| 136 |
|
| 137 |
var internals = getIntlObjectInternals(obj); |
| 138 |
assert( |
| 139 |
internals.type === "DateTimeFormat", |
| 140 |
"bad type escaped getIntlObjectInternals" |
| 141 |
); |
| 142 |
|
| 143 |
// If internal properties have already been computed, use them. |
| 144 |
var internalProps = maybeInternalProperties(internals); |
| 145 |
if (internalProps) { |
| 146 |
return internalProps; |
| 147 |
} |
| 148 |
|
| 149 |
// Otherwise it's time to fully create them. |
| 150 |
internalProps = resolveDateTimeFormatInternals(internals.lazyData); |
| 151 |
setInternalProperties(internals, internalProps); |
| 152 |
return internalProps; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* 12.1.10 UnwrapDateTimeFormat( dtf ) |
| 157 |
*/ |
| 158 |
function UnwrapDateTimeFormat(dtf) { |
| 159 |
// Steps 2 and 4 (error handling moved to caller). |
| 160 |
if ( |
| 161 |
IsObject(dtf) && |
| 162 |
intl_GuardToDateTimeFormat(dtf) === null && |
| 163 |
!intl_IsWrappedDateTimeFormat(dtf) && |
| 164 |
callFunction( |
| 165 |
std_Object_isPrototypeOf, |
| 166 |
GetBuiltinPrototype("DateTimeFormat"), |
| 167 |
dtf |
| 168 |
) |
| 169 |
) { |
| 170 |
dtf = dtf[intlFallbackSymbol()]; |
| 171 |
} |
| 172 |
return dtf; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* 6.4.2 CanonicalizeTimeZoneName ( timeZone ) |
| 177 |
* |
| 178 |
* Canonicalizes the given IANA time zone name. |
| 179 |
* |
| 180 |
* ES2017 Intl draft rev 4a23f407336d382ed5e3471200c690c9b020b5f3 |
| 181 |
*/ |
| 182 |
function CanonicalizeTimeZoneName(timeZone) { |
| 183 |
assert(typeof timeZone === "string", "CanonicalizeTimeZoneName"); |
| 184 |
|
| 185 |
// Step 1. (Not applicable, the input is already a valid IANA time zone.) |
| 186 |
assert(timeZone !== "Etc/Unknown", "Invalid time zone"); |
| 187 |
assert( |
| 188 |
timeZone === intl_IsValidTimeZoneName(timeZone), |
| 189 |
"Time zone name not normalized" |
| 190 |
); |
| 191 |
|
| 192 |
// Step 2. |
| 193 |
var ianaTimeZone = intl_canonicalizeTimeZone(timeZone); |
| 194 |
assert(ianaTimeZone !== "Etc/Unknown", "Invalid canonical time zone"); |
| 195 |
assert( |
| 196 |
ianaTimeZone === intl_IsValidTimeZoneName(ianaTimeZone), |
| 197 |
"Unsupported canonical time zone" |
| 198 |
); |
| 199 |
|
| 200 |
// Step 3. (Not applicable.) |
| 201 |
assert(ianaTimeZone !== "Etc/UTC", "Invalid link to UTC"); |
| 202 |
assert(ianaTimeZone !== "Etc/GMT", "Invalid link to UTC"); |
| 203 |
assert(ianaTimeZone !== "GMT", "Invalid link to UTC"); |
| 204 |
|
| 205 |
// Step 4. |
| 206 |
return ianaTimeZone; |
| 207 |
} |
| 208 |
|
| 209 |
var timeZoneCache = { |
| 210 |
icuDefaultTimeZone: undefined, |
| 211 |
defaultTimeZone: undefined, |
| 212 |
}; |
| 213 |
|
| 214 |
/** |
| 215 |
* 6.4.3 DefaultTimeZone () |
| 216 |
* |
| 217 |
* Returns the IANA time zone name for the host environment's current time zone. |
| 218 |
* |
| 219 |
* ES2017 Intl draft rev 4a23f407336d382ed5e3471200c690c9b020b5f3 |
| 220 |
*/ |
| 221 |
function DefaultTimeZone() { |
| 222 |
if (intl_isDefaultTimeZone(timeZoneCache.icuDefaultTimeZone)) { |
| 223 |
return timeZoneCache.defaultTimeZone; |
| 224 |
} |
| 225 |
|
| 226 |
// Verify that the current ICU time zone is a valid ECMA-402 time zone. |
| 227 |
var icuDefaultTimeZone = intl_defaultTimeZone(); |
| 228 |
var timeZone = intl_IsValidTimeZoneName(icuDefaultTimeZone); |
| 229 |
if (timeZone === null) { |
| 230 |
// Before defaulting to "UTC", try to represent the default time zone |
| 231 |
// using the Etc/GMT + offset format. This format only accepts full |
| 232 |
// hour offsets. |
| 233 |
var msPerHour = 60 * 60 * 1000; |
| 234 |
var offset = intl_defaultTimeZoneOffset(); |
| 235 |
assert( |
| 236 |
offset === (offset | 0), |
| 237 |
"milliseconds offset shouldn't be able to exceed int32_t range" |
| 238 |
); |
| 239 |
var offsetHours = offset / msPerHour; |
| 240 |
var offsetHoursFraction = offset % msPerHour; |
| 241 |
if (offsetHoursFraction === 0) { |
| 242 |
// Etc/GMT + offset uses POSIX-style signs, i.e. a positive offset |
| 243 |
// means a location west of GMT. |
| 244 |
timeZone = |
| 245 |
"Etc/GMT" + (offsetHours < 0 ? "+" : "-") + std_Math_abs(offsetHours); |
| 246 |
|
| 247 |
// Check if the fallback is valid. |
| 248 |
timeZone = intl_IsValidTimeZoneName(timeZone); |
| 249 |
} |
| 250 |
|
| 251 |
// Fallback to "UTC" if everything else fails. |
| 252 |
if (timeZone === null) { |
| 253 |
timeZone = "UTC"; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
// Canonicalize the ICU time zone, e.g. change Etc/UTC to UTC. |
| 258 |
var defaultTimeZone = CanonicalizeTimeZoneName(timeZone); |
| 259 |
|
| 260 |
timeZoneCache.defaultTimeZone = defaultTimeZone; |
| 261 |
timeZoneCache.icuDefaultTimeZone = icuDefaultTimeZone; |
| 262 |
|
| 263 |
return defaultTimeZone; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* 21.4.1.33.1 IsTimeZoneOffsetString ( offsetString ) |
| 268 |
* 21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString ) |
| 269 |
* 11.1.3 FormatOffsetTimeZoneIdentifier ( offsetMinutes ) |
| 270 |
* |
| 271 |
* Function to parse, validate, and normalize time zone offset strings. |
| 272 |
* |
| 273 |
* ES2024 draft rev 10d44bfce4640894a0ed366bb769f2700cc8839a |
| 274 |
* ES2024 Intl draft rev 2f002b2000bf8b908efb793767bcfd23620e06db |
| 275 |
*/ |
| 276 |
function TimeZoneOffsetString(offsetString) { |
| 277 |
assert(typeof(offsetString) === "string", "offsetString is a string"); |
| 278 |
|
| 279 |
// UTCOffset ::: |
| 280 |
// ASCIISign Hour |
| 281 |
// ASCIISign Hour HourSubcomponents[+Extended] |
| 282 |
// ASCIISign Hour HourSubcomponents[~Extended] |
| 283 |
// |
| 284 |
// ASCIISign ::: one of |
| 285 |
// + - |
| 286 |
// |
| 287 |
// Hour ::: |
| 288 |
// 0 DecimalDigit |
| 289 |
// 1 DecimalDigit |
| 290 |
// 20 |
| 291 |
// 21 |
| 292 |
// 22 |
| 293 |
// 23 |
| 294 |
// |
| 295 |
// HourSubcomponents[Extended] ::: |
| 296 |
// TimeSeparator[?Extended] MinuteSecond |
| 297 |
// |
| 298 |
// TimeSeparator[Extended] ::: |
| 299 |
// [+Extended] : |
| 300 |
// [~Extended] [empty] |
| 301 |
// |
| 302 |
// MinuteSecond ::: |
| 303 |
// 0 DecimalDigit |
| 304 |
// 1 DecimalDigit |
| 305 |
// 2 DecimalDigit |
| 306 |
// 3 DecimalDigit |
| 307 |
// 4 DecimalDigit |
| 308 |
// 5 DecimalDigit |
| 309 |
|
| 310 |
// Return if there are too few or too many characters for an offset string. |
| 311 |
if (offsetString.length < 3 || offsetString.length > 6) { |
| 312 |
return null; |
| 313 |
} |
| 314 |
|
| 315 |
// The first character must match |ASCIISign|. |
| 316 |
var sign = offsetString[0]; |
| 317 |
if (sign !== "+" && sign !== "-") { |
| 318 |
return null; |
| 319 |
} |
| 320 |
|
| 321 |
// Read the next two characters for the |Hour| grammar production. |
| 322 |
var hourTens = offsetString[1]; |
| 323 |
var hourOnes = offsetString[2]; |
| 324 |
|
| 325 |
// Read the remaining characters for the optional |MinuteSecond| grammar production. |
| 326 |
var minutesTens = "0"; |
| 327 |
var minutesOnes = "0"; |
| 328 |
if (offsetString.length > 3) { |
| 329 |
// |TimeSeparator| is optional. |
| 330 |
var separatorLength = offsetString[3] === ":" ? 1 : 0; |
| 331 |
|
| 332 |
// Return if there are too many characters for an offset string. |
| 333 |
if (offsetString.length !== (5 + separatorLength)) { |
| 334 |
return null; |
| 335 |
} |
| 336 |
|
| 337 |
minutesTens = offsetString[3 + separatorLength]; |
| 338 |
minutesOnes = offsetString[4 + separatorLength]; |
| 339 |
} |
| 340 |
|
| 341 |
// Validate the characters match the |Hour| and |MinuteSecond| productions: |
| 342 |
// - hours must be in the range 0..23 |
| 343 |
// - minutes must in the range 0..59 |
| 344 |
if ( |
| 345 |
hourTens < "0" || |
| 346 |
hourOnes < "0" || |
| 347 |
minutesTens < "0" || |
| 348 |
minutesOnes < "0" || |
| 349 |
hourTens > "2" || |
| 350 |
hourOnes > "9" || |
| 351 |
minutesTens > "5" || |
| 352 |
minutesOnes > "9" || |
| 353 |
(hourTens === "2" && hourOnes > "3") |
| 354 |
) { |
| 355 |
return null; |
| 356 |
} |
| 357 |
|
| 358 |
// FormatOffsetTimeZoneIdentifier, steps 1-5. |
| 359 |
if ( |
| 360 |
hourTens === "0" && |
| 361 |
hourOnes === "0" && |
| 362 |
minutesTens === "0" && |
| 363 |
minutesOnes === "0" |
| 364 |
) { |
| 365 |
sign = "+"; |
| 366 |
} |
| 367 |
|
| 368 |
return sign + hourTens + hourOnes + ":" + minutesTens + minutesOnes; |
| 369 |
} |
| 370 |
|
| 371 |
/* eslint-disable complexity */ |
| 372 |
/** |
| 373 |
* 11.1.2 CreateDateTimeFormat ( newTarget, locales, options, required, defaults [ , toLocaleStringTimeZone ] ) |
| 374 |
* |
| 375 |
* Initializes an object as a DateTimeFormat. |
| 376 |
* |
| 377 |
* This method is complicated a moderate bit by its implementing initialization |
| 378 |
* as a *lazy* concept. Everything that must happen now, does -- but we defer |
| 379 |
* all the work we can until the object is actually used as a DateTimeFormat. |
| 380 |
* This later work occurs in |resolveDateTimeFormatInternals|; steps not noted |
| 381 |
* here occur there. |
| 382 |
*/ |
| 383 |
function InitializeDateTimeFormat( |
| 384 |
dateTimeFormat, |
| 385 |
thisValue, |
| 386 |
locales, |
| 387 |
options, |
| 388 |
required, |
| 389 |
defaults, |
| 390 |
toLocaleStringTimeZone, |
| 391 |
mozExtensions |
| 392 |
) { |
| 393 |
assert( |
| 394 |
IsObject(dateTimeFormat), |
| 395 |
"InitializeDateTimeFormat called with non-Object" |
| 396 |
); |
| 397 |
assert( |
| 398 |
intl_GuardToDateTimeFormat(dateTimeFormat) !== null, |
| 399 |
"InitializeDateTimeFormat called with non-DateTimeFormat" |
| 400 |
); |
| 401 |
assert( |
| 402 |
required === "date" || required === "time" || required === "any", |
| 403 |
`InitializeDateTimeFormat called with invalid required value: ${required}` |
| 404 |
); |
| 405 |
assert( |
| 406 |
defaults === "date" || defaults === "time" || defaults === "all", |
| 407 |
`InitializeDateTimeFormat called with invalid defaults value: ${defaults}` |
| 408 |
); |
| 409 |
assert( |
| 410 |
toLocaleStringTimeZone === undefined || typeof toLocaleStringTimeZone === "string", |
| 411 |
`InitializeDateTimeFormat called with invalid toLocaleStringTimeZone value: ${toLocaleStringTimeZone}` |
| 412 |
); |
| 413 |
|
| 414 |
// Lazy DateTimeFormat data has the following structure: |
| 415 |
// |
| 416 |
// { |
| 417 |
// requestedLocales: List of locales, |
| 418 |
// |
| 419 |
// localeOpt: // *first* opt computed in InitializeDateTimeFormat |
| 420 |
// { |
| 421 |
// localeMatcher: "lookup" / "best fit", |
| 422 |
// |
| 423 |
// ca: string matching a Unicode extension type, // optional |
| 424 |
// |
| 425 |
// nu: string matching a Unicode extension type, // optional |
| 426 |
// |
| 427 |
// hc: "h11" / "h12" / "h23" / "h24", // optional |
| 428 |
// } |
| 429 |
// |
| 430 |
// timeZone: IANA time zone name or a normalized time zone offset string, |
| 431 |
// |
| 432 |
// formatOptions: // *second* opt computed in InitializeDateTimeFormat |
| 433 |
// { |
| 434 |
// // all the properties/values listed in Table 3 |
| 435 |
// // (weekday, era, year, month, day, &c.) |
| 436 |
// |
| 437 |
// hour12: true / false, // optional |
| 438 |
// } |
| 439 |
// |
| 440 |
// formatMatcher: "basic" / "best fit", |
| 441 |
// |
| 442 |
// required: "date" / "time" / "any", // optional |
| 443 |
// |
| 444 |
// defaults: "date" / "time" / "all", // optional |
| 445 |
// } |
| 446 |
// |
| 447 |
// Note that lazy data is only installed as a final step of initialization, |
| 448 |
// so every DateTimeFormat lazy data object has *all* these properties, |
| 449 |
// never a subset of them. |
| 450 |
var lazyDateTimeFormatData = std_Object_create(null); |
| 451 |
|
| 452 |
// Step 1. (Performed in caller) |
| 453 |
|
| 454 |
// Step 2. |
| 455 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 456 |
lazyDateTimeFormatData.requestedLocales = requestedLocales; |
| 457 |
|
| 458 |
// Step 3. (Inlined call to CoerceOptionsToObject.) |
| 459 |
if (options === undefined) { |
| 460 |
options = std_Object_create(null); |
| 461 |
} else { |
| 462 |
options = ToObject(options); |
| 463 |
} |
| 464 |
|
| 465 |
// Compute options that impact interpretation of locale. |
| 466 |
// Step 4. |
| 467 |
var localeOpt = new_Record(); |
| 468 |
lazyDateTimeFormatData.localeOpt = localeOpt; |
| 469 |
|
| 470 |
// Steps 5-6. |
| 471 |
var localeMatcher = GetOption( |
| 472 |
options, |
| 473 |
"localeMatcher", |
| 474 |
"string", |
| 475 |
["lookup", "best fit"], |
| 476 |
"best fit" |
| 477 |
); |
| 478 |
localeOpt.localeMatcher = localeMatcher; |
| 479 |
|
| 480 |
// Step 7. |
| 481 |
var calendar = GetOption(options, "calendar", "string", undefined, undefined); |
| 482 |
|
| 483 |
// Step 8. |
| 484 |
if (calendar !== undefined) { |
| 485 |
calendar = intl_ValidateAndCanonicalizeUnicodeExtensionType( |
| 486 |
calendar, |
| 487 |
"calendar", |
| 488 |
"ca" |
| 489 |
); |
| 490 |
} |
| 491 |
|
| 492 |
// Step 9. |
| 493 |
localeOpt.ca = calendar; |
| 494 |
|
| 495 |
// Step 10. |
| 496 |
var numberingSystem = GetOption( |
| 497 |
options, |
| 498 |
"numberingSystem", |
| 499 |
"string", |
| 500 |
undefined, |
| 501 |
undefined |
| 502 |
); |
| 503 |
|
| 504 |
// Step 11. |
| 505 |
if (numberingSystem !== undefined) { |
| 506 |
numberingSystem = intl_ValidateAndCanonicalizeUnicodeExtensionType( |
| 507 |
numberingSystem, |
| 508 |
"numberingSystem", |
| 509 |
"nu" |
| 510 |
); |
| 511 |
} |
| 512 |
|
| 513 |
// Step 12. |
| 514 |
localeOpt.nu = numberingSystem; |
| 515 |
|
| 516 |
// Step 13. |
| 517 |
var hour12 = GetOption(options, "hour12", "boolean", undefined, undefined); |
| 518 |
|
| 519 |
// Step 14. |
| 520 |
var hourCycle = GetOption( |
| 521 |
options, |
| 522 |
"hourCycle", |
| 523 |
"string", |
| 524 |
["h11", "h12", "h23", "h24"], |
| 525 |
undefined |
| 526 |
); |
| 527 |
|
| 528 |
// Step 15. |
| 529 |
if (hour12 !== undefined) { |
| 530 |
// The "hourCycle" option is ignored if "hr12" is also present. |
| 531 |
hourCycle = null; |
| 532 |
} |
| 533 |
|
| 534 |
// Step 16. |
| 535 |
localeOpt.hc = hourCycle; |
| 536 |
|
| 537 |
// Steps 17-29 (see resolveDateTimeFormatInternals). |
| 538 |
|
| 539 |
// Step 29. |
| 540 |
var timeZone = options.timeZone; |
| 541 |
|
| 542 |
// Steps 30-34. |
| 543 |
if (timeZone === undefined) { |
| 544 |
// Step 30.a. |
| 545 |
if (toLocaleStringTimeZone !== undefined) { |
| 546 |
timeZone = toLocaleStringTimeZone; |
| 547 |
} else { |
| 548 |
timeZone = DefaultTimeZone(); |
| 549 |
} |
| 550 |
|
| 551 |
// Steps 32-34. (Not applicable in our implementation.) |
| 552 |
} else { |
| 553 |
// Step 31.a. |
| 554 |
if (toLocaleStringTimeZone !== undefined) { |
| 555 |
ThrowTypeError( |
| 556 |
JSMSG_INVALID_DATETIME_OPTION, |
| 557 |
"timeZone", |
| 558 |
"Temporal.ZonedDateTime.toLocaleString" |
| 559 |
); |
| 560 |
} |
| 561 |
timeZone = ToString(timeZone); |
| 562 |
|
| 563 |
// Steps 32-34. |
| 564 |
var offsetString = TimeZoneOffsetString(timeZone); |
| 565 |
if (offsetString !== null) { |
| 566 |
// Steps 32.a-g. (Performed in TimeZoneOffsetString in our implementation.) |
| 567 |
timeZone = offsetString; |
| 568 |
} else { |
| 569 |
// Steps 33-34. |
| 570 |
var validTimeZone = intl_IsValidTimeZoneName(timeZone); |
| 571 |
if (validTimeZone !== null) { |
| 572 |
// Step 33.a. |
| 573 |
timeZone = CanonicalizeTimeZoneName(validTimeZone); |
| 574 |
} else { |
| 575 |
// Step 34.a. |
| 576 |
ThrowRangeError(JSMSG_INVALID_TIME_ZONE, timeZone); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
// Step 33. |
| 582 |
lazyDateTimeFormatData.timeZone = timeZone; |
| 583 |
|
| 584 |
// Step 34. |
| 585 |
var formatOptions = new_Record(); |
| 586 |
lazyDateTimeFormatData.formatOptions = formatOptions; |
| 587 |
|
| 588 |
if (mozExtensions) { |
| 589 |
var pattern = GetOption(options, "pattern", "string", undefined, undefined); |
| 590 |
lazyDateTimeFormatData.patternOption = pattern; |
| 591 |
} |
| 592 |
|
| 593 |
// Step 35. |
| 594 |
// |
| 595 |
// Pass hr12 on to ICU. The hour cycle option is passed through |localeOpt|. |
| 596 |
if (hour12 !== undefined) { |
| 597 |
formatOptions.hour12 = hour12; |
| 598 |
} |
| 599 |
|
| 600 |
// Step 36. (Explicit format component computed in step 43.) |
| 601 |
|
| 602 |
// Step 37. |
| 603 |
// 11.5, Table 7: Components of date and time formats. |
| 604 |
formatOptions.weekday = GetOption( |
| 605 |
options, |
| 606 |
"weekday", |
| 607 |
"string", |
| 608 |
["narrow", "short", "long"], |
| 609 |
undefined |
| 610 |
); |
| 611 |
formatOptions.era = GetOption( |
| 612 |
options, |
| 613 |
"era", |
| 614 |
"string", |
| 615 |
["narrow", "short", "long"], |
| 616 |
undefined |
| 617 |
); |
| 618 |
formatOptions.year = GetOption( |
| 619 |
options, |
| 620 |
"year", |
| 621 |
"string", |
| 622 |
["2-digit", "numeric"], |
| 623 |
undefined |
| 624 |
); |
| 625 |
formatOptions.month = GetOption( |
| 626 |
options, |
| 627 |
"month", |
| 628 |
"string", |
| 629 |
["2-digit", "numeric", "narrow", "short", "long"], |
| 630 |
undefined |
| 631 |
); |
| 632 |
formatOptions.day = GetOption( |
| 633 |
options, |
| 634 |
"day", |
| 635 |
"string", |
| 636 |
["2-digit", "numeric"], |
| 637 |
undefined |
| 638 |
); |
| 639 |
formatOptions.dayPeriod = GetOption( |
| 640 |
options, |
| 641 |
"dayPeriod", |
| 642 |
"string", |
| 643 |
["narrow", "short", "long"], |
| 644 |
undefined |
| 645 |
); |
| 646 |
formatOptions.hour = GetOption( |
| 647 |
options, |
| 648 |
"hour", |
| 649 |
"string", |
| 650 |
["2-digit", "numeric"], |
| 651 |
undefined |
| 652 |
); |
| 653 |
formatOptions.minute = GetOption( |
| 654 |
options, |
| 655 |
"minute", |
| 656 |
"string", |
| 657 |
["2-digit", "numeric"], |
| 658 |
undefined |
| 659 |
); |
| 660 |
formatOptions.second = GetOption( |
| 661 |
options, |
| 662 |
"second", |
| 663 |
"string", |
| 664 |
["2-digit", "numeric"], |
| 665 |
undefined |
| 666 |
); |
| 667 |
formatOptions.fractionalSecondDigits = GetNumberOption( |
| 668 |
options, |
| 669 |
"fractionalSecondDigits", |
| 670 |
1, |
| 671 |
3, |
| 672 |
undefined |
| 673 |
); |
| 674 |
formatOptions.timeZoneName = GetOption( |
| 675 |
options, |
| 676 |
"timeZoneName", |
| 677 |
"string", |
| 678 |
[ |
| 679 |
"short", |
| 680 |
"long", |
| 681 |
"shortOffset", |
| 682 |
"longOffset", |
| 683 |
"shortGeneric", |
| 684 |
"longGeneric", |
| 685 |
], |
| 686 |
undefined |
| 687 |
); |
| 688 |
|
| 689 |
// Step 38. |
| 690 |
// |
| 691 |
// For some reason (ICU not exposing enough interface?) we drop the |
| 692 |
// requested format matcher on the floor after this. In any case, even if |
| 693 |
// doing so is justified, we have to do this work here in case it triggers |
| 694 |
// getters or similar. (bug 852837) |
| 695 |
var formatMatcher = GetOption( |
| 696 |
options, |
| 697 |
"formatMatcher", |
| 698 |
"string", |
| 699 |
["basic", "best fit"], |
| 700 |
"best fit" |
| 701 |
); |
| 702 |
void formatMatcher; |
| 703 |
|
| 704 |
// Steps 39-40. |
| 705 |
var dateStyle = GetOption( |
| 706 |
options, |
| 707 |
"dateStyle", |
| 708 |
"string", |
| 709 |
["full", "long", "medium", "short"], |
| 710 |
undefined |
| 711 |
); |
| 712 |
lazyDateTimeFormatData.dateStyle = dateStyle; |
| 713 |
|
| 714 |
// Steps 41-42. |
| 715 |
var timeStyle = GetOption( |
| 716 |
options, |
| 717 |
"timeStyle", |
| 718 |
"string", |
| 719 |
["full", "long", "medium", "short"], |
| 720 |
undefined |
| 721 |
); |
| 722 |
lazyDateTimeFormatData.timeStyle = timeStyle; |
| 723 |
|
| 724 |
// Step 43. |
| 725 |
if (dateStyle !== undefined || timeStyle !== undefined) { |
| 726 |
/* eslint-disable no-nested-ternary */ |
| 727 |
var explicitFormatComponent = |
| 728 |
formatOptions.weekday !== undefined |
| 729 |
? "weekday" |
| 730 |
: formatOptions.era !== undefined |
| 731 |
? "era" |
| 732 |
: formatOptions.year !== undefined |
| 733 |
? "year" |
| 734 |
: formatOptions.month !== undefined |
| 735 |
? "month" |
| 736 |
: formatOptions.day !== undefined |
| 737 |
? "day" |
| 738 |
: formatOptions.dayPeriod !== undefined |
| 739 |
? "dayPeriod" |
| 740 |
: formatOptions.hour !== undefined |
| 741 |
? "hour" |
| 742 |
: formatOptions.minute !== undefined |
| 743 |
? "minute" |
| 744 |
: formatOptions.second !== undefined |
| 745 |
? "second" |
| 746 |
: formatOptions.fractionalSecondDigits !== undefined |
| 747 |
? "fractionalSecondDigits" |
| 748 |
: formatOptions.timeZoneName !== undefined |
| 749 |
? "timeZoneName" |
| 750 |
: undefined; |
| 751 |
/* eslint-enable no-nested-ternary */ |
| 752 |
|
| 753 |
// Step 43.a. |
| 754 |
if (explicitFormatComponent !== undefined) { |
| 755 |
ThrowTypeError( |
| 756 |
JSMSG_INVALID_DATETIME_OPTION, |
| 757 |
explicitFormatComponent, |
| 758 |
dateStyle !== undefined ? "dateStyle" : "timeStyle" |
| 759 |
); |
| 760 |
} |
| 761 |
|
| 762 |
// Step 43.b. |
| 763 |
if (required === "date" && timeStyle !== undefined) { |
| 764 |
ThrowTypeError(JSMSG_INVALID_DATETIME_STYLE, "timeStyle", "date"); |
| 765 |
} |
| 766 |
|
| 767 |
// Step 43.c. |
| 768 |
if (required === "time" && dateStyle !== undefined) { |
| 769 |
ThrowTypeError(JSMSG_INVALID_DATETIME_STYLE, "dateStyle", "time"); |
| 770 |
} |
| 771 |
} else { |
| 772 |
lazyDateTimeFormatData.required = required; |
| 773 |
lazyDateTimeFormatData.defaults = defaults; |
| 774 |
|
| 775 |
// Steps 44.f-h provided by ICU, more or less. |
| 776 |
} |
| 777 |
|
| 778 |
// Steps 45-50. (see resolveDateTimeFormatInternals). |
| 779 |
|
| 780 |
// We've done everything that must be done now: mark the lazy data as fully |
| 781 |
// computed and install it. |
| 782 |
initializeIntlObject( |
| 783 |
dateTimeFormat, |
| 784 |
"DateTimeFormat", |
| 785 |
lazyDateTimeFormatData |
| 786 |
); |
| 787 |
|
| 788 |
// 11.1.1 Intl.DateTimeFormat, step 3. (Inlined call to ChainDateTimeFormat.) |
| 789 |
if ( |
| 790 |
dateTimeFormat !== thisValue && |
| 791 |
callFunction( |
| 792 |
std_Object_isPrototypeOf, |
| 793 |
GetBuiltinPrototype("DateTimeFormat"), |
| 794 |
thisValue |
| 795 |
) |
| 796 |
) { |
| 797 |
DefineDataProperty( |
| 798 |
thisValue, |
| 799 |
intlFallbackSymbol(), |
| 800 |
dateTimeFormat, |
| 801 |
ATTR_NONENUMERABLE | ATTR_NONCONFIGURABLE | ATTR_NONWRITABLE |
| 802 |
); |
| 803 |
|
| 804 |
return thisValue; |
| 805 |
} |
| 806 |
|
| 807 |
// Step 51. |
| 808 |
return dateTimeFormat; |
| 809 |
} |
| 810 |
/* eslint-enable complexity */ |
| 811 |
|
| 812 |
/** |
| 813 |
* Returns the subset of the given locale list for which this locale list has a |
| 814 |
* matching (possibly fallback) locale. Locales appear in the same order in the |
| 815 |
* returned list as in the input list. |
| 816 |
* |
| 817 |
* Spec: ECMAScript Internationalization API Specification, 12.3.2. |
| 818 |
*/ |
| 819 |
function Intl_DateTimeFormat_supportedLocalesOf(locales /*, options*/) { |
| 820 |
var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined; |
| 821 |
|
| 822 |
// Step 1. |
| 823 |
var availableLocales = "DateTimeFormat"; |
| 824 |
|
| 825 |
// Step 2. |
| 826 |
var requestedLocales = CanonicalizeLocaleList(locales); |
| 827 |
|
| 828 |
// Step 3. |
| 829 |
return SupportedLocales(availableLocales, requestedLocales, options); |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* DateTimeFormat internal properties. |
| 834 |
* |
| 835 |
* Spec: ECMAScript Internationalization API Specification, 9.1 and 12.3.3. |
| 836 |
*/ |
| 837 |
var dateTimeFormatInternalProperties = { |
| 838 |
localeData: dateTimeFormatLocaleData, |
| 839 |
relevantExtensionKeys: ["ca", "hc", "nu"], |
| 840 |
}; |
| 841 |
|
| 842 |
function dateTimeFormatLocaleData() { |
| 843 |
return { |
| 844 |
ca: intl_availableCalendars, |
| 845 |
nu: getNumberingSystems, |
| 846 |
hc: () => { |
| 847 |
return [null, "h11", "h12", "h23", "h24"]; |
| 848 |
}, |
| 849 |
default: { |
| 850 |
ca: intl_defaultCalendar, |
| 851 |
nu: intl_numberingSystem, |
| 852 |
hc: () => { |
| 853 |
return null; |
| 854 |
}, |
| 855 |
}, |
| 856 |
}; |
| 857 |
} |
| 858 |
|
| 859 |
/** |
| 860 |
* Create function to be cached and returned by Intl.DateTimeFormat.prototype.format. |
| 861 |
* |
| 862 |
* Spec: ECMAScript Internationalization API Specification, 12.1.5. |
| 863 |
*/ |
| 864 |
function createDateTimeFormatFormat(dtf) { |
| 865 |
// This function is not inlined in $Intl_DateTimeFormat_format_get to avoid |
| 866 |
// creating a call-object on each call to $Intl_DateTimeFormat_format_get. |
| 867 |
return function(date) { |
| 868 |
// Step 1 (implicit). |
| 869 |
|
| 870 |
// Step 2. |
| 871 |
assert(IsObject(dtf), "dateTimeFormatFormatToBind called with non-Object"); |
| 872 |
assert( |
| 873 |
intl_GuardToDateTimeFormat(dtf) !== null, |
| 874 |
"dateTimeFormatFormatToBind called with non-DateTimeFormat" |
| 875 |
); |
| 876 |
|
| 877 |
// Steps 3-5. |
| 878 |
return intl_FormatDateTime(dtf, date, /* formatToParts = */ false); |
| 879 |
}; |
| 880 |
} |
| 881 |
|
| 882 |
/** |
| 883 |
* Returns a function bound to this DateTimeFormat that returns a String value |
| 884 |
* representing the result of calling ToNumber(date) according to the |
| 885 |
* effective locale and the formatting options of this DateTimeFormat. |
| 886 |
* |
| 887 |
* Spec: ECMAScript Internationalization API Specification, 12.4.3. |
| 888 |
*/ |
| 889 |
// Uncloned functions with `$` prefix are allocated as extended function |
| 890 |
// to store the original name in `SetCanonicalName`. |
| 891 |
function $Intl_DateTimeFormat_format_get() { |
| 892 |
// Steps 1-3. |
| 893 |
var thisArg = UnwrapDateTimeFormat(this); |
| 894 |
var dtf = thisArg; |
| 895 |
if (!IsObject(dtf) || (dtf = intl_GuardToDateTimeFormat(dtf)) === null) { |
| 896 |
return callFunction( |
| 897 |
intl_CallDateTimeFormatMethodIfWrapped, |
| 898 |
thisArg, |
| 899 |
"$Intl_DateTimeFormat_format_get" |
| 900 |
); |
| 901 |
} |
| 902 |
|
| 903 |
var internals = getDateTimeFormatInternals(dtf); |
| 904 |
|
| 905 |
// Step 4. |
| 906 |
if (internals.boundFormat === undefined) { |
| 907 |
// Steps 4.a-c. |
| 908 |
internals.boundFormat = createDateTimeFormatFormat(dtf); |
| 909 |
} |
| 910 |
|
| 911 |
// Step 5. |
| 912 |
return internals.boundFormat; |
| 913 |
} |
| 914 |
SetCanonicalName($Intl_DateTimeFormat_format_get, "get format"); |
| 915 |
|
| 916 |
/** |
| 917 |
* Intl.DateTimeFormat.prototype.formatToParts ( date ) |
| 918 |
* |
| 919 |
* Spec: ECMAScript Internationalization API Specification, 12.4.4. |
| 920 |
*/ |
| 921 |
function Intl_DateTimeFormat_formatToParts(date) { |
| 922 |
// Step 1. |
| 923 |
var dtf = this; |
| 924 |
|
| 925 |
// Steps 2-3. |
| 926 |
if (!IsObject(dtf) || (dtf = intl_GuardToDateTimeFormat(dtf)) === null) { |
| 927 |
return callFunction( |
| 928 |
intl_CallDateTimeFormatMethodIfWrapped, |
| 929 |
this, |
| 930 |
date, |
| 931 |
"Intl_DateTimeFormat_formatToParts" |
| 932 |
); |
| 933 |
} |
| 934 |
|
| 935 |
// Ensure the DateTimeFormat internals are resolved. |
| 936 |
getDateTimeFormatInternals(dtf); |
| 937 |
|
| 938 |
// Steps 4-6. |
| 939 |
return intl_FormatDateTime(dtf, date, /* formatToParts = */ true); |
| 940 |
} |
| 941 |
|
| 942 |
/** |
| 943 |
* Intl.DateTimeFormat.prototype.formatRange ( startDate , endDate ) |
| 944 |
* |
| 945 |
* Spec: Intl.DateTimeFormat.prototype.formatRange proposal |
| 946 |
*/ |
| 947 |
function Intl_DateTimeFormat_formatRange(startDate, endDate) { |
| 948 |
// Step 1. |
| 949 |
var dtf = this; |
| 950 |
|
| 951 |
// Step 2. |
| 952 |
if (!IsObject(dtf) || (dtf = intl_GuardToDateTimeFormat(dtf)) === null) { |
| 953 |
return callFunction( |
| 954 |
intl_CallDateTimeFormatMethodIfWrapped, |
| 955 |
this, |
| 956 |
startDate, |
| 957 |
endDate, |
| 958 |
"Intl_DateTimeFormat_formatRange" |
| 959 |
); |
| 960 |
} |
| 961 |
|
| 962 |
// Step 3. |
| 963 |
if (startDate === undefined || endDate === undefined) { |
| 964 |
ThrowTypeError( |
| 965 |
JSMSG_UNDEFINED_DATE, |
| 966 |
startDate === undefined ? "start" : "end", |
| 967 |
"formatRange" |
| 968 |
); |
| 969 |
} |
| 970 |
|
| 971 |
// Ensure the DateTimeFormat internals are resolved. |
| 972 |
getDateTimeFormatInternals(dtf); |
| 973 |
|
| 974 |
// Steps 4-6. |
| 975 |
return intl_FormatDateTimeRange(dtf, startDate, endDate, /* formatToParts = */ false); |
| 976 |
} |
| 977 |
|
| 978 |
/** |
| 979 |
* Intl.DateTimeFormat.prototype.formatRangeToParts ( startDate , endDate ) |
| 980 |
* |
| 981 |
* Spec: Intl.DateTimeFormat.prototype.formatRange proposal |
| 982 |
*/ |
| 983 |
function Intl_DateTimeFormat_formatRangeToParts(startDate, endDate) { |
| 984 |
// Step 1. |
| 985 |
var dtf = this; |
| 986 |
|
| 987 |
// Step 2. |
| 988 |
if (!IsObject(dtf) || (dtf = intl_GuardToDateTimeFormat(dtf)) === null) { |
| 989 |
return callFunction( |
| 990 |
intl_CallDateTimeFormatMethodIfWrapped, |
| 991 |
this, |
| 992 |
startDate, |
| 993 |
endDate, |
| 994 |
"Intl_DateTimeFormat_formatRangeToParts" |
| 995 |
); |
| 996 |
} |
| 997 |
|
| 998 |
// Step 3. |
| 999 |
if (startDate === undefined || endDate === undefined) { |
| 1000 |
ThrowTypeError( |
| 1001 |
JSMSG_UNDEFINED_DATE, |
| 1002 |
startDate === undefined ? "start" : "end", |
| 1003 |
"formatRangeToParts" |
| 1004 |
); |
| 1005 |
} |
| 1006 |
|
| 1007 |
// Ensure the DateTimeFormat internals are resolved. |
| 1008 |
getDateTimeFormatInternals(dtf); |
| 1009 |
|
| 1010 |
// Steps 4-6. |
| 1011 |
return intl_FormatDateTimeRange(dtf, startDate, endDate, /* formatToParts = */ true); |
| 1012 |
} |
| 1013 |
|
| 1014 |
/** |
| 1015 |
* Returns the resolved options for a DateTimeFormat object. |
| 1016 |
* |
| 1017 |
* Spec: ECMAScript Internationalization API Specification, 12.4.5. |
| 1018 |
*/ |
| 1019 |
function Intl_DateTimeFormat_resolvedOptions() { |
| 1020 |
// Steps 1-3. |
| 1021 |
var thisArg = UnwrapDateTimeFormat(this); |
| 1022 |
var dtf = thisArg; |
| 1023 |
if (!IsObject(dtf) || (dtf = intl_GuardToDateTimeFormat(dtf)) === null) { |
| 1024 |
return callFunction( |
| 1025 |
intl_CallDateTimeFormatMethodIfWrapped, |
| 1026 |
thisArg, |
| 1027 |
"Intl_DateTimeFormat_resolvedOptions" |
| 1028 |
); |
| 1029 |
} |
| 1030 |
|
| 1031 |
// Ensure the internals are resolved. |
| 1032 |
var internals = getDateTimeFormatInternals(dtf); |
| 1033 |
|
| 1034 |
// Steps 4-5. |
| 1035 |
var result = { |
| 1036 |
locale: internals.locale, |
| 1037 |
calendar: internals.calendar, |
| 1038 |
numberingSystem: internals.numberingSystem, |
| 1039 |
timeZone: internals.timeZone, |
| 1040 |
}; |
| 1041 |
|
| 1042 |
if (internals.pattern !== undefined) { |
| 1043 |
// The raw pattern option is only internal to Mozilla, and not part of the |
| 1044 |
// ECMA-402 API. |
| 1045 |
DefineDataProperty(result, "pattern", internals.pattern); |
| 1046 |
} |
| 1047 |
|
| 1048 |
var hasDateStyle = internals.dateStyle !== undefined; |
| 1049 |
var hasTimeStyle = internals.timeStyle !== undefined; |
| 1050 |
|
| 1051 |
if (hasDateStyle || hasTimeStyle) { |
| 1052 |
if (hasTimeStyle) { |
| 1053 |
// timeStyle (unlike dateStyle) requires resolving the pattern to |
| 1054 |
// ensure "hourCycle" and "hour12" properties are added to |result|. |
| 1055 |
intl_resolveDateTimeFormatComponents( |
| 1056 |
dtf, |
| 1057 |
result, |
| 1058 |
/* includeDateTimeFields = */ false |
| 1059 |
); |
| 1060 |
} |
| 1061 |
if (hasDateStyle) { |
| 1062 |
DefineDataProperty(result, "dateStyle", internals.dateStyle); |
| 1063 |
} |
| 1064 |
if (hasTimeStyle) { |
| 1065 |
DefineDataProperty(result, "timeStyle", internals.timeStyle); |
| 1066 |
} |
| 1067 |
} else { |
| 1068 |
// Components bag or a (Mozilla-only) raw pattern. |
| 1069 |
intl_resolveDateTimeFormatComponents( |
| 1070 |
dtf, |
| 1071 |
result, |
| 1072 |
/* includeDateTimeFields = */ true |
| 1073 |
); |
| 1074 |
} |
| 1075 |
|
| 1076 |
// Step 6. |
| 1077 |
return result; |
| 1078 |
} |