| 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 |
#include "builtin/temporal/TimeZone.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
#include "mozilla/intl/TimeZone.h" |
| 11 |
#include "mozilla/Likely.h" |
| 12 |
#include "mozilla/Maybe.h" |
| 13 |
#include "mozilla/Range.h" |
| 14 |
#include "mozilla/Result.h" |
| 15 |
#include "mozilla/Span.h" |
| 16 |
#include "mozilla/UniquePtr.h" |
| 17 |
|
| 18 |
#include <cmath> |
| 19 |
#include <cstdlib> |
| 20 |
#include <iterator> |
| 21 |
#include <string_view> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#include "jsdate.h" |
| 25 |
#include "jstypes.h" |
| 26 |
#include "NamespaceImports.h" |
| 27 |
|
| 28 |
#include "builtin/intl/CommonFunctions.h" |
| 29 |
#include "builtin/intl/FormatBuffer.h" |
| 30 |
#include "builtin/intl/SharedIntlData.h" |
| 31 |
#include "builtin/temporal/Calendar.h" |
| 32 |
#include "builtin/temporal/Instant.h" |
| 33 |
#include "builtin/temporal/PlainDate.h" |
| 34 |
#include "builtin/temporal/PlainDateTime.h" |
| 35 |
#include "builtin/temporal/PlainTime.h" |
| 36 |
#include "builtin/temporal/Temporal.h" |
| 37 |
#include "builtin/temporal/TemporalParser.h" |
| 38 |
#include "builtin/temporal/TemporalTypes.h" |
| 39 |
#include "builtin/temporal/TemporalUnit.h" |
| 40 |
#include "builtin/temporal/ZonedDateTime.h" |
| 41 |
#include "gc/Barrier.h" |
| 42 |
#include "gc/GCContext.h" |
| 43 |
#include "gc/GCEnum.h" |
| 44 |
#include "gc/Tracer.h" |
| 45 |
#include "js/AllocPolicy.h" |
| 46 |
#include "js/Class.h" |
| 47 |
#include "js/ErrorReport.h" |
| 48 |
#include "js/friend/ErrorMessages.h" |
| 49 |
#include "js/Printer.h" |
| 50 |
#include "js/RootingAPI.h" |
| 51 |
#include "js/StableStringChars.h" |
| 52 |
#include "vm/BytecodeUtil.h" |
| 53 |
#include "vm/Compartment.h" |
| 54 |
#include "vm/DateTime.h" |
| 55 |
#include "vm/JSAtomState.h" |
| 56 |
#include "vm/JSContext.h" |
| 57 |
#include "vm/JSObject.h" |
| 58 |
#include "vm/Runtime.h" |
| 59 |
#include "vm/StringType.h" |
| 60 |
|
| 61 |
#include "vm/JSObject-inl.h" |
| 62 |
|
| 63 |
using namespace js; |
| 64 |
using namespace js::temporal; |
| 65 |
|
| 66 |
void js::temporal::TimeZoneValue::trace(JSTracer* trc) { |
| 67 |
TraceNullableRoot(trc, &object_, "TimeZoneValue::object"); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* FormatOffsetTimeZoneIdentifier ( offsetMinutes [ , style ] ) |
| 72 |
*/ |
| 73 |
static JSLinearString* FormatOffsetTimeZoneIdentifier(JSContext* cx, |
| 74 |
int32_t offsetMinutes) { |
| 75 |
MOZ_ASSERT(std::abs(offsetMinutes) < UnitsPerDay(TemporalUnit::Minute)); |
| 76 |
|
| 77 |
// Step 1. |
| 78 |
char sign = offsetMinutes >= 0 ? '+' : '-'; |
| 79 |
|
| 80 |
// Step 2. |
| 81 |
int32_t absoluteMinutes = std::abs(offsetMinutes); |
| 82 |
|
| 83 |
// Step 3. |
| 84 |
int32_t hour = absoluteMinutes / 60; |
| 85 |
|
| 86 |
// Step 4. |
| 87 |
int32_t minute = absoluteMinutes % 60; |
| 88 |
|
| 89 |
// Step 5. (Inlined FormatTimeString). |
| 90 |
// |
| 91 |
// Format: "sign hour{2} : minute{2}" |
| 92 |
char result[] = { |
| 93 |
sign, char('0' + (hour / 10)), char('0' + (hour % 10)), |
| 94 |
':', char('0' + (minute / 10)), char('0' + (minute % 10)), |
| 95 |
}; |
| 96 |
|
| 97 |
// Step 6. |
| 98 |
return NewStringCopyN<CanGC>(cx, result, std::size(result)); |
| 99 |
} |
| 100 |
|
| 101 |
static TimeZoneObject* CreateTimeZoneObject( |
| 102 |
JSContext* cx, Handle<JSLinearString*> identifier, |
| 103 |
Handle<JSLinearString*> primaryIdentifier) { |
| 104 |
// TODO: Implement a built-in time zone object cache. |
| 105 |
|
| 106 |
auto* object = NewObjectWithGivenProto<TimeZoneObject>(cx, nullptr); |
| 107 |
if (!object) { |
| 108 |
return nullptr; |
| 109 |
} |
| 110 |
|
| 111 |
object->setFixedSlot(TimeZoneObject::IDENTIFIER_SLOT, |
| 112 |
StringValue(identifier)); |
| 113 |
|
| 114 |
object->setFixedSlot(TimeZoneObject::PRIMARY_IDENTIFIER_SLOT, |
| 115 |
StringValue(primaryIdentifier)); |
| 116 |
|
| 117 |
object->setFixedSlot(TimeZoneObject::OFFSET_MINUTES_SLOT, UndefinedValue()); |
| 118 |
|
| 119 |
return object; |
| 120 |
} |
| 121 |
|
| 122 |
static TimeZoneObject* CreateTimeZoneObject(JSContext* cx, |
| 123 |
int32_t offsetMinutes) { |
| 124 |
// TODO: It's unclear if offset time zones should also be cached. Real world |
| 125 |
// experience will tell if a cache should be added. |
| 126 |
|
| 127 |
MOZ_ASSERT(std::abs(offsetMinutes) < UnitsPerDay(TemporalUnit::Minute)); |
| 128 |
|
| 129 |
Rooted<JSLinearString*> identifier( |
| 130 |
cx, FormatOffsetTimeZoneIdentifier(cx, offsetMinutes)); |
| 131 |
if (!identifier) { |
| 132 |
return nullptr; |
| 133 |
} |
| 134 |
|
| 135 |
auto* object = NewObjectWithGivenProto<TimeZoneObject>(cx, nullptr); |
| 136 |
if (!object) { |
| 137 |
return nullptr; |
| 138 |
} |
| 139 |
|
| 140 |
object->setFixedSlot(TimeZoneObject::IDENTIFIER_SLOT, |
| 141 |
StringValue(identifier)); |
| 142 |
|
| 143 |
object->setFixedSlot(TimeZoneObject::PRIMARY_IDENTIFIER_SLOT, |
| 144 |
UndefinedValue()); |
| 145 |
|
| 146 |
object->setFixedSlot(TimeZoneObject::OFFSET_MINUTES_SLOT, |
| 147 |
Int32Value(offsetMinutes)); |
| 148 |
|
| 149 |
return object; |
| 150 |
} |
| 151 |
|
| 152 |
static mozilla::UniquePtr<mozilla::intl::TimeZone> CreateIntlTimeZone( |
| 153 |
JSContext* cx, JSLinearString* identifier) { |
| 154 |
JS::AutoStableStringChars stableChars(cx); |
| 155 |
if (!stableChars.initTwoByte(cx, identifier)) { |
| 156 |
return nullptr; |
| 157 |
} |
| 158 |
|
| 159 |
auto result = mozilla::intl::TimeZone::TryCreate( |
| 160 |
mozilla::Some(stableChars.twoByteRange())); |
| 161 |
if (result.isErr()) { |
| 162 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 163 |
return nullptr; |
| 164 |
} |
| 165 |
return result.unwrap(); |
| 166 |
} |
| 167 |
|
| 168 |
static mozilla::intl::TimeZone* GetOrCreateIntlTimeZone( |
| 169 |
JSContext* cx, Handle<TimeZoneValue> timeZone) { |
| 170 |
MOZ_ASSERT(!timeZone.isOffset()); |
| 171 |
|
| 172 |
// Obtain a cached mozilla::intl::TimeZone object. |
| 173 |
if (auto* tz = timeZone.getTimeZone()) { |
| 174 |
return tz; |
| 175 |
} |
| 176 |
|
| 177 |
auto* tz = CreateIntlTimeZone(cx, timeZone.primaryIdentifier()).release(); |
| 178 |
if (!tz) { |
| 179 |
return nullptr; |
| 180 |
} |
| 181 |
|
| 182 |
auto* obj = timeZone.get().toTimeZoneObject(); |
| 183 |
obj->setTimeZone(tz); |
| 184 |
|
| 185 |
intl::AddICUCellMemory(obj, TimeZoneObject::EstimatedMemoryUse); |
| 186 |
return tz; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* IsValidTimeZoneName ( timeZone ) |
| 191 |
* IsAvailableTimeZoneName ( timeZone ) |
| 192 |
*/ |
| 193 |
static bool IsValidTimeZoneName(JSContext* cx, Handle<JSLinearString*> timeZone, |
| 194 |
MutableHandle<JSAtom*> validatedTimeZone) { |
| 195 |
intl::SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 196 |
|
| 197 |
if (!sharedIntlData.validateTimeZoneName(cx, timeZone, validatedTimeZone)) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
if (validatedTimeZone) { |
| 202 |
cx->markAtom(validatedTimeZone); |
| 203 |
} |
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* 6.5.2 CanonicalizeTimeZoneName ( timeZone ) |
| 209 |
* |
| 210 |
* Canonicalizes the given IANA time zone name. |
| 211 |
* |
| 212 |
* ES2024 Intl draft rev 74ca7099f103d143431b2ea422ae640c6f43e3e6 |
| 213 |
*/ |
| 214 |
static JSLinearString* CanonicalizeTimeZoneName( |
| 215 |
JSContext* cx, Handle<JSLinearString*> timeZone) { |
| 216 |
// Step 1. (Not applicable, the input is already a valid IANA time zone.) |
| 217 |
#ifdef DEBUG |
| 218 |
MOZ_ASSERT(!StringEqualsLiteral(timeZone, "Etc/Unknown"), |
| 219 |
"Invalid time zone"); |
| 220 |
|
| 221 |
Rooted<JSAtom*> checkTimeZone(cx); |
| 222 |
if (!IsValidTimeZoneName(cx, timeZone, &checkTimeZone)) { |
| 223 |
return nullptr; |
| 224 |
} |
| 225 |
MOZ_ASSERT(EqualStrings(timeZone, checkTimeZone), |
| 226 |
"Time zone name not normalized"); |
| 227 |
#endif |
| 228 |
|
| 229 |
// Step 2. |
| 230 |
Rooted<JSLinearString*> ianaTimeZone(cx); |
| 231 |
do { |
| 232 |
intl::SharedIntlData& sharedIntlData = cx->runtime()->sharedIntlData.ref(); |
| 233 |
|
| 234 |
// Some time zone names are canonicalized differently by ICU -- handle |
| 235 |
// those first: |
| 236 |
Rooted<JSAtom*> canonicalTimeZone(cx); |
| 237 |
if (!sharedIntlData.tryCanonicalizeTimeZoneConsistentWithIANA( |
| 238 |
cx, timeZone, &canonicalTimeZone)) { |
| 239 |
return nullptr; |
| 240 |
} |
| 241 |
|
| 242 |
if (canonicalTimeZone) { |
| 243 |
cx->markAtom(canonicalTimeZone); |
| 244 |
ianaTimeZone = canonicalTimeZone; |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
JS::AutoStableStringChars stableChars(cx); |
| 249 |
if (!stableChars.initTwoByte(cx, timeZone)) { |
| 250 |
return nullptr; |
| 251 |
} |
| 252 |
|
| 253 |
intl::FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> buffer(cx); |
| 254 |
auto result = mozilla::intl::TimeZone::GetCanonicalTimeZoneID( |
| 255 |
stableChars.twoByteRange(), buffer); |
| 256 |
if (result.isErr()) { |
| 257 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 258 |
return nullptr; |
| 259 |
} |
| 260 |
|
| 261 |
ianaTimeZone = buffer.toString(cx); |
| 262 |
if (!ianaTimeZone) { |
| 263 |
return nullptr; |
| 264 |
} |
| 265 |
} while (false); |
| 266 |
|
| 267 |
#ifdef DEBUG |
| 268 |
MOZ_ASSERT(!StringEqualsLiteral(ianaTimeZone, "Etc/Unknown"), |
| 269 |
"Invalid canonical time zone"); |
| 270 |
|
| 271 |
if (!IsValidTimeZoneName(cx, ianaTimeZone, &checkTimeZone)) { |
| 272 |
return nullptr; |
| 273 |
} |
| 274 |
MOZ_ASSERT(EqualStrings(ianaTimeZone, checkTimeZone), |
| 275 |
"Unsupported canonical time zone"); |
| 276 |
#endif |
| 277 |
|
| 278 |
// Step 3. (Links to UTC are handled by SharedIntlData.) |
| 279 |
MOZ_ASSERT(!StringEqualsLiteral(ianaTimeZone, "Etc/UTC")); |
| 280 |
MOZ_ASSERT(!StringEqualsLiteral(ianaTimeZone, "Etc/GMT")); |
| 281 |
|
| 282 |
// We don't need to check against "GMT", because ICU uses the tzdata rearguard |
| 283 |
// format, where "GMT" is a link to "Etc/GMT". |
| 284 |
MOZ_ASSERT(!StringEqualsLiteral(ianaTimeZone, "GMT")); |
| 285 |
|
| 286 |
// Step 4. |
| 287 |
return ianaTimeZone; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* IsValidTimeZoneName ( timeZone ) |
| 292 |
* IsAvailableTimeZoneName ( timeZone ) |
| 293 |
* CanonicalizeTimeZoneName ( timeZone ) |
| 294 |
*/ |
| 295 |
static bool ValidateAndCanonicalizeTimeZoneName( |
| 296 |
JSContext* cx, Handle<JSLinearString*> timeZone, |
| 297 |
MutableHandle<JSLinearString*> identifier, |
| 298 |
MutableHandle<JSLinearString*> primaryIdentifier) { |
| 299 |
Rooted<JSAtom*> validatedTimeZone(cx); |
| 300 |
if (!IsValidTimeZoneName(cx, timeZone, &validatedTimeZone)) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
if (!validatedTimeZone) { |
| 305 |
if (auto chars = QuoteString(cx, timeZone)) { |
| 306 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 307 |
JSMSG_TEMPORAL_TIMEZONE_INVALID_IDENTIFIER, |
| 308 |
chars.get()); |
| 309 |
} |
| 310 |
return false; |
| 311 |
} |
| 312 |
|
| 313 |
auto* canonical = CanonicalizeTimeZoneName(cx, validatedTimeZone); |
| 314 |
if (!canonical) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
identifier.set(validatedTimeZone); |
| 319 |
primaryIdentifier.set(canonical); |
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
static bool SystemTimeZoneOffset(JSContext* cx, int32_t* offset) { |
| 324 |
auto rawOffset = |
| 325 |
DateTimeInfo::getRawOffsetMs(DateTimeInfo::forceUTC(cx->realm())); |
| 326 |
if (rawOffset.isErr()) { |
| 327 |
intl::ReportInternalError(cx); |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
*offset = rawOffset.unwrap(); |
| 332 |
return true; |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* SystemTimeZoneIdentifier ( ) |
| 337 |
* |
| 338 |
* Returns the IANA time zone name for the host environment's current time zone. |
| 339 |
*/ |
| 340 |
JSLinearString* js::temporal::SystemTimeZoneIdentifier(JSContext* cx) { |
| 341 |
intl::FormatBuffer<char16_t, intl::INITIAL_CHAR_BUFFER_SIZE> formatBuffer(cx); |
| 342 |
auto result = DateTimeInfo::timeZoneId(DateTimeInfo::forceUTC(cx->realm()), |
| 343 |
formatBuffer); |
| 344 |
if (result.isErr()) { |
| 345 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 346 |
return nullptr; |
| 347 |
} |
| 348 |
|
| 349 |
Rooted<JSLinearString*> timeZone(cx, formatBuffer.toString(cx)); |
| 350 |
if (!timeZone) { |
| 351 |
return nullptr; |
| 352 |
} |
| 353 |
|
| 354 |
Rooted<JSAtom*> validTimeZone(cx); |
| 355 |
if (!IsValidTimeZoneName(cx, timeZone, &validTimeZone)) { |
| 356 |
return nullptr; |
| 357 |
} |
| 358 |
if (validTimeZone) { |
| 359 |
return CanonicalizeTimeZoneName(cx, validTimeZone); |
| 360 |
} |
| 361 |
|
| 362 |
// See DateTimeFormat.js for the JS implementation. |
| 363 |
// TODO: Move the JS implementation into C++. |
| 364 |
|
| 365 |
// Before defaulting to "UTC", try to represent the system time zone using |
| 366 |
// the Etc/GMT + offset format. This format only accepts full hour offsets. |
| 367 |
int32_t offset; |
| 368 |
if (!SystemTimeZoneOffset(cx, &offset)) { |
| 369 |
return nullptr; |
| 370 |
} |
| 371 |
|
| 372 |
constexpr int32_t msPerHour = 60 * 60 * 1000; |
| 373 |
int32_t offsetHours = std::abs(offset / msPerHour); |
| 374 |
int32_t offsetHoursFraction = offset % msPerHour; |
| 375 |
if (offsetHoursFraction == 0 && offsetHours < 24) { |
| 376 |
// Etc/GMT + offset uses POSIX-style signs, i.e. a positive offset |
| 377 |
// means a location west of GMT. |
| 378 |
constexpr std::string_view etcGMT = "Etc/GMT"; |
| 379 |
|
| 380 |
char offsetString[etcGMT.length() + 3]; |
| 381 |
|
| 382 |
size_t n = etcGMT.copy(offsetString, etcGMT.length()); |
| 383 |
offsetString[n++] = offset < 0 ? '+' : '-'; |
| 384 |
if (offsetHours >= 10) { |
| 385 |
offsetString[n++] = char('0' + (offsetHours / 10)); |
| 386 |
} |
| 387 |
offsetString[n++] = char('0' + (offsetHours % 10)); |
| 388 |
|
| 389 |
MOZ_ASSERT(n == etcGMT.length() + 2 || n == etcGMT.length() + 3); |
| 390 |
|
| 391 |
timeZone = NewStringCopyN<CanGC>(cx, offsetString, n); |
| 392 |
if (!timeZone) { |
| 393 |
return nullptr; |
| 394 |
} |
| 395 |
|
| 396 |
// Check if the fallback is valid. |
| 397 |
if (!IsValidTimeZoneName(cx, timeZone, &validTimeZone)) { |
| 398 |
return nullptr; |
| 399 |
} |
| 400 |
if (validTimeZone) { |
| 401 |
return CanonicalizeTimeZoneName(cx, validTimeZone); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
// Fallback to "UTC" if everything else fails. |
| 406 |
return cx->names().UTC; |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* SystemTimeZoneIdentifier ( ) |
| 411 |
*/ |
| 412 |
bool js::temporal::SystemTimeZone(JSContext* cx, |
| 413 |
MutableHandle<TimeZoneValue> result) { |
| 414 |
Rooted<JSLinearString*> identifier(cx, SystemTimeZoneIdentifier(cx)); |
| 415 |
if (!identifier) { |
| 416 |
return false; |
| 417 |
} |
| 418 |
|
| 419 |
auto* timeZone = CreateTimeZoneObject(cx, identifier, identifier); |
| 420 |
if (!timeZone) { |
| 421 |
return false; |
| 422 |
} |
| 423 |
|
| 424 |
result.set(TimeZoneValue(timeZone)); |
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier, isoDateTime ) |
| 430 |
*/ |
| 431 |
static bool GetNamedTimeZoneEpochNanoseconds(JSContext* cx, |
| 432 |
Handle<TimeZoneValue> timeZone, |
| 433 |
const ISODateTime& isoDateTime, |
| 434 |
PossibleEpochNanoseconds* result) { |
| 435 |
MOZ_ASSERT(!timeZone.isOffset()); |
| 436 |
MOZ_ASSERT(IsValidISODateTime(isoDateTime)); |
| 437 |
MOZ_ASSERT(ISODateTimeWithinLimits(isoDateTime)); |
| 438 |
|
| 439 |
// FIXME: spec issue - assert ISODateTimeWithinLimits instead of |
| 440 |
// IsValidISODate |
| 441 |
|
| 442 |
int64_t ms = MakeDate(isoDateTime); |
| 443 |
|
| 444 |
auto* tz = GetOrCreateIntlTimeZone(cx, timeZone); |
| 445 |
if (!tz) { |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
auto getOffset = [&](mozilla::intl::TimeZone::LocalOption skippedTime, |
| 450 |
mozilla::intl::TimeZone::LocalOption repeatedTime, |
| 451 |
int32_t* offset) { |
| 452 |
auto result = tz->GetUTCOffsetMs(ms, skippedTime, repeatedTime); |
| 453 |
if (result.isErr()) { |
| 454 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
*offset = result.unwrap(); |
| 459 |
MOZ_ASSERT(std::abs(*offset) < UnitsPerDay(TemporalUnit::Millisecond)); |
| 460 |
|
| 461 |
return true; |
| 462 |
}; |
| 463 |
|
| 464 |
constexpr auto formerTime = mozilla::intl::TimeZone::LocalOption::Former; |
| 465 |
constexpr auto latterTime = mozilla::intl::TimeZone::LocalOption::Latter; |
| 466 |
|
| 467 |
int32_t formerOffset; |
| 468 |
if (!getOffset(formerTime, formerTime, &formerOffset)) { |
| 469 |
return false; |
| 470 |
} |
| 471 |
|
| 472 |
int32_t latterOffset; |
| 473 |
if (!getOffset(latterTime, latterTime, &latterOffset)) { |
| 474 |
return false; |
| 475 |
} |
| 476 |
|
| 477 |
if (formerOffset == latterOffset) { |
| 478 |
auto epochNs = GetUTCEpochNanoseconds(isoDateTime) - |
| 479 |
EpochDuration::fromMilliseconds(formerOffset); |
| 480 |
*result = PossibleEpochNanoseconds{epochNs}; |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
int32_t disambiguationOffset; |
| 485 |
if (!getOffset(formerTime, latterTime, &disambiguationOffset)) { |
| 486 |
return false; |
| 487 |
} |
| 488 |
|
| 489 |
// Skipped time. |
| 490 |
if (disambiguationOffset == formerOffset) { |
| 491 |
*result = {}; |
| 492 |
return true; |
| 493 |
} |
| 494 |
|
| 495 |
// Repeated time. |
| 496 |
auto formerInstant = GetUTCEpochNanoseconds(isoDateTime) - |
| 497 |
EpochDuration::fromMilliseconds(formerOffset); |
| 498 |
auto latterInstant = GetUTCEpochNanoseconds(isoDateTime) - |
| 499 |
EpochDuration::fromMilliseconds(latterOffset); |
| 500 |
|
| 501 |
// Ensure the returned epoch nanoseconds are sorted in numerical order. |
| 502 |
if (formerInstant > latterInstant) { |
| 503 |
std::swap(formerInstant, latterInstant); |
| 504 |
} |
| 505 |
|
| 506 |
*result = PossibleEpochNanoseconds{formerInstant, latterInstant}; |
| 507 |
return true; |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier, epochNanoseconds ) |
| 512 |
*/ |
| 513 |
static bool GetNamedTimeZoneOffsetNanoseconds( |
| 514 |
JSContext* cx, Handle<TimeZoneValue> timeZone, |
| 515 |
const EpochNanoseconds& epochNanoseconds, int64_t* offset) { |
| 516 |
MOZ_ASSERT(!timeZone.isOffset()); |
| 517 |
|
| 518 |
// Round down (floor) to the previous full milliseconds. |
| 519 |
int64_t millis = epochNanoseconds.floorToMilliseconds(); |
| 520 |
|
| 521 |
auto* tz = GetOrCreateIntlTimeZone(cx, timeZone); |
| 522 |
if (!tz) { |
| 523 |
return false; |
| 524 |
} |
| 525 |
|
| 526 |
auto result = tz->GetOffsetMs(millis); |
| 527 |
if (result.isErr()) { |
| 528 |
intl::ReportInternalError(cx, result.unwrapErr()); |
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
// FIXME: spec issue - should constrain the range to not exceed 24-hours. |
| 533 |
// https://github.com/tc39/ecma262/issues/3101 |
| 534 |
|
| 535 |
int64_t nanoPerMs = 1'000'000; |
| 536 |
*offset = result.unwrap() * nanoPerMs; |
| 537 |
return true; |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* GetNamedTimeZoneNextTransition ( timeZoneIdentifier, epochNanoseconds ) |
| 542 |
*/ |
| 543 |
bool js::temporal::GetNamedTimeZoneNextTransition( |
| 544 |
JSContext* cx, Handle<TimeZoneValue> timeZone, |
| 545 |
const EpochNanoseconds& epochNanoseconds, |
| 546 |
mozilla::Maybe<EpochNanoseconds>* result) { |
| 547 |
MOZ_ASSERT(!timeZone.isOffset()); |
| 548 |
|
| 549 |
// Round down (floor) to the previous full millisecond. |
| 550 |
// |
| 551 |
// IANA has experimental support for transitions at sub-second precision, but |
| 552 |
// the default configuration doesn't enable it, therefore it's safe to round |
| 553 |
// to milliseconds here. In addition to that, ICU also only supports |
| 554 |
// transitions at millisecond precision. |
| 555 |
int64_t millis = epochNanoseconds.floorToMilliseconds(); |
| 556 |
|
| 557 |
auto* tz = GetOrCreateIntlTimeZone(cx, timeZone); |
| 558 |
if (!tz) { |
| 559 |
return false; |
| 560 |
} |
| 561 |
|
| 562 |
auto next = tz->GetNextTransition(millis); |
| 563 |
if (next.isErr()) { |
| 564 |
intl::ReportInternalError(cx, next.unwrapErr()); |
| 565 |
return false; |
| 566 |
} |
| 567 |
|
| 568 |
auto transition = next.unwrap(); |
| 569 |
if (!transition) { |
| 570 |
*result = mozilla::Nothing(); |
| 571 |
return true; |
| 572 |
} |
| 573 |
|
| 574 |
auto transitionInstant = EpochNanoseconds::fromMilliseconds(*transition); |
| 575 |
if (!IsValidEpochNanoseconds(transitionInstant)) { |
| 576 |
*result = mozilla::Nothing(); |
| 577 |
return true; |
| 578 |
} |
| 579 |
|
| 580 |
*result = mozilla::Some(transitionInstant); |
| 581 |
return true; |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* GetNamedTimeZonePreviousTransition ( timeZoneIdentifier, epochNanoseconds ) |
| 586 |
*/ |
| 587 |
bool js::temporal::GetNamedTimeZonePreviousTransition( |
| 588 |
JSContext* cx, Handle<TimeZoneValue> timeZone, |
| 589 |
const EpochNanoseconds& epochNanoseconds, |
| 590 |
mozilla::Maybe<EpochNanoseconds>* result) { |
| 591 |
MOZ_ASSERT(!timeZone.isOffset()); |
| 592 |
|
| 593 |
// Round up (ceil) to the next full millisecond. |
| 594 |
// |
| 595 |
// IANA has experimental support for transitions at sub-second precision, but |
| 596 |
// the default configuration doesn't enable it, therefore it's safe to round |
| 597 |
// to milliseconds here. In addition to that, ICU also only supports |
| 598 |
// transitions at millisecond precision. |
| 599 |
int64_t millis = epochNanoseconds.ceilToMilliseconds(); |
| 600 |
|
| 601 |
auto* tz = GetOrCreateIntlTimeZone(cx, timeZone); |
| 602 |
if (!tz) { |
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
auto previous = tz->GetPreviousTransition(millis); |
| 607 |
if (previous.isErr()) { |
| 608 |
intl::ReportInternalError(cx, previous.unwrapErr()); |
| 609 |
return false; |
| 610 |
} |
| 611 |
|
| 612 |
auto transition = previous.unwrap(); |
| 613 |
if (!transition) { |
| 614 |
*result = mozilla::Nothing(); |
| 615 |
return true; |
| 616 |
} |
| 617 |
|
| 618 |
auto transitionInstant = EpochNanoseconds::fromMilliseconds(*transition); |
| 619 |
if (!IsValidEpochNanoseconds(transitionInstant)) { |
| 620 |
*result = mozilla::Nothing(); |
| 621 |
return true; |
| 622 |
} |
| 623 |
|
| 624 |
*result = mozilla::Some(transitionInstant); |
| 625 |
return true; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* GetStartOfDay ( timeZone, isoDate ) |
| 630 |
*/ |
| 631 |
bool js::temporal::GetStartOfDay(JSContext* cx, Handle<TimeZoneValue> timeZone, |
| 632 |
const ISODate& isoDate, |
| 633 |
EpochNanoseconds* result) { |
| 634 |
MOZ_ASSERT(IsValidISODate(isoDate)); |
| 635 |
|
| 636 |
// Step 1. |
| 637 |
auto isoDateTime = ISODateTime{isoDate, {}}; |
| 638 |
|
| 639 |
// Step 2. |
| 640 |
PossibleEpochNanoseconds possibleEpochNs; |
| 641 |
if (!GetPossibleEpochNanoseconds(cx, timeZone, isoDateTime, |
| 642 |
&possibleEpochNs)) { |
| 643 |
return false; |
| 644 |
} |
| 645 |
MOZ_ASSERT(ISODateTimeWithinLimits(isoDateTime)); |
| 646 |
|
| 647 |
// Step 3. |
| 648 |
if (!possibleEpochNs.empty()) { |
| 649 |
*result = possibleEpochNs[0]; |
| 650 |
return true; |
| 651 |
} |
| 652 |
|
| 653 |
// Step 4. |
| 654 |
MOZ_ASSERT(!timeZone.isOffset()); |
| 655 |
|
| 656 |
constexpr auto oneDay = EpochDuration::fromDays(1); |
| 657 |
|
| 658 |
// Step 5. |
| 659 |
auto previousDayEpochNs = GetUTCEpochNanoseconds(isoDateTime) - oneDay; |
| 660 |
mozilla::Maybe<EpochNanoseconds> transition{}; |
| 661 |
if (!GetNamedTimeZoneNextTransition(cx, timeZone, previousDayEpochNs, |
| 662 |
&transition)) { |
| 663 |
return false; |
| 664 |
} |
| 665 |
|
| 666 |
// Step 6. |
| 667 |
MOZ_ASSERT(transition, "time zone transition not found"); |
| 668 |
|
| 669 |
// Step 7. |
| 670 |
*result = *transition; |
| 671 |
return true; |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* ToTemporalTimeZoneIdentifier ( temporalTimeZoneLike ) |
| 676 |
*/ |
| 677 |
bool js::temporal::ToTemporalTimeZone(JSContext* cx, |
| 678 |
Handle<ParsedTimeZone> string, |
| 679 |
MutableHandle<TimeZoneValue> result) { |
| 680 |
// Steps 1-3. (Not applicable) |
| 681 |
|
| 682 |
// Steps 4-5. |
| 683 |
if (!string.name()) { |
| 684 |
auto* obj = CreateTimeZoneObject(cx, string.offset()); |
| 685 |
if (!obj) { |
| 686 |
return false; |
| 687 |
} |
| 688 |
|
| 689 |
result.set(TimeZoneValue(obj)); |
| 690 |
return true; |
| 691 |
} |
| 692 |
|
| 693 |
// Steps 6-8. |
| 694 |
Rooted<JSLinearString*> identifier(cx); |
| 695 |
Rooted<JSLinearString*> primaryIdentifier(cx); |
| 696 |
if (!ValidateAndCanonicalizeTimeZoneName(cx, string.name(), &identifier, |
| 697 |
&primaryIdentifier)) { |
| 698 |
return false; |
| 699 |
} |
| 700 |
|
| 701 |
// Step 9. |
| 702 |
auto* obj = CreateTimeZoneObject(cx, identifier, primaryIdentifier); |
| 703 |
if (!obj) { |
| 704 |
return false; |
| 705 |
} |
| 706 |
|
| 707 |
result.set(TimeZoneValue(obj)); |
| 708 |
return true; |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
* ToTemporalTimeZoneIdentifier ( temporalTimeZoneLike ) |
| 713 |
*/ |
| 714 |
bool js::temporal::ToTemporalTimeZone(JSContext* cx, |
| 715 |
Handle<Value> temporalTimeZoneLike, |
| 716 |
MutableHandle<TimeZoneValue> result) { |
| 717 |
// Step 1. |
| 718 |
if (temporalTimeZoneLike.isObject()) { |
| 719 |
JSObject* obj = &temporalTimeZoneLike.toObject(); |
| 720 |
|
| 721 |
// Step 1.a. |
| 722 |
if (auto* zonedDateTime = obj->maybeUnwrapIf<ZonedDateTimeObject>()) { |
| 723 |
result.set(zonedDateTime->timeZone()); |
| 724 |
return result.wrap(cx); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
// Step 2. |
| 729 |
if (!temporalTimeZoneLike.isString()) { |
| 730 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, |
| 731 |
temporalTimeZoneLike, nullptr, "not a string"); |
| 732 |
return false; |
| 733 |
} |
| 734 |
Rooted<JSString*> identifier(cx, temporalTimeZoneLike.toString()); |
| 735 |
|
| 736 |
// Step 3. |
| 737 |
Rooted<ParsedTimeZone> timeZoneName(cx); |
| 738 |
if (!ParseTemporalTimeZoneString(cx, identifier, &timeZoneName)) { |
| 739 |
return false; |
| 740 |
} |
| 741 |
|
| 742 |
// Steps 4-9. |
| 743 |
return ToTemporalTimeZone(cx, timeZoneName, result); |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* GetOffsetNanosecondsFor ( timeZone, epochNs ) |
| 748 |
*/ |
| 749 |
bool js::temporal::GetOffsetNanosecondsFor(JSContext* cx, |
| 750 |
Handle<TimeZoneValue> timeZone, |
| 751 |
const EpochNanoseconds& epochNs, |
| 752 |
int64_t* offsetNanoseconds) { |
| 753 |
// Step 1. (Not applicable) |
| 754 |
|
| 755 |
// Step 2. |
| 756 |
if (timeZone.isOffset()) { |
| 757 |
int32_t offset = timeZone.offsetMinutes(); |
| 758 |
MOZ_ASSERT(std::abs(offset) < UnitsPerDay(TemporalUnit::Minute)); |
| 759 |
|
| 760 |
*offsetNanoseconds = int64_t(offset) * ToNanoseconds(TemporalUnit::Minute); |
| 761 |
return true; |
| 762 |
} |
| 763 |
|
| 764 |
// Step 3. |
| 765 |
int64_t offset; |
| 766 |
if (!GetNamedTimeZoneOffsetNanoseconds(cx, timeZone, epochNs, &offset)) { |
| 767 |
return false; |
| 768 |
} |
| 769 |
MOZ_ASSERT(std::abs(offset) < ToNanoseconds(TemporalUnit::Day)); |
| 770 |
|
| 771 |
*offsetNanoseconds = offset; |
| 772 |
return true; |
| 773 |
} |
| 774 |
|
| 775 |
/** |
| 776 |
* TimeZoneEquals ( one, two ) |
| 777 |
*/ |
| 778 |
bool js::temporal::TimeZoneEquals(const TimeZoneValue& one, |
| 779 |
const TimeZoneValue& two) { |
| 780 |
// Steps 1-3. (Not applicable in our implementation.) |
| 781 |
|
| 782 |
// Step 4. |
| 783 |
if (!one.isOffset() && !two.isOffset()) { |
| 784 |
return EqualStrings(one.primaryIdentifier(), two.primaryIdentifier()); |
| 785 |
} |
| 786 |
|
| 787 |
// Step 5. |
| 788 |
if (one.isOffset() && two.isOffset()) { |
| 789 |
return one.offsetMinutes() == two.offsetMinutes(); |
| 790 |
} |
| 791 |
|
| 792 |
// Step 6. |
| 793 |
return false; |
| 794 |
} |
| 795 |
|
| 796 |
/** |
| 797 |
* GetISOPartsFromEpoch ( epochNanoseconds ) |
| 798 |
*/ |
| 799 |
static ISODateTime GetISOPartsFromEpoch( |
| 800 |
const EpochNanoseconds& epochNanoseconds, int64_t offsetNanoseconds) { |
| 801 |
// Step 1. |
| 802 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 803 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 804 |
|
| 805 |
auto totalNanoseconds = |
| 806 |
epochNanoseconds + EpochDuration::fromNanoseconds(offsetNanoseconds); |
| 807 |
|
| 808 |
// Step 2. |
| 809 |
int32_t remainderNs = totalNanoseconds.nanoseconds % 1'000'000; |
| 810 |
|
| 811 |
// Step 10. (Reordered) |
| 812 |
// |
| 813 |
// Reordered so the compiler can merge the divisons in steps 2, 3, and 10. |
| 814 |
int32_t millisecond = totalNanoseconds.nanoseconds / 1'000'000; |
| 815 |
|
| 816 |
// Step 3. |
| 817 |
int64_t epochMilliseconds = totalNanoseconds.floorToMilliseconds(); |
| 818 |
|
| 819 |
// Steps 4-6. |
| 820 |
auto [year, month, day] = ToYearMonthDay(epochMilliseconds); |
| 821 |
|
| 822 |
// Steps 7-9. |
| 823 |
auto [hour, minute, second] = ToHourMinuteSecond(epochMilliseconds); |
| 824 |
|
| 825 |
// Step 10. (Moved above) |
| 826 |
|
| 827 |
// Steps 11-12. |
| 828 |
int32_t microsecond = remainderNs / 1000; |
| 829 |
|
| 830 |
// Step 13. |
| 831 |
int32_t nanosecond = remainderNs % 1000; |
| 832 |
|
| 833 |
// Step 14. |
| 834 |
auto isoDate = ISODate{year, month + 1, day}; |
| 835 |
MOZ_ASSERT(IsValidISODate(isoDate)); |
| 836 |
|
| 837 |
// Step 15. |
| 838 |
auto time = Time{hour, minute, second, millisecond, microsecond, nanosecond}; |
| 839 |
MOZ_ASSERT(IsValidTime(time)); |
| 840 |
|
| 841 |
// Step 16. |
| 842 |
auto result = ISODateTime{isoDate, time}; |
| 843 |
|
| 844 |
// Always within date-time limits when the epoch nanoseconds are within limit. |
| 845 |
MOZ_ASSERT(ISODateTimeWithinLimits(result)); |
| 846 |
|
| 847 |
return result; |
| 848 |
} |
| 849 |
|
| 850 |
/** |
| 851 |
* GetISODateTimeFor ( timeZone, epochNs ) |
| 852 |
*/ |
| 853 |
ISODateTime js::temporal::GetISODateTimeFor(const EpochNanoseconds& epochNs, |
| 854 |
int64_t offsetNanoseconds) { |
| 855 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNs)); |
| 856 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 857 |
|
| 858 |
// Step 1. (Not applicable) |
| 859 |
|
| 860 |
// Steps 2-3. |
| 861 |
return GetISOPartsFromEpoch(epochNs, offsetNanoseconds); |
| 862 |
} |
| 863 |
|
| 864 |
/** |
| 865 |
* GetISODateTimeFor ( timeZone, epochNs ) |
| 866 |
*/ |
| 867 |
bool js::temporal::GetISODateTimeFor(JSContext* cx, |
| 868 |
Handle<TimeZoneValue> timeZone, |
| 869 |
const EpochNanoseconds& epochNs, |
| 870 |
ISODateTime* result) { |
| 871 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNs)); |
| 872 |
|
| 873 |
// Step 1. |
| 874 |
int64_t offsetNanoseconds; |
| 875 |
if (!GetOffsetNanosecondsFor(cx, timeZone, epochNs, &offsetNanoseconds)) { |
| 876 |
return false; |
| 877 |
} |
| 878 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 879 |
|
| 880 |
// Steps 2-3. |
| 881 |
*result = GetISODateTimeFor(epochNs, offsetNanoseconds); |
| 882 |
return true; |
| 883 |
} |
| 884 |
|
| 885 |
/** |
| 886 |
* GetPossibleEpochNanoseconds ( timeZone, isoDateTime ) |
| 887 |
*/ |
| 888 |
bool js::temporal::GetPossibleEpochNanoseconds( |
| 889 |
JSContext* cx, Handle<TimeZoneValue> timeZone, |
| 890 |
const ISODateTime& isoDateTime, PossibleEpochNanoseconds* result) { |
| 891 |
// TODO: https://github.com/tc39/proposal-temporal/pull/3014 |
| 892 |
if (!ISODateTimeWithinLimits(isoDateTime)) { |
| 893 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 894 |
JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID); |
| 895 |
return false; |
| 896 |
} |
| 897 |
|
| 898 |
// Step 1. (Not applicable) |
| 899 |
|
| 900 |
// Step 2. |
| 901 |
PossibleEpochNanoseconds possibleEpochNanoseconds; |
| 902 |
if (timeZone.isOffset()) { |
| 903 |
int32_t offsetMin = timeZone.offsetMinutes(); |
| 904 |
MOZ_ASSERT(std::abs(offsetMin) < UnitsPerDay(TemporalUnit::Minute)); |
| 905 |
|
| 906 |
// Step 2.a. |
| 907 |
auto epochInstant = GetUTCEpochNanoseconds(isoDateTime) - |
| 908 |
EpochDuration::fromMinutes(offsetMin); |
| 909 |
|
| 910 |
// Step 2.b. |
| 911 |
possibleEpochNanoseconds = PossibleEpochNanoseconds{epochInstant}; |
| 912 |
} else { |
| 913 |
// Step 3. |
| 914 |
if (!GetNamedTimeZoneEpochNanoseconds(cx, timeZone, isoDateTime, |
| 915 |
&possibleEpochNanoseconds)) { |
| 916 |
return false; |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
MOZ_ASSERT(possibleEpochNanoseconds.length() <= 2); |
| 921 |
|
| 922 |
// Step 4. |
| 923 |
for (const auto& epochInstant : possibleEpochNanoseconds) { |
| 924 |
if (!IsValidEpochNanoseconds(epochInstant)) { |
| 925 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 926 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 927 |
return false; |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
// Step 5. |
| 932 |
*result = possibleEpochNanoseconds; |
| 933 |
return true; |
| 934 |
} |
| 935 |
|
| 936 |
/** |
| 937 |
* AddTime ( time, timeDuration ) |
| 938 |
*/ |
| 939 |
static auto AddTime(const Time& time, int64_t nanoseconds) { |
| 940 |
MOZ_ASSERT(IsValidTime(time)); |
| 941 |
MOZ_ASSERT(std::abs(nanoseconds) <= ToNanoseconds(TemporalUnit::Day)); |
| 942 |
|
| 943 |
// Steps 1-2. |
| 944 |
return BalanceTime(time, nanoseconds); |
| 945 |
} |
| 946 |
|
| 947 |
/** |
| 948 |
* DisambiguatePossibleEpochNanoseconds ( possibleEpochNs, timeZone, |
| 949 |
* isoDateTime, disambiguation ) |
| 950 |
*/ |
| 951 |
bool js::temporal::DisambiguatePossibleEpochNanoseconds( |
| 952 |
JSContext* cx, const PossibleEpochNanoseconds& possibleEpochNs, |
| 953 |
Handle<TimeZoneValue> timeZone, const ISODateTime& isoDateTime, |
| 954 |
TemporalDisambiguation disambiguation, EpochNanoseconds* result) { |
| 955 |
MOZ_ASSERT(IsValidISODateTime(isoDateTime)); |
| 956 |
|
| 957 |
// Steps 1-2. |
| 958 |
if (possibleEpochNs.length() == 1) { |
| 959 |
*result = possibleEpochNs.front(); |
| 960 |
return true; |
| 961 |
} |
| 962 |
|
| 963 |
// Steps 3-4. |
| 964 |
if (!possibleEpochNs.empty()) { |
| 965 |
// Step 3.a. |
| 966 |
if (disambiguation == TemporalDisambiguation::Earlier || |
| 967 |
disambiguation == TemporalDisambiguation::Compatible) { |
| 968 |
*result = possibleEpochNs.front(); |
| 969 |
return true; |
| 970 |
} |
| 971 |
|
| 972 |
// Step 3.b. |
| 973 |
if (disambiguation == TemporalDisambiguation::Later) { |
| 974 |
*result = possibleEpochNs.back(); |
| 975 |
return true; |
| 976 |
} |
| 977 |
|
| 978 |
// Step 3.c. |
| 979 |
MOZ_ASSERT(disambiguation == TemporalDisambiguation::Reject); |
| 980 |
|
| 981 |
// Step 3.d. |
| 982 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 983 |
JSMSG_TEMPORAL_TIMEZONE_INSTANT_AMBIGUOUS); |
| 984 |
return false; |
| 985 |
} |
| 986 |
|
| 987 |
// Step 5. |
| 988 |
if (disambiguation == TemporalDisambiguation::Reject) { |
| 989 |
// TODO: Improve error message to say the date was skipped. |
| 990 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 991 |
JSMSG_TEMPORAL_TIMEZONE_INSTANT_AMBIGUOUS); |
| 992 |
return false; |
| 993 |
} |
| 994 |
|
| 995 |
constexpr auto oneDay = EpochDuration::fromDays(1); |
| 996 |
|
| 997 |
auto epochNanoseconds = GetUTCEpochNanoseconds(isoDateTime); |
| 998 |
|
| 999 |
// Step 6 and 8-9. |
| 1000 |
auto dayBefore = epochNanoseconds - oneDay; |
| 1001 |
MOZ_ASSERT(IsValidEpochNanoseconds(dayBefore)); |
| 1002 |
|
| 1003 |
// Step 7 and 10-11. |
| 1004 |
auto dayAfter = epochNanoseconds + oneDay; |
| 1005 |
MOZ_ASSERT(IsValidEpochNanoseconds(dayAfter)); |
| 1006 |
|
| 1007 |
// Step 12. |
| 1008 |
int64_t offsetBefore; |
| 1009 |
if (!GetOffsetNanosecondsFor(cx, timeZone, dayBefore, &offsetBefore)) { |
| 1010 |
return false; |
| 1011 |
} |
| 1012 |
MOZ_ASSERT(std::abs(offsetBefore) < ToNanoseconds(TemporalUnit::Day)); |
| 1013 |
|
| 1014 |
// Step 13. |
| 1015 |
int64_t offsetAfter; |
| 1016 |
if (!GetOffsetNanosecondsFor(cx, timeZone, dayAfter, &offsetAfter)) { |
| 1017 |
return false; |
| 1018 |
} |
| 1019 |
MOZ_ASSERT(std::abs(offsetAfter) < ToNanoseconds(TemporalUnit::Day)); |
| 1020 |
|
| 1021 |
// Step 14. |
| 1022 |
int64_t nanoseconds = offsetAfter - offsetBefore; |
| 1023 |
|
| 1024 |
// Step 15. |
| 1025 |
MOZ_ASSERT(std::abs(nanoseconds) <= ToNanoseconds(TemporalUnit::Day)); |
| 1026 |
|
| 1027 |
// Step 16. |
| 1028 |
if (disambiguation == TemporalDisambiguation::Earlier) { |
| 1029 |
// Steps 16.a-b. |
| 1030 |
auto earlierTime = ::AddTime(isoDateTime.time, -nanoseconds); |
| 1031 |
MOZ_ASSERT(std::abs(earlierTime.days) <= 1, |
| 1032 |
"subtracting nanoseconds is at most one day"); |
| 1033 |
|
| 1034 |
// Step 16.c. |
| 1035 |
auto earlierDate = BalanceISODate(isoDateTime.date, earlierTime.days); |
| 1036 |
|
| 1037 |
// Step 16.d. |
| 1038 |
auto earlierDateTime = ISODateTime{earlierDate, earlierTime.time}; |
| 1039 |
|
| 1040 |
// Step 16.e. |
| 1041 |
PossibleEpochNanoseconds earlierEpochNs; |
| 1042 |
if (!GetPossibleEpochNanoseconds(cx, timeZone, earlierDateTime, |
| 1043 |
&earlierEpochNs)) { |
| 1044 |
return false; |
| 1045 |
} |
| 1046 |
|
| 1047 |
// Step 16.f. |
| 1048 |
MOZ_ASSERT(!earlierEpochNs.empty()); |
| 1049 |
|
| 1050 |
// Step 16.g. |
| 1051 |
*result = earlierEpochNs.front(); |
| 1052 |
return true; |
| 1053 |
} |
| 1054 |
|
| 1055 |
// Step 17. |
| 1056 |
MOZ_ASSERT(disambiguation == TemporalDisambiguation::Compatible || |
| 1057 |
disambiguation == TemporalDisambiguation::Later); |
| 1058 |
|
| 1059 |
// Steps 18-19. |
| 1060 |
auto laterTime = ::AddTime(isoDateTime.time, nanoseconds); |
| 1061 |
MOZ_ASSERT(std::abs(laterTime.days) <= 1, |
| 1062 |
"adding nanoseconds is at most one day"); |
| 1063 |
|
| 1064 |
// Step 20. |
| 1065 |
auto laterDate = BalanceISODate(isoDateTime.date, laterTime.days); |
| 1066 |
|
| 1067 |
// Step 21. |
| 1068 |
auto laterDateTime = ISODateTime{laterDate, laterTime.time}; |
| 1069 |
|
| 1070 |
// Step 22. |
| 1071 |
PossibleEpochNanoseconds laterEpochNs; |
| 1072 |
if (!GetPossibleEpochNanoseconds(cx, timeZone, laterDateTime, |
| 1073 |
&laterEpochNs)) { |
| 1074 |
return false; |
| 1075 |
} |
| 1076 |
|
| 1077 |
// Steps 23-24. |
| 1078 |
MOZ_ASSERT(!laterEpochNs.empty()); |
| 1079 |
|
| 1080 |
// Step 25. |
| 1081 |
*result = laterEpochNs.back(); |
| 1082 |
return true; |
| 1083 |
} |
| 1084 |
|
| 1085 |
/** |
| 1086 |
* GetEpochNanosecondsFor ( timeZone, isoDateTime, disambiguation ) |
| 1087 |
*/ |
| 1088 |
bool js::temporal::GetEpochNanosecondsFor(JSContext* cx, |
| 1089 |
Handle<TimeZoneValue> timeZone, |
| 1090 |
const ISODateTime& isoDateTime, |
| 1091 |
TemporalDisambiguation disambiguation, |
| 1092 |
EpochNanoseconds* result) { |
| 1093 |
// Step 1. |
| 1094 |
PossibleEpochNanoseconds possibleEpochNs; |
| 1095 |
if (!GetPossibleEpochNanoseconds(cx, timeZone, isoDateTime, |
| 1096 |
&possibleEpochNs)) { |
| 1097 |
return false; |
| 1098 |
} |
| 1099 |
|
| 1100 |
// Step 2. |
| 1101 |
return DisambiguatePossibleEpochNanoseconds( |
| 1102 |
cx, possibleEpochNs, timeZone, isoDateTime, disambiguation, result); |
| 1103 |
} |
| 1104 |
|
| 1105 |
bool js::temporal::WrapTimeZoneValueObject( |
| 1106 |
JSContext* cx, MutableHandle<TimeZoneObject*> timeZone) { |
| 1107 |
// Handle the common case when |timeZone| is from the current compartment. |
| 1108 |
if (MOZ_LIKELY(timeZone->compartment() == cx->compartment())) { |
| 1109 |
return true; |
| 1110 |
} |
| 1111 |
|
| 1112 |
if (timeZone->isOffset()) { |
| 1113 |
auto* obj = CreateTimeZoneObject(cx, timeZone->offsetMinutes()); |
| 1114 |
if (!obj) { |
| 1115 |
return false; |
| 1116 |
} |
| 1117 |
|
| 1118 |
timeZone.set(obj); |
| 1119 |
return true; |
| 1120 |
} |
| 1121 |
|
| 1122 |
Rooted<JSString*> identifier(cx, timeZone->identifier()); |
| 1123 |
if (!cx->compartment()->wrap(cx, &identifier)) { |
| 1124 |
return false; |
| 1125 |
} |
| 1126 |
|
| 1127 |
Rooted<JSString*> primaryIdentifier(cx, timeZone->primaryIdentifier()); |
| 1128 |
if (!cx->compartment()->wrap(cx, &primaryIdentifier)) { |
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
|
| 1132 |
Rooted<JSLinearString*> identifierLinear(cx, identifier->ensureLinear(cx)); |
| 1133 |
if (!identifierLinear) { |
| 1134 |
return false; |
| 1135 |
} |
| 1136 |
|
| 1137 |
Rooted<JSLinearString*> primaryIdentifierLinear( |
| 1138 |
cx, primaryIdentifier->ensureLinear(cx)); |
| 1139 |
if (!primaryIdentifierLinear) { |
| 1140 |
return false; |
| 1141 |
} |
| 1142 |
|
| 1143 |
auto* obj = |
| 1144 |
CreateTimeZoneObject(cx, identifierLinear, primaryIdentifierLinear); |
| 1145 |
if (!obj) { |
| 1146 |
return false; |
| 1147 |
} |
| 1148 |
|
| 1149 |
timeZone.set(obj); |
| 1150 |
return true; |
| 1151 |
} |
| 1152 |
|
| 1153 |
void js::temporal::TimeZoneObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 1154 |
MOZ_ASSERT(gcx->onMainThread()); |
| 1155 |
|
| 1156 |
if (auto* timeZone = obj->as<TimeZoneObject>().getTimeZone()) { |
| 1157 |
intl::RemoveICUCellMemory(gcx, obj, EstimatedMemoryUse); |
| 1158 |
delete timeZone; |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
const JSClassOps TimeZoneObject::classOps_ = { |
| 1163 |
nullptr, // addProperty |
| 1164 |
nullptr, // delProperty |
| 1165 |
nullptr, // enumerate |
| 1166 |
nullptr, // newEnumerate |
| 1167 |
nullptr, // resolve |
| 1168 |
nullptr, // mayResolve |
| 1169 |
TimeZoneObject::finalize, // finalize |
| 1170 |
nullptr, // call |
| 1171 |
nullptr, // construct |
| 1172 |
nullptr, // trace |
| 1173 |
}; |
| 1174 |
|
| 1175 |
const JSClass TimeZoneObject::class_ = { |
| 1176 |
"Temporal.TimeZone", |
| 1177 |
JSCLASS_HAS_RESERVED_SLOTS(TimeZoneObject::SLOT_COUNT) | |
| 1178 |
JSCLASS_FOREGROUND_FINALIZE, |
| 1179 |
&TimeZoneObject::classOps_, |
| 1180 |
}; |