| 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/ZonedDateTime.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
#include "mozilla/EnumSet.h" |
| 11 |
#include "mozilla/Maybe.h" |
| 12 |
|
| 13 |
#include <algorithm> |
| 14 |
#include <cstdlib> |
| 15 |
#include <utility> |
| 16 |
|
| 17 |
#include "jspubtd.h" |
| 18 |
#include "NamespaceImports.h" |
| 19 |
|
| 20 |
#include "builtin/intl/DateTimeFormat.h" |
| 21 |
#include "builtin/temporal/Calendar.h" |
| 22 |
#include "builtin/temporal/CalendarFields.h" |
| 23 |
#include "builtin/temporal/Duration.h" |
| 24 |
#include "builtin/temporal/Instant.h" |
| 25 |
#include "builtin/temporal/Int128.h" |
| 26 |
#include "builtin/temporal/PlainDate.h" |
| 27 |
#include "builtin/temporal/PlainDateTime.h" |
| 28 |
#include "builtin/temporal/PlainMonthDay.h" |
| 29 |
#include "builtin/temporal/PlainTime.h" |
| 30 |
#include "builtin/temporal/PlainYearMonth.h" |
| 31 |
#include "builtin/temporal/Temporal.h" |
| 32 |
#include "builtin/temporal/TemporalParser.h" |
| 33 |
#include "builtin/temporal/TemporalRoundingMode.h" |
| 34 |
#include "builtin/temporal/TemporalTypes.h" |
| 35 |
#include "builtin/temporal/TemporalUnit.h" |
| 36 |
#include "builtin/temporal/TimeZone.h" |
| 37 |
#include "builtin/temporal/ToString.h" |
| 38 |
#include "gc/AllocKind.h" |
| 39 |
#include "gc/Barrier.h" |
| 40 |
#include "gc/GCEnum.h" |
| 41 |
#include "js/CallArgs.h" |
| 42 |
#include "js/CallNonGenericMethod.h" |
| 43 |
#include "js/Class.h" |
| 44 |
#include "js/ErrorReport.h" |
| 45 |
#include "js/friend/ErrorMessages.h" |
| 46 |
#include "js/Printer.h" |
| 47 |
#include "js/PropertyDescriptor.h" |
| 48 |
#include "js/PropertySpec.h" |
| 49 |
#include "js/RootingAPI.h" |
| 50 |
#include "js/Value.h" |
| 51 |
#include "vm/BigIntType.h" |
| 52 |
#include "vm/BytecodeUtil.h" |
| 53 |
#include "vm/GlobalObject.h" |
| 54 |
#include "vm/JSAtomState.h" |
| 55 |
#include "vm/JSContext.h" |
| 56 |
#include "vm/JSObject.h" |
| 57 |
#include "vm/PlainObject.h" |
| 58 |
#include "vm/StringType.h" |
| 59 |
|
| 60 |
#include "vm/JSObject-inl.h" |
| 61 |
#include "vm/NativeObject-inl.h" |
| 62 |
|
| 63 |
using namespace js; |
| 64 |
using namespace js::temporal; |
| 65 |
|
| 66 |
static inline bool IsZonedDateTime(Handle<Value> v) { |
| 67 |
return v.isObject() && v.toObject().is<ZonedDateTimeObject>(); |
| 68 |
} |
| 69 |
|
| 70 |
// Returns |RoundNumberToIncrement(offsetNanoseconds, 60 × 10^9, "halfExpand")|. |
| 71 |
static int64_t RoundNanosecondsToMinutesIncrement(int64_t offsetNanoseconds) { |
| 72 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 73 |
|
| 74 |
constexpr int64_t increment = ToNanoseconds(TemporalUnit::Minute); |
| 75 |
|
| 76 |
int64_t quotient = offsetNanoseconds / increment; |
| 77 |
int64_t remainder = offsetNanoseconds % increment; |
| 78 |
if (std::abs(remainder * 2) >= increment) { |
| 79 |
quotient += (offsetNanoseconds > 0 ? 1 : -1); |
| 80 |
} |
| 81 |
return quotient * increment; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* InterpretISODateTimeOffset ( isoDate, time, offsetBehaviour, |
| 86 |
* offsetNanoseconds, timeZone, disambiguation, offsetOption, matchBehaviour ) |
| 87 |
*/ |
| 88 |
bool js::temporal::InterpretISODateTimeOffset( |
| 89 |
JSContext* cx, const ISODateTime& dateTime, OffsetBehaviour offsetBehaviour, |
| 90 |
int64_t offsetNanoseconds, Handle<TimeZoneValue> timeZone, |
| 91 |
TemporalDisambiguation disambiguation, TemporalOffset offsetOption, |
| 92 |
MatchBehaviour matchBehaviour, EpochNanoseconds* result) { |
| 93 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 94 |
MOZ_ASSERT(IsValidISODateTime(dateTime)); |
| 95 |
|
| 96 |
// FIXME: spec issue - avoid calling with date-time outside of limits |
| 97 |
// https://github.com/tc39/proposal-temporal/pull/3014 |
| 98 |
if (!ISODateTimeWithinLimits(dateTime)) { |
| 99 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 100 |
JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID); |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// Steps 1-2. (Not applicable in our implementation.) |
| 105 |
|
| 106 |
// Step 3. |
| 107 |
if (offsetBehaviour == OffsetBehaviour::Wall || |
| 108 |
(offsetBehaviour == OffsetBehaviour::Option && |
| 109 |
offsetOption == TemporalOffset::Ignore)) { |
| 110 |
// Steps 3.a-b. |
| 111 |
return GetEpochNanosecondsFor(cx, timeZone, dateTime, disambiguation, |
| 112 |
result); |
| 113 |
} |
| 114 |
|
| 115 |
// Step 4. |
| 116 |
if (offsetBehaviour == OffsetBehaviour::Exact || |
| 117 |
(offsetBehaviour == OffsetBehaviour::Option && |
| 118 |
offsetOption == TemporalOffset::Use)) { |
| 119 |
// Step 4.a. |
| 120 |
auto epochNanoseconds = GetUTCEpochNanoseconds(dateTime) - |
| 121 |
EpochDuration::fromNanoseconds(offsetNanoseconds); |
| 122 |
|
| 123 |
// Step 4.b. |
| 124 |
if (!IsValidEpochNanoseconds(epochNanoseconds)) { |
| 125 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 126 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
// Step 4.c. |
| 131 |
*result = epochNanoseconds; |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
// Step 5. |
| 136 |
MOZ_ASSERT(offsetBehaviour == OffsetBehaviour::Option); |
| 137 |
|
| 138 |
// Step 6. |
| 139 |
MOZ_ASSERT(offsetOption == TemporalOffset::Prefer || |
| 140 |
offsetOption == TemporalOffset::Reject); |
| 141 |
|
| 142 |
// Step 7. |
| 143 |
PossibleEpochNanoseconds possibleEpochNs; |
| 144 |
if (!GetPossibleEpochNanoseconds(cx, timeZone, dateTime, &possibleEpochNs)) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
// Step 8.a. |
| 149 |
for (const auto& candidate : possibleEpochNs) { |
| 150 |
// Step 8.a.i. |
| 151 |
int64_t candidateNanoseconds; |
| 152 |
if (!GetOffsetNanosecondsFor(cx, timeZone, candidate, |
| 153 |
&candidateNanoseconds)) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
MOZ_ASSERT(std::abs(candidateNanoseconds) < |
| 157 |
ToNanoseconds(TemporalUnit::Day)); |
| 158 |
|
| 159 |
// Step 8.a.ii. |
| 160 |
if (candidateNanoseconds == offsetNanoseconds) { |
| 161 |
*result = candidate; |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
// Step 8.a.iii. |
| 166 |
if (matchBehaviour == MatchBehaviour::MatchMinutes) { |
| 167 |
// Step 8.a.iii.1. |
| 168 |
int64_t roundedCandidateNanoseconds = |
| 169 |
RoundNanosecondsToMinutesIncrement(candidateNanoseconds); |
| 170 |
|
| 171 |
// Step 8.a.iii.2. |
| 172 |
if (roundedCandidateNanoseconds == offsetNanoseconds) { |
| 173 |
// Step 8.a.iii.2.a. |
| 174 |
*result = candidate; |
| 175 |
return true; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// Step 9. |
| 181 |
if (offsetOption == TemporalOffset::Reject) { |
| 182 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 183 |
JSMSG_TEMPORAL_ZONED_DATE_TIME_NO_TIME_FOUND); |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
// Step 10. |
| 188 |
return DisambiguatePossibleEpochNanoseconds(cx, possibleEpochNs, timeZone, |
| 189 |
dateTime, disambiguation, result); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* InterpretISODateTimeOffset ( isoDate, time, offsetBehaviour, |
| 194 |
* offsetNanoseconds, timeZone, disambiguation, offsetOption, matchBehaviour ) |
| 195 |
*/ |
| 196 |
bool js::temporal::InterpretISODateTimeOffset( |
| 197 |
JSContext* cx, const ISODate& isoDate, OffsetBehaviour offsetBehaviour, |
| 198 |
int64_t offsetNanoseconds, Handle<TimeZoneValue> timeZone, |
| 199 |
TemporalDisambiguation disambiguation, TemporalOffset offsetOption, |
| 200 |
MatchBehaviour matchBehaviour, EpochNanoseconds* result) { |
| 201 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 202 |
MOZ_ASSERT(IsValidISODate(isoDate)); |
| 203 |
|
| 204 |
// Step 1. (Not applicable in our implementation.) |
| 205 |
|
| 206 |
// Step 2.a. |
| 207 |
MOZ_ASSERT(offsetBehaviour == OffsetBehaviour::Wall); |
| 208 |
|
| 209 |
// Step 2.b. |
| 210 |
MOZ_ASSERT(offsetNanoseconds == 0); |
| 211 |
|
| 212 |
// Step 2.c. |
| 213 |
return GetStartOfDay(cx, timeZone, isoDate, result); |
| 214 |
} |
| 215 |
|
| 216 |
struct ZonedDateTimeOptions { |
| 217 |
TemporalDisambiguation disambiguation = TemporalDisambiguation::Compatible; |
| 218 |
TemporalOffset offset = TemporalOffset::Reject; |
| 219 |
TemporalOverflow overflow = TemporalOverflow::Constrain; |
| 220 |
}; |
| 221 |
|
| 222 |
/** |
| 223 |
* ToTemporalZonedDateTime ( item [ , options ] ) |
| 224 |
*/ |
| 225 |
static bool ToTemporalZonedDateTimeOptions(JSContext* cx, Handle<Value> options, |
| 226 |
ZonedDateTimeOptions* result) { |
| 227 |
if (options.isUndefined()) { |
| 228 |
*result = {}; |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
// NOTE: |options| are only passed from `Temporal.ZonedDateTime.from`. |
| 233 |
|
| 234 |
Rooted<JSObject*> resolvedOptions( |
| 235 |
cx, RequireObjectArg(cx, "options", "from", options)); |
| 236 |
if (!resolvedOptions) { |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
auto disambiguation = TemporalDisambiguation::Compatible; |
| 241 |
if (!GetTemporalDisambiguationOption(cx, resolvedOptions, &disambiguation)) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
auto offset = TemporalOffset::Reject; |
| 246 |
if (!GetTemporalOffsetOption(cx, resolvedOptions, &offset)) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
auto overflow = TemporalOverflow::Constrain; |
| 251 |
if (!GetTemporalOverflowOption(cx, resolvedOptions, &overflow)) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
*result = {disambiguation, offset, overflow}; |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* ToTemporalZonedDateTime ( item [ , options ] ) |
| 261 |
*/ |
| 262 |
static bool ToTemporalZonedDateTime(JSContext* cx, Handle<JSObject*> item, |
| 263 |
Handle<Value> options, |
| 264 |
MutableHandle<ZonedDateTime> result) { |
| 265 |
// Step 1. (Not applicable in our implementation.) |
| 266 |
|
| 267 |
// Step 2. |
| 268 |
auto offsetBehaviour = OffsetBehaviour::Option; |
| 269 |
|
| 270 |
// Step 3. |
| 271 |
auto matchBehaviour = MatchBehaviour::MatchExactly; |
| 272 |
|
| 273 |
// Step 4.a. |
| 274 |
if (auto* zonedDateTime = item->maybeUnwrapIf<ZonedDateTimeObject>()) { |
| 275 |
auto epochNs = zonedDateTime->epochNanoseconds(); |
| 276 |
Rooted<TimeZoneValue> timeZone(cx, zonedDateTime->timeZone()); |
| 277 |
Rooted<CalendarValue> calendar(cx, zonedDateTime->calendar()); |
| 278 |
|
| 279 |
if (!timeZone.wrap(cx)) { |
| 280 |
return false; |
| 281 |
} |
| 282 |
if (!calendar.wrap(cx)) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
// Steps 4.a.i-v. |
| 287 |
ZonedDateTimeOptions ignoredOptions; |
| 288 |
if (!ToTemporalZonedDateTimeOptions(cx, options, &ignoredOptions)) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
// Step 4.a.vi. |
| 293 |
result.set(ZonedDateTime{epochNs, timeZone, calendar}); |
| 294 |
return true; |
| 295 |
} |
| 296 |
|
| 297 |
// Step 4.b. |
| 298 |
Rooted<CalendarValue> calendar(cx); |
| 299 |
if (!GetTemporalCalendarWithISODefault(cx, item, &calendar)) { |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
// Step 4.c. |
| 304 |
Rooted<CalendarFields> fields(cx); |
| 305 |
if (!PrepareCalendarFields(cx, calendar, item, |
| 306 |
{ |
| 307 |
CalendarField::Year, |
| 308 |
CalendarField::Month, |
| 309 |
CalendarField::MonthCode, |
| 310 |
CalendarField::Day, |
| 311 |
CalendarField::Hour, |
| 312 |
CalendarField::Minute, |
| 313 |
CalendarField::Second, |
| 314 |
CalendarField::Millisecond, |
| 315 |
CalendarField::Microsecond, |
| 316 |
CalendarField::Nanosecond, |
| 317 |
CalendarField::Offset, |
| 318 |
CalendarField::TimeZone, |
| 319 |
}, |
| 320 |
{CalendarField::TimeZone}, &fields)) { |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
// Step 4.d. |
| 325 |
auto timeZone = fields.timeZone(); |
| 326 |
|
| 327 |
// Step 4.e. |
| 328 |
auto offsetString = fields.offset(); |
| 329 |
|
| 330 |
// Step 4.f. |
| 331 |
if (!fields.has(CalendarField::Offset)) { |
| 332 |
offsetBehaviour = OffsetBehaviour::Wall; |
| 333 |
} |
| 334 |
|
| 335 |
// Steps 4.g-j. |
| 336 |
ZonedDateTimeOptions resolvedOptions; |
| 337 |
if (!ToTemporalZonedDateTimeOptions(cx, options, &resolvedOptions)) { |
| 338 |
return false; |
| 339 |
} |
| 340 |
auto [disambiguation, offsetOption, overflow] = resolvedOptions; |
| 341 |
|
| 342 |
// Step 4.k. |
| 343 |
ISODateTime dateTime; |
| 344 |
if (!InterpretTemporalDateTimeFields(cx, calendar, fields, overflow, |
| 345 |
&dateTime)) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
// Step 6. |
| 350 |
int64_t offsetNanoseconds = 0; |
| 351 |
|
| 352 |
// Step 7. |
| 353 |
if (offsetBehaviour == OffsetBehaviour::Option) { |
| 354 |
offsetNanoseconds = int64_t(offsetString); |
| 355 |
} |
| 356 |
|
| 357 |
// Step 8. |
| 358 |
EpochNanoseconds epochNanoseconds; |
| 359 |
if (!InterpretISODateTimeOffset( |
| 360 |
cx, dateTime, offsetBehaviour, offsetNanoseconds, timeZone, |
| 361 |
disambiguation, offsetOption, matchBehaviour, &epochNanoseconds)) { |
| 362 |
return false; |
| 363 |
} |
| 364 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 365 |
|
| 366 |
// Step 9. |
| 367 |
result.set(ZonedDateTime{epochNanoseconds, timeZone, calendar}); |
| 368 |
return true; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* ToTemporalZonedDateTime ( item [ , options ] ) |
| 373 |
*/ |
| 374 |
static bool ToTemporalZonedDateTime(JSContext* cx, Handle<Value> item, |
| 375 |
Handle<Value> options, |
| 376 |
MutableHandle<ZonedDateTime> result) { |
| 377 |
// Step 1. (Not applicable in our implementation.) |
| 378 |
|
| 379 |
// Step 2. |
| 380 |
auto offsetBehaviour = OffsetBehaviour::Option; |
| 381 |
|
| 382 |
// Step 3. |
| 383 |
auto matchBehaviour = MatchBehaviour::MatchExactly; |
| 384 |
|
| 385 |
// Step 4. |
| 386 |
if (item.isObject()) { |
| 387 |
Rooted<JSObject*> itemObj(cx, &item.toObject()); |
| 388 |
return ToTemporalZonedDateTime(cx, itemObj, options, result); |
| 389 |
} |
| 390 |
|
| 391 |
// Step 5.a. |
| 392 |
if (!item.isString()) { |
| 393 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, item, |
| 394 |
nullptr, "not a string"); |
| 395 |
return false; |
| 396 |
} |
| 397 |
Rooted<JSString*> string(cx, item.toString()); |
| 398 |
|
| 399 |
// Case 1: 19700101Z[+02:00] |
| 400 |
// { [[Z]]: true, [[OffsetString]]: undefined, [[Name]]: "+02:00" } |
| 401 |
// |
| 402 |
// Case 2: 19700101+00:00[+02:00] |
| 403 |
// { [[Z]]: false, [[OffsetString]]: "+00:00", [[Name]]: "+02:00" } |
| 404 |
// |
| 405 |
// Case 3: 19700101[+02:00] |
| 406 |
// { [[Z]]: false, [[OffsetString]]: undefined, [[Name]]: "+02:00" } |
| 407 |
// |
| 408 |
// Case 4: 19700101Z[Europe/Berlin] |
| 409 |
// { [[Z]]: true, [[OffsetString]]: undefined, [[Name]]: "Europe/Berlin" } |
| 410 |
// |
| 411 |
// Case 5: 19700101+00:00[Europe/Berlin] |
| 412 |
// { [[Z]]: false, [[OffsetString]]: "+00:00", [[Name]]: "Europe/Berlin" } |
| 413 |
// |
| 414 |
// Case 6: 19700101[Europe/Berlin] |
| 415 |
// { [[Z]]: false, [[OffsetString]]: undefined, [[Name]]: "Europe/Berlin" } |
| 416 |
|
| 417 |
// Steps 5.b-c. |
| 418 |
Rooted<ParsedZonedDateTime> parsed(cx); |
| 419 |
if (!ParseTemporalZonedDateTimeString(cx, string, &parsed)) { |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
// Step 5.d. |
| 424 |
MOZ_ASSERT(parsed.timeZoneAnnotation()); |
| 425 |
|
| 426 |
// Step 5.e. |
| 427 |
Rooted<TimeZoneValue> timeZone(cx); |
| 428 |
if (!ToTemporalTimeZone(cx, parsed.timeZoneAnnotation(), &timeZone)) { |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
// Step 5.f. (Not applicable in our implementation.) |
| 433 |
|
| 434 |
// Step 5.g. |
| 435 |
if (parsed.isUTC()) { |
| 436 |
offsetBehaviour = OffsetBehaviour::Exact; |
| 437 |
} |
| 438 |
|
| 439 |
// Step 5.h. |
| 440 |
else if (!parsed.hasOffset()) { |
| 441 |
offsetBehaviour = OffsetBehaviour::Wall; |
| 442 |
} |
| 443 |
|
| 444 |
// Steps 5.i-k. |
| 445 |
Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601)); |
| 446 |
if (parsed.calendar()) { |
| 447 |
if (!CanonicalizeCalendar(cx, parsed.calendar(), &calendar)) { |
| 448 |
return false; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
// Step 5.l. |
| 453 |
matchBehaviour = MatchBehaviour::MatchMinutes; |
| 454 |
|
| 455 |
// Steps 5.m-p. |
| 456 |
ZonedDateTimeOptions resolvedOptions; |
| 457 |
if (!ToTemporalZonedDateTimeOptions(cx, options, &resolvedOptions)) { |
| 458 |
return false; |
| 459 |
} |
| 460 |
auto [disambiguation, offsetOption, overflow] = resolvedOptions; |
| 461 |
|
| 462 |
// Steps 5.q-r. (Not applicable in our implementation.) |
| 463 |
|
| 464 |
// Step 6. |
| 465 |
int64_t offsetNanoseconds = 0; |
| 466 |
|
| 467 |
// Step 7. |
| 468 |
if (offsetBehaviour == OffsetBehaviour::Option) { |
| 469 |
MOZ_ASSERT(parsed.hasOffset()); |
| 470 |
offsetNanoseconds = parsed.timeZoneOffset(); |
| 471 |
} |
| 472 |
|
| 473 |
// Step 8. |
| 474 |
EpochNanoseconds epochNanoseconds; |
| 475 |
if (parsed.isStartOfDay()) { |
| 476 |
if (!InterpretISODateTimeOffset(cx, parsed.dateTime().date, offsetBehaviour, |
| 477 |
offsetNanoseconds, timeZone, disambiguation, |
| 478 |
offsetOption, matchBehaviour, |
| 479 |
&epochNanoseconds)) { |
| 480 |
return false; |
| 481 |
} |
| 482 |
} else { |
| 483 |
if (!InterpretISODateTimeOffset( |
| 484 |
cx, parsed.dateTime(), offsetBehaviour, offsetNanoseconds, timeZone, |
| 485 |
disambiguation, offsetOption, matchBehaviour, &epochNanoseconds)) { |
| 486 |
return false; |
| 487 |
} |
| 488 |
} |
| 489 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 490 |
|
| 491 |
// Step 9. |
| 492 |
result.set(ZonedDateTime{epochNanoseconds, timeZone, calendar}); |
| 493 |
return true; |
| 494 |
} |
| 495 |
|
| 496 |
/** |
| 497 |
* ToTemporalZonedDateTime ( item [ , options ] ) |
| 498 |
*/ |
| 499 |
static bool ToTemporalZonedDateTime(JSContext* cx, Handle<Value> item, |
| 500 |
MutableHandle<ZonedDateTime> result) { |
| 501 |
return ToTemporalZonedDateTime(cx, item, UndefinedHandleValue, result); |
| 502 |
} |
| 503 |
|
| 504 |
/** |
| 505 |
* CreateTemporalZonedDateTime ( epochNanoseconds, timeZone, calendar [ , |
| 506 |
* newTarget ] ) |
| 507 |
*/ |
| 508 |
static ZonedDateTimeObject* CreateTemporalZonedDateTime( |
| 509 |
JSContext* cx, const CallArgs& args, Handle<BigInt*> epochNanoseconds, |
| 510 |
Handle<TimeZoneValue> timeZone, Handle<CalendarValue> calendar) { |
| 511 |
// Step 1. |
| 512 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 513 |
|
| 514 |
// Steps 3-4. |
| 515 |
Rooted<JSObject*> proto(cx); |
| 516 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_ZonedDateTime, |
| 517 |
&proto)) { |
| 518 |
return nullptr; |
| 519 |
} |
| 520 |
|
| 521 |
auto* object = NewObjectWithClassProto<ZonedDateTimeObject>(cx, proto); |
| 522 |
if (!object) { |
| 523 |
return nullptr; |
| 524 |
} |
| 525 |
|
| 526 |
// Step 4. |
| 527 |
auto epochNs = ToEpochNanoseconds(epochNanoseconds); |
| 528 |
object->setFixedSlot(ZonedDateTimeObject::SECONDS_SLOT, |
| 529 |
NumberValue(epochNs.seconds)); |
| 530 |
object->setFixedSlot(ZonedDateTimeObject::NANOSECONDS_SLOT, |
| 531 |
Int32Value(epochNs.nanoseconds)); |
| 532 |
|
| 533 |
// Step 5. |
| 534 |
object->setFixedSlot(ZonedDateTimeObject::TIMEZONE_SLOT, |
| 535 |
timeZone.toSlotValue()); |
| 536 |
|
| 537 |
// Step 6. |
| 538 |
object->setFixedSlot(ZonedDateTimeObject::CALENDAR_SLOT, |
| 539 |
calendar.toSlotValue()); |
| 540 |
|
| 541 |
// Step 7. |
| 542 |
return object; |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* CreateTemporalZonedDateTime ( epochNanoseconds, timeZone, calendar [ , |
| 547 |
* newTarget ] ) |
| 548 |
*/ |
| 549 |
ZonedDateTimeObject* js::temporal::CreateTemporalZonedDateTime( |
| 550 |
JSContext* cx, const EpochNanoseconds& epochNanoseconds, |
| 551 |
Handle<TimeZoneValue> timeZone, Handle<CalendarValue> calendar) { |
| 552 |
// Step 1. |
| 553 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 554 |
|
| 555 |
// Steps 2-3. |
| 556 |
auto* object = NewBuiltinClassInstance<ZonedDateTimeObject>(cx); |
| 557 |
if (!object) { |
| 558 |
return nullptr; |
| 559 |
} |
| 560 |
|
| 561 |
// Step 4. |
| 562 |
object->setFixedSlot(ZonedDateTimeObject::SECONDS_SLOT, |
| 563 |
NumberValue(epochNanoseconds.seconds)); |
| 564 |
object->setFixedSlot(ZonedDateTimeObject::NANOSECONDS_SLOT, |
| 565 |
Int32Value(epochNanoseconds.nanoseconds)); |
| 566 |
|
| 567 |
// Step 5. |
| 568 |
object->setFixedSlot(ZonedDateTimeObject::TIMEZONE_SLOT, |
| 569 |
timeZone.toSlotValue()); |
| 570 |
|
| 571 |
// Step 6. |
| 572 |
object->setFixedSlot(ZonedDateTimeObject::CALENDAR_SLOT, |
| 573 |
calendar.toSlotValue()); |
| 574 |
|
| 575 |
// Step 7. |
| 576 |
return object; |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* CreateTemporalZonedDateTime ( epochNanoseconds, timeZone, calendar [ , |
| 581 |
* newTarget ] ) |
| 582 |
*/ |
| 583 |
static auto* CreateTemporalZonedDateTime(JSContext* cx, |
| 584 |
Handle<ZonedDateTime> zonedDateTime) { |
| 585 |
return CreateTemporalZonedDateTime(cx, zonedDateTime.epochNanoseconds(), |
| 586 |
zonedDateTime.timeZone(), |
| 587 |
zonedDateTime.calendar()); |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* AddZonedDateTime ( epochNanoseconds, timeZone, calendar, duration, overflow ) |
| 592 |
*/ |
| 593 |
static bool AddZonedDateTime(JSContext* cx, Handle<ZonedDateTime> zonedDateTime, |
| 594 |
const InternalDuration& duration, |
| 595 |
TemporalOverflow overflow, |
| 596 |
EpochNanoseconds* result) { |
| 597 |
MOZ_ASSERT(IsValidDuration(duration)); |
| 598 |
|
| 599 |
// Step 1. |
| 600 |
if (duration.date == DateDuration{}) { |
| 601 |
// Step 1.a. |
| 602 |
return AddInstant(cx, zonedDateTime.epochNanoseconds(), duration.time, |
| 603 |
result); |
| 604 |
} |
| 605 |
|
| 606 |
// Step 2. |
| 607 |
ISODateTime isoDateTime; |
| 608 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 609 |
zonedDateTime.epochNanoseconds(), &isoDateTime)) { |
| 610 |
return false; |
| 611 |
} |
| 612 |
|
| 613 |
// Step 3. |
| 614 |
ISODate addedDate; |
| 615 |
if (!CalendarDateAdd(cx, zonedDateTime.calendar(), isoDateTime.date, |
| 616 |
duration.date, overflow, &addedDate)) { |
| 617 |
return false; |
| 618 |
} |
| 619 |
|
| 620 |
// Step 4. |
| 621 |
auto intermediateDateTime = ISODateTime{addedDate, isoDateTime.time}; |
| 622 |
|
| 623 |
// Step 5. |
| 624 |
if (!ISODateTimeWithinLimits(intermediateDateTime)) { |
| 625 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 626 |
JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID); |
| 627 |
return false; |
| 628 |
} |
| 629 |
|
| 630 |
// Step 6. |
| 631 |
EpochNanoseconds intermediateNs; |
| 632 |
if (!GetEpochNanosecondsFor( |
| 633 |
cx, zonedDateTime.timeZone(), intermediateDateTime, |
| 634 |
TemporalDisambiguation::Compatible, &intermediateNs)) { |
| 635 |
return false; |
| 636 |
} |
| 637 |
|
| 638 |
// Step 7. |
| 639 |
return AddInstant(cx, intermediateNs, duration.time, result); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* AddZonedDateTime ( epochNanoseconds, timeZone, calendar, duration, overflow ) |
| 644 |
*/ |
| 645 |
bool js::temporal::AddZonedDateTime(JSContext* cx, |
| 646 |
Handle<ZonedDateTime> zonedDateTime, |
| 647 |
const InternalDuration& duration, |
| 648 |
EpochNanoseconds* result) { |
| 649 |
return ::AddZonedDateTime(cx, zonedDateTime, duration, |
| 650 |
TemporalOverflow::Constrain, result); |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* DifferenceZonedDateTime ( ns1, ns2, timeZone, calendar, largestUnit ) |
| 655 |
*/ |
| 656 |
static bool DifferenceZonedDateTime(JSContext* cx, const EpochNanoseconds& ns1, |
| 657 |
const EpochNanoseconds& ns2, |
| 658 |
Handle<TimeZoneValue> timeZone, |
| 659 |
Handle<CalendarValue> calendar, |
| 660 |
TemporalUnit largestUnit, |
| 661 |
InternalDuration* result) { |
| 662 |
MOZ_ASSERT(IsValidEpochNanoseconds(ns1)); |
| 663 |
MOZ_ASSERT(IsValidEpochNanoseconds(ns2)); |
| 664 |
|
| 665 |
// Steps 1. |
| 666 |
if (ns1 == ns2) { |
| 667 |
*result = InternalDuration{{}, {}}; |
| 668 |
return true; |
| 669 |
} |
| 670 |
|
| 671 |
// Step 2. |
| 672 |
ISODateTime startDateTime; |
| 673 |
if (!GetISODateTimeFor(cx, timeZone, ns1, &startDateTime)) { |
| 674 |
return false; |
| 675 |
} |
| 676 |
|
| 677 |
// Steps 2-3. |
| 678 |
ISODateTime endDateTime; |
| 679 |
if (!GetISODateTimeFor(cx, timeZone, ns2, &endDateTime)) { |
| 680 |
return false; |
| 681 |
} |
| 682 |
|
| 683 |
// Step 4. |
| 684 |
int32_t sign = (ns2 - ns1 < EpochDuration{}) ? -1 : 1; |
| 685 |
|
| 686 |
// Step 5. |
| 687 |
int32_t maxDayCorrection = 1 + (sign > 0); |
| 688 |
|
| 689 |
// Step 6. |
| 690 |
int32_t dayCorrection = 0; |
| 691 |
|
| 692 |
// Step 7. |
| 693 |
auto timeDuration = DifferenceTime(startDateTime.time, endDateTime.time); |
| 694 |
|
| 695 |
// Step 8. |
| 696 |
if (TimeDurationSign(timeDuration) == -sign) { |
| 697 |
dayCorrection += 1; |
| 698 |
} |
| 699 |
|
| 700 |
// Steps 9-10. |
| 701 |
while (dayCorrection <= maxDayCorrection) { |
| 702 |
// Step 10.a. |
| 703 |
auto intermediateDate = |
| 704 |
BalanceISODate(endDateTime.date, -dayCorrection * sign); |
| 705 |
|
| 706 |
// Step 10.b. |
| 707 |
auto intermediateDateTime = |
| 708 |
ISODateTime{intermediateDate, startDateTime.time}; |
| 709 |
if (!ISODateTimeWithinLimits(intermediateDateTime)) { |
| 710 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 711 |
JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID); |
| 712 |
return false; |
| 713 |
} |
| 714 |
|
| 715 |
// Step 10.c. |
| 716 |
EpochNanoseconds intermediateNs; |
| 717 |
if (!GetEpochNanosecondsFor(cx, timeZone, intermediateDateTime, |
| 718 |
TemporalDisambiguation::Compatible, |
| 719 |
&intermediateNs)) { |
| 720 |
return false; |
| 721 |
} |
| 722 |
|
| 723 |
// Step 10.d. |
| 724 |
auto timeDuration = |
| 725 |
TimeDurationFromEpochNanosecondsDifference(ns2, intermediateNs); |
| 726 |
|
| 727 |
// Step 10.e. |
| 728 |
int32_t timeSign = TimeDurationSign(timeDuration); |
| 729 |
|
| 730 |
// Step 10.f. |
| 731 |
if (sign != -timeSign) { |
| 732 |
// Step 12. |
| 733 |
auto dateLargestUnit = std::min(largestUnit, TemporalUnit::Day); |
| 734 |
|
| 735 |
// Step 13. |
| 736 |
DateDuration dateDifference; |
| 737 |
if (!CalendarDateUntil(cx, calendar, startDateTime.date, intermediateDate, |
| 738 |
dateLargestUnit, &dateDifference)) { |
| 739 |
return false; |
| 740 |
} |
| 741 |
|
| 742 |
// Step 14. |
| 743 |
MOZ_ASSERT(DateDurationSign(dateDifference) * |
| 744 |
TimeDurationSign(timeDuration) >= |
| 745 |
0); |
| 746 |
*result = {dateDifference, timeDuration}; |
| 747 |
return true; |
| 748 |
} |
| 749 |
|
| 750 |
// Step 10.g. |
| 751 |
dayCorrection += 1; |
| 752 |
} |
| 753 |
|
| 754 |
// Step 11. |
| 755 |
JS_ReportErrorNumberASCII( |
| 756 |
cx, GetErrorMessage, nullptr, |
| 757 |
JSMSG_TEMPORAL_ZONED_DATE_TIME_INCONSISTENT_INSTANT); |
| 758 |
return false; |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* DifferenceZonedDateTimeWithRounding ( ns1, ns2, timeZone, calendar, |
| 763 |
* largestUnit, roundingIncrement, smallestUnit, roundingMode ) |
| 764 |
*/ |
| 765 |
bool js::temporal::DifferenceZonedDateTimeWithRounding( |
| 766 |
JSContext* cx, JS::Handle<ZonedDateTime> zonedDateTime, |
| 767 |
const EpochNanoseconds& ns2, const DifferenceSettings& settings, |
| 768 |
InternalDuration* result) { |
| 769 |
MOZ_ASSERT(IsValidEpochNanoseconds(ns2)); |
| 770 |
MOZ_ASSERT(settings.smallestUnit >= settings.largestUnit); |
| 771 |
|
| 772 |
const auto& ns1 = zonedDateTime.epochNanoseconds(); |
| 773 |
auto timeZone = zonedDateTime.timeZone(); |
| 774 |
auto calendar = zonedDateTime.calendar(); |
| 775 |
|
| 776 |
// Step 1. |
| 777 |
if (settings.largestUnit > TemporalUnit::Day) { |
| 778 |
// Step 1.a. |
| 779 |
auto difference = |
| 780 |
DifferenceInstant(ns1, ns2, settings.roundingIncrement, |
| 781 |
settings.smallestUnit, settings.roundingMode); |
| 782 |
*result = InternalDuration{{}, difference}; |
| 783 |
return true; |
| 784 |
} |
| 785 |
|
| 786 |
// Step 2. |
| 787 |
InternalDuration difference; |
| 788 |
if (!DifferenceZonedDateTime(cx, ns1, ns2, timeZone, calendar, |
| 789 |
settings.largestUnit, &difference)) { |
| 790 |
return false; |
| 791 |
} |
| 792 |
|
| 793 |
// Step 3. |
| 794 |
if (settings.smallestUnit == TemporalUnit::Nanosecond && |
| 795 |
settings.roundingIncrement == Increment{1}) { |
| 796 |
// Step 3.a. |
| 797 |
*result = difference; |
| 798 |
return true; |
| 799 |
} |
| 800 |
|
| 801 |
// Step 4. |
| 802 |
ISODateTime dateTime; |
| 803 |
if (!GetISODateTimeFor(cx, timeZone, ns1, &dateTime)) { |
| 804 |
return false; |
| 805 |
} |
| 806 |
|
| 807 |
// Step 5. |
| 808 |
return RoundRelativeDuration( |
| 809 |
cx, difference, ns2, dateTime, timeZone, calendar, settings.largestUnit, |
| 810 |
settings.roundingIncrement, settings.smallestUnit, settings.roundingMode, |
| 811 |
result); |
| 812 |
} |
| 813 |
|
| 814 |
/** |
| 815 |
* DifferenceZonedDateTimeWithTotal ( ns1, ns2, timeZone, calendar, unit ) |
| 816 |
*/ |
| 817 |
bool js::temporal::DifferenceZonedDateTimeWithTotal( |
| 818 |
JSContext* cx, JS::Handle<ZonedDateTime> zonedDateTime, |
| 819 |
const EpochNanoseconds& ns2, TemporalUnit unit, double* result) { |
| 820 |
MOZ_ASSERT(IsValidEpochNanoseconds(ns2)); |
| 821 |
|
| 822 |
const auto& ns1 = zonedDateTime.epochNanoseconds(); |
| 823 |
auto timeZone = zonedDateTime.timeZone(); |
| 824 |
auto calendar = zonedDateTime.calendar(); |
| 825 |
|
| 826 |
// Step 1. |
| 827 |
if (unit > TemporalUnit::Day) { |
| 828 |
// Step 1.a. |
| 829 |
auto difference = TimeDurationFromEpochNanosecondsDifference(ns2, ns1); |
| 830 |
MOZ_ASSERT(IsValidEpochDuration(difference.to<EpochDuration>())); |
| 831 |
|
| 832 |
// Step 1.b. |
| 833 |
*result = TotalTimeDuration(difference, unit); |
| 834 |
return true; |
| 835 |
} |
| 836 |
|
| 837 |
// Step 2. |
| 838 |
InternalDuration difference; |
| 839 |
if (!DifferenceZonedDateTime(cx, ns1, ns2, timeZone, calendar, unit, |
| 840 |
&difference)) { |
| 841 |
return false; |
| 842 |
} |
| 843 |
|
| 844 |
// Step 3. |
| 845 |
ISODateTime dateTime; |
| 846 |
if (!GetISODateTimeFor(cx, timeZone, ns1, &dateTime)) { |
| 847 |
return false; |
| 848 |
} |
| 849 |
|
| 850 |
// Step 5. |
| 851 |
return TotalRelativeDuration(cx, difference, ns2, dateTime, timeZone, |
| 852 |
calendar, unit, result); |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* DifferenceTemporalZonedDateTime ( operation, zonedDateTime, other, options ) |
| 857 |
*/ |
| 858 |
static bool DifferenceTemporalZonedDateTime(JSContext* cx, |
| 859 |
TemporalDifference operation, |
| 860 |
const CallArgs& args) { |
| 861 |
Rooted<ZonedDateTime> zonedDateTime( |
| 862 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 863 |
|
| 864 |
// Step 1. |
| 865 |
Rooted<ZonedDateTime> other(cx); |
| 866 |
if (!ToTemporalZonedDateTime(cx, args.get(0), &other)) { |
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
// Step 2. |
| 871 |
if (!CalendarEquals(zonedDateTime.calendar(), other.calendar())) { |
| 872 |
JS_ReportErrorNumberASCII( |
| 873 |
cx, GetErrorMessage, nullptr, JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE, |
| 874 |
CalendarIdentifier(zonedDateTime.calendar()).data(), |
| 875 |
CalendarIdentifier(other.calendar()).data()); |
| 876 |
return false; |
| 877 |
} |
| 878 |
|
| 879 |
// Steps 3-4. |
| 880 |
DifferenceSettings settings; |
| 881 |
if (args.hasDefined(1)) { |
| 882 |
// Step 3. |
| 883 |
Rooted<JSObject*> options( |
| 884 |
cx, RequireObjectArg(cx, "options", ToName(operation), args[1])); |
| 885 |
if (!options) { |
| 886 |
return false; |
| 887 |
} |
| 888 |
|
| 889 |
// Step 4. |
| 890 |
if (!GetDifferenceSettings( |
| 891 |
cx, operation, options, TemporalUnitGroup::DateTime, |
| 892 |
TemporalUnit::Nanosecond, TemporalUnit::Hour, &settings)) { |
| 893 |
return false; |
| 894 |
} |
| 895 |
} else { |
| 896 |
// Steps 3-4. |
| 897 |
settings = { |
| 898 |
TemporalUnit::Nanosecond, |
| 899 |
TemporalUnit::Hour, |
| 900 |
TemporalRoundingMode::Trunc, |
| 901 |
Increment{1}, |
| 902 |
}; |
| 903 |
} |
| 904 |
|
| 905 |
// Step 5. |
| 906 |
if (settings.largestUnit > TemporalUnit::Day) { |
| 907 |
MOZ_ASSERT(settings.smallestUnit >= settings.largestUnit); |
| 908 |
|
| 909 |
// Step 5.a. |
| 910 |
auto timeDuration = |
| 911 |
DifferenceInstant(zonedDateTime.epochNanoseconds(), |
| 912 |
other.epochNanoseconds(), settings.roundingIncrement, |
| 913 |
settings.smallestUnit, settings.roundingMode); |
| 914 |
|
| 915 |
// Step 5.b. |
| 916 |
Duration result; |
| 917 |
if (!TemporalDurationFromInternal(cx, timeDuration, settings.largestUnit, |
| 918 |
&result)) { |
| 919 |
return false; |
| 920 |
} |
| 921 |
|
| 922 |
// Step 5.c. |
| 923 |
if (operation == TemporalDifference::Since) { |
| 924 |
result = result.negate(); |
| 925 |
} |
| 926 |
|
| 927 |
// Step 5.d. |
| 928 |
auto* obj = CreateTemporalDuration(cx, result); |
| 929 |
if (!obj) { |
| 930 |
return false; |
| 931 |
} |
| 932 |
|
| 933 |
args.rval().setObject(*obj); |
| 934 |
return true; |
| 935 |
} |
| 936 |
|
| 937 |
// Steps 6-7. |
| 938 |
if (!TimeZoneEquals(zonedDateTime.timeZone(), other.timeZone())) { |
| 939 |
if (auto one = QuoteString(cx, zonedDateTime.timeZone().identifier())) { |
| 940 |
if (auto two = QuoteString(cx, other.timeZone().identifier())) { |
| 941 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 942 |
JSMSG_TEMPORAL_TIMEZONE_INCOMPATIBLE, |
| 943 |
one.get(), two.get()); |
| 944 |
} |
| 945 |
} |
| 946 |
return false; |
| 947 |
} |
| 948 |
|
| 949 |
// Step 8. |
| 950 |
if (zonedDateTime.epochNanoseconds() == other.epochNanoseconds()) { |
| 951 |
auto* obj = CreateTemporalDuration(cx, {}); |
| 952 |
if (!obj) { |
| 953 |
return false; |
| 954 |
} |
| 955 |
|
| 956 |
args.rval().setObject(*obj); |
| 957 |
return true; |
| 958 |
} |
| 959 |
|
| 960 |
// Step 9. |
| 961 |
InternalDuration internalDuration; |
| 962 |
if (!DifferenceZonedDateTimeWithRounding(cx, zonedDateTime, |
| 963 |
other.epochNanoseconds(), settings, |
| 964 |
&internalDuration)) { |
| 965 |
return false; |
| 966 |
} |
| 967 |
MOZ_ASSERT(IsValidDuration(internalDuration)); |
| 968 |
|
| 969 |
// Step 10. |
| 970 |
Duration result; |
| 971 |
if (!TemporalDurationFromInternal(cx, internalDuration, TemporalUnit::Hour, |
| 972 |
&result)) { |
| 973 |
return false; |
| 974 |
} |
| 975 |
|
| 976 |
// Step 11. |
| 977 |
if (operation == TemporalDifference::Since) { |
| 978 |
result = result.negate(); |
| 979 |
} |
| 980 |
|
| 981 |
// Step 12. |
| 982 |
auto* obj = CreateTemporalDuration(cx, result); |
| 983 |
if (!obj) { |
| 984 |
return false; |
| 985 |
} |
| 986 |
|
| 987 |
args.rval().setObject(*obj); |
| 988 |
return true; |
| 989 |
} |
| 990 |
|
| 991 |
/** |
| 992 |
* AddDurationToZonedDateTime ( operation, zonedDateTime, temporalDurationLike, |
| 993 |
* options ) |
| 994 |
*/ |
| 995 |
static bool AddDurationToZonedDateTime(JSContext* cx, |
| 996 |
TemporalAddDuration operation, |
| 997 |
const CallArgs& args) { |
| 998 |
Rooted<ZonedDateTime> zonedDateTime( |
| 999 |
cx, &args.thisv().toObject().as<ZonedDateTimeObject>()); |
| 1000 |
|
| 1001 |
// Step 1. |
| 1002 |
Duration duration; |
| 1003 |
if (!ToTemporalDuration(cx, args.get(0), &duration)) { |
| 1004 |
return false; |
| 1005 |
} |
| 1006 |
|
| 1007 |
// Step 2. |
| 1008 |
if (operation == TemporalAddDuration::Subtract) { |
| 1009 |
duration = duration.negate(); |
| 1010 |
} |
| 1011 |
|
| 1012 |
// Steps 3-4. |
| 1013 |
auto overflow = TemporalOverflow::Constrain; |
| 1014 |
if (args.hasDefined(1)) { |
| 1015 |
// Step 3. |
| 1016 |
Rooted<JSObject*> options( |
| 1017 |
cx, RequireObjectArg(cx, "options", ToName(operation), args[1])); |
| 1018 |
if (!options) { |
| 1019 |
return false; |
| 1020 |
} |
| 1021 |
|
| 1022 |
// Step 4. |
| 1023 |
if (!GetTemporalOverflowOption(cx, options, &overflow)) { |
| 1024 |
return false; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
// Step 5. |
| 1029 |
auto calendar = zonedDateTime.calendar(); |
| 1030 |
|
| 1031 |
// Step 6. |
| 1032 |
auto timeZone = zonedDateTime.timeZone(); |
| 1033 |
|
| 1034 |
// Step 7. |
| 1035 |
auto internalDuration = ToInternalDurationRecord(duration); |
| 1036 |
|
| 1037 |
// Step 8. |
| 1038 |
EpochNanoseconds epochNanoseconds; |
| 1039 |
if (!::AddZonedDateTime(cx, zonedDateTime, internalDuration, overflow, |
| 1040 |
&epochNanoseconds)) { |
| 1041 |
return false; |
| 1042 |
} |
| 1043 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 1044 |
|
| 1045 |
// Step 9. |
| 1046 |
auto* result = |
| 1047 |
CreateTemporalZonedDateTime(cx, epochNanoseconds, timeZone, calendar); |
| 1048 |
if (!result) { |
| 1049 |
return false; |
| 1050 |
} |
| 1051 |
|
| 1052 |
args.rval().setObject(*result); |
| 1053 |
return true; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/** |
| 1057 |
* FormatUTCOffsetNanoseconds ( offsetNanoseconds ) |
| 1058 |
*/ |
| 1059 |
static JSString* FormatUTCOffsetNanoseconds(JSContext* cx, |
| 1060 |
int64_t offsetNanoseconds) { |
| 1061 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 1062 |
|
| 1063 |
// Step 1. |
| 1064 |
char sign = offsetNanoseconds >= 0 ? '+' : '-'; |
| 1065 |
|
| 1066 |
// Step 2. |
| 1067 |
int64_t absoluteNanoseconds = std::abs(offsetNanoseconds); |
| 1068 |
|
| 1069 |
// Step 6. (Reordered) |
| 1070 |
int32_t subSecondNanoseconds = int32_t(absoluteNanoseconds % 1'000'000'000); |
| 1071 |
|
| 1072 |
// Step 5. (Reordered) |
| 1073 |
int32_t quotient = int32_t(absoluteNanoseconds / 1'000'000'000); |
| 1074 |
int32_t second = quotient % 60; |
| 1075 |
|
| 1076 |
// Step 4. (Reordered) |
| 1077 |
quotient /= 60; |
| 1078 |
int32_t minute = quotient % 60; |
| 1079 |
|
| 1080 |
// Step 3. |
| 1081 |
int32_t hour = quotient / 60; |
| 1082 |
MOZ_ASSERT(hour < 24, "time zone offset mustn't exceed 24-hours"); |
| 1083 |
|
| 1084 |
// Format: "sign hour{2} : minute{2} : second{2} . fractional{9}" |
| 1085 |
constexpr size_t maxLength = 1 + 2 + 1 + 2 + 1 + 2 + 1 + 9; |
| 1086 |
char result[maxLength]; |
| 1087 |
|
| 1088 |
size_t n = 0; |
| 1089 |
|
| 1090 |
// Steps 7-8. (Inlined FormatTimeString). |
| 1091 |
result[n++] = sign; |
| 1092 |
result[n++] = char('0' + (hour / 10)); |
| 1093 |
result[n++] = char('0' + (hour % 10)); |
| 1094 |
result[n++] = ':'; |
| 1095 |
result[n++] = char('0' + (minute / 10)); |
| 1096 |
result[n++] = char('0' + (minute % 10)); |
| 1097 |
|
| 1098 |
if (second != 0 || subSecondNanoseconds != 0) { |
| 1099 |
result[n++] = ':'; |
| 1100 |
result[n++] = char('0' + (second / 10)); |
| 1101 |
result[n++] = char('0' + (second % 10)); |
| 1102 |
|
| 1103 |
if (uint32_t fractional = subSecondNanoseconds) { |
| 1104 |
result[n++] = '.'; |
| 1105 |
|
| 1106 |
uint32_t k = 100'000'000; |
| 1107 |
do { |
| 1108 |
result[n++] = char('0' + (fractional / k)); |
| 1109 |
fractional %= k; |
| 1110 |
k /= 10; |
| 1111 |
} while (fractional); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
MOZ_ASSERT(n <= maxLength); |
| 1116 |
|
| 1117 |
// Step 9. |
| 1118 |
return NewStringCopyN<CanGC>(cx, result, n); |
| 1119 |
} |
| 1120 |
|
| 1121 |
/** |
| 1122 |
* Temporal.ZonedDateTime ( epochNanoseconds, timeZone [ , calendar ] ) |
| 1123 |
*/ |
| 1124 |
static bool ZonedDateTimeConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 1125 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1126 |
|
| 1127 |
// Step 1. |
| 1128 |
if (!ThrowIfNotConstructing(cx, args, "Temporal.ZonedDateTime")) { |
| 1129 |
return false; |
| 1130 |
} |
| 1131 |
|
| 1132 |
// Step 2. |
| 1133 |
Rooted<BigInt*> epochNanoseconds(cx, js::ToBigInt(cx, args.get(0))); |
| 1134 |
if (!epochNanoseconds) { |
| 1135 |
return false; |
| 1136 |
} |
| 1137 |
|
| 1138 |
// Step 3. |
| 1139 |
if (!IsValidEpochNanoseconds(epochNanoseconds)) { |
| 1140 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1141 |
JSMSG_TEMPORAL_INSTANT_INVALID); |
| 1142 |
return false; |
| 1143 |
} |
| 1144 |
|
| 1145 |
// Step 4. |
| 1146 |
if (!args.get(1).isString()) { |
| 1147 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, args.get(1), |
| 1148 |
nullptr, "not a string"); |
| 1149 |
return false; |
| 1150 |
} |
| 1151 |
|
| 1152 |
// Step 5. |
| 1153 |
Rooted<JSString*> timeZoneString(cx, args[1].toString()); |
| 1154 |
Rooted<ParsedTimeZone> timeZoneParse(cx); |
| 1155 |
if (!ParseTimeZoneIdentifier(cx, timeZoneString, &timeZoneParse)) { |
| 1156 |
return false; |
| 1157 |
} |
| 1158 |
|
| 1159 |
// Steps 6-7. |
| 1160 |
Rooted<TimeZoneValue> timeZone(cx); |
| 1161 |
if (!ToTemporalTimeZone(cx, timeZoneParse, &timeZone)) { |
| 1162 |
return false; |
| 1163 |
} |
| 1164 |
|
| 1165 |
// Steps 8-10. |
| 1166 |
Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601)); |
| 1167 |
if (args.hasDefined(2)) { |
| 1168 |
// Step 9. |
| 1169 |
if (!args[2].isString()) { |
| 1170 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, args[2], |
| 1171 |
nullptr, "not a string"); |
| 1172 |
return false; |
| 1173 |
} |
| 1174 |
|
| 1175 |
// Step 10. |
| 1176 |
Rooted<JSString*> calendarString(cx, args[2].toString()); |
| 1177 |
if (!CanonicalizeCalendar(cx, calendarString, &calendar)) { |
| 1178 |
return false; |
| 1179 |
} |
| 1180 |
} |
| 1181 |
|
| 1182 |
// Step 11. |
| 1183 |
auto* obj = CreateTemporalZonedDateTime(cx, args, epochNanoseconds, timeZone, |
| 1184 |
calendar); |
| 1185 |
if (!obj) { |
| 1186 |
return false; |
| 1187 |
} |
| 1188 |
|
| 1189 |
args.rval().setObject(*obj); |
| 1190 |
return true; |
| 1191 |
} |
| 1192 |
|
| 1193 |
/** |
| 1194 |
* Temporal.ZonedDateTime.from ( item [ , options ] ) |
| 1195 |
*/ |
| 1196 |
static bool ZonedDateTime_from(JSContext* cx, unsigned argc, Value* vp) { |
| 1197 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1198 |
|
| 1199 |
// Step 1. |
| 1200 |
Rooted<ZonedDateTime> zonedDateTime(cx); |
| 1201 |
if (!ToTemporalZonedDateTime(cx, args.get(0), args.get(1), &zonedDateTime)) { |
| 1202 |
return false; |
| 1203 |
} |
| 1204 |
|
| 1205 |
auto* result = CreateTemporalZonedDateTime(cx, zonedDateTime); |
| 1206 |
if (!result) { |
| 1207 |
return false; |
| 1208 |
} |
| 1209 |
|
| 1210 |
args.rval().setObject(*result); |
| 1211 |
return true; |
| 1212 |
} |
| 1213 |
|
| 1214 |
/** |
| 1215 |
* Temporal.ZonedDateTime.compare ( one, two ) |
| 1216 |
*/ |
| 1217 |
static bool ZonedDateTime_compare(JSContext* cx, unsigned argc, Value* vp) { |
| 1218 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1219 |
|
| 1220 |
// Step 1. |
| 1221 |
Rooted<ZonedDateTime> one(cx); |
| 1222 |
if (!ToTemporalZonedDateTime(cx, args.get(0), &one)) { |
| 1223 |
return false; |
| 1224 |
} |
| 1225 |
|
| 1226 |
// Step 2. |
| 1227 |
Rooted<ZonedDateTime> two(cx); |
| 1228 |
if (!ToTemporalZonedDateTime(cx, args.get(1), &two)) { |
| 1229 |
return false; |
| 1230 |
} |
| 1231 |
|
| 1232 |
// Step 3. |
| 1233 |
const auto& oneNs = one.epochNanoseconds(); |
| 1234 |
const auto& twoNs = two.epochNanoseconds(); |
| 1235 |
args.rval().setInt32(oneNs > twoNs ? 1 : oneNs < twoNs ? -1 : 0); |
| 1236 |
return true; |
| 1237 |
} |
| 1238 |
|
| 1239 |
/** |
| 1240 |
* get Temporal.ZonedDateTime.prototype.calendarId |
| 1241 |
*/ |
| 1242 |
static bool ZonedDateTime_calendarId(JSContext* cx, const CallArgs& args) { |
| 1243 |
auto* zonedDateTime = &args.thisv().toObject().as<ZonedDateTimeObject>(); |
| 1244 |
|
| 1245 |
// Step 3. |
| 1246 |
auto* str = |
| 1247 |
NewStringCopy<CanGC>(cx, CalendarIdentifier(zonedDateTime->calendar())); |
| 1248 |
if (!str) { |
| 1249 |
return false; |
| 1250 |
} |
| 1251 |
|
| 1252 |
args.rval().setString(str); |
| 1253 |
return true; |
| 1254 |
} |
| 1255 |
|
| 1256 |
/** |
| 1257 |
* get Temporal.ZonedDateTime.prototype.calendarId |
| 1258 |
*/ |
| 1259 |
static bool ZonedDateTime_calendarId(JSContext* cx, unsigned argc, Value* vp) { |
| 1260 |
// Steps 1-2. |
| 1261 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1262 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_calendarId>(cx, |
| 1263 |
args); |
| 1264 |
} |
| 1265 |
|
| 1266 |
/** |
| 1267 |
* get Temporal.ZonedDateTime.prototype.timeZoneId |
| 1268 |
*/ |
| 1269 |
static bool ZonedDateTime_timeZoneId(JSContext* cx, const CallArgs& args) { |
| 1270 |
auto* zonedDateTime = &args.thisv().toObject().as<ZonedDateTimeObject>(); |
| 1271 |
|
| 1272 |
// Step 3. |
| 1273 |
args.rval().setString(zonedDateTime->timeZone().identifier()); |
| 1274 |
return true; |
| 1275 |
} |
| 1276 |
|
| 1277 |
/** |
| 1278 |
* get Temporal.ZonedDateTime.prototype.timeZoneId |
| 1279 |
*/ |
| 1280 |
static bool ZonedDateTime_timeZoneId(JSContext* cx, unsigned argc, Value* vp) { |
| 1281 |
// Steps 1-2. |
| 1282 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1283 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_timeZoneId>(cx, |
| 1284 |
args); |
| 1285 |
} |
| 1286 |
|
| 1287 |
/** |
| 1288 |
* get Temporal.ZonedDateTime.prototype.era |
| 1289 |
*/ |
| 1290 |
static bool ZonedDateTime_era(JSContext* cx, const CallArgs& args) { |
| 1291 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1292 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1293 |
|
| 1294 |
// Step 3. |
| 1295 |
ISODateTime dateTime; |
| 1296 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1297 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1298 |
return false; |
| 1299 |
} |
| 1300 |
|
| 1301 |
// Step 4. |
| 1302 |
return CalendarEra(cx, zonedDateTime.calendar(), dateTime.date, args.rval()); |
| 1303 |
} |
| 1304 |
|
| 1305 |
/** |
| 1306 |
* get Temporal.ZonedDateTime.prototype.era |
| 1307 |
*/ |
| 1308 |
static bool ZonedDateTime_era(JSContext* cx, unsigned argc, Value* vp) { |
| 1309 |
// Steps 1-2. |
| 1310 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1311 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_era>(cx, args); |
| 1312 |
} |
| 1313 |
|
| 1314 |
/** |
| 1315 |
* get Temporal.ZonedDateTime.prototype.eraYear |
| 1316 |
*/ |
| 1317 |
static bool ZonedDateTime_eraYear(JSContext* cx, const CallArgs& args) { |
| 1318 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1319 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1320 |
|
| 1321 |
// Step 3. |
| 1322 |
ISODateTime dateTime; |
| 1323 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1324 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1325 |
return false; |
| 1326 |
} |
| 1327 |
|
| 1328 |
// Steps 4-6. |
| 1329 |
return CalendarEraYear(cx, zonedDateTime.calendar(), dateTime.date, |
| 1330 |
args.rval()); |
| 1331 |
} |
| 1332 |
|
| 1333 |
/** |
| 1334 |
* get Temporal.ZonedDateTime.prototype.eraYear |
| 1335 |
*/ |
| 1336 |
static bool ZonedDateTime_eraYear(JSContext* cx, unsigned argc, Value* vp) { |
| 1337 |
// Steps 1-2. |
| 1338 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1339 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_eraYear>(cx, args); |
| 1340 |
} |
| 1341 |
|
| 1342 |
/** |
| 1343 |
* get Temporal.ZonedDateTime.prototype.year |
| 1344 |
*/ |
| 1345 |
static bool ZonedDateTime_year(JSContext* cx, const CallArgs& args) { |
| 1346 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1347 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1348 |
|
| 1349 |
// Step 3. |
| 1350 |
ISODateTime dateTime; |
| 1351 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1352 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1353 |
return false; |
| 1354 |
} |
| 1355 |
|
| 1356 |
// Step 4. |
| 1357 |
return CalendarYear(cx, zonedDateTime.calendar(), dateTime.date, args.rval()); |
| 1358 |
} |
| 1359 |
|
| 1360 |
/** |
| 1361 |
* get Temporal.ZonedDateTime.prototype.year |
| 1362 |
*/ |
| 1363 |
static bool ZonedDateTime_year(JSContext* cx, unsigned argc, Value* vp) { |
| 1364 |
// Steps 1-2. |
| 1365 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1366 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_year>(cx, args); |
| 1367 |
} |
| 1368 |
|
| 1369 |
/** |
| 1370 |
* get Temporal.ZonedDateTime.prototype.month |
| 1371 |
*/ |
| 1372 |
static bool ZonedDateTime_month(JSContext* cx, const CallArgs& args) { |
| 1373 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1374 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1375 |
|
| 1376 |
// Step 3. |
| 1377 |
ISODateTime dateTime; |
| 1378 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1379 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1380 |
return false; |
| 1381 |
} |
| 1382 |
|
| 1383 |
// Step 4. |
| 1384 |
return CalendarMonth(cx, zonedDateTime.calendar(), dateTime.date, |
| 1385 |
args.rval()); |
| 1386 |
} |
| 1387 |
|
| 1388 |
/** |
| 1389 |
* get Temporal.ZonedDateTime.prototype.month |
| 1390 |
*/ |
| 1391 |
static bool ZonedDateTime_month(JSContext* cx, unsigned argc, Value* vp) { |
| 1392 |
// Steps 1-2. |
| 1393 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1394 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_month>(cx, args); |
| 1395 |
} |
| 1396 |
|
| 1397 |
/** |
| 1398 |
* get Temporal.ZonedDateTime.prototype.monthCode |
| 1399 |
*/ |
| 1400 |
static bool ZonedDateTime_monthCode(JSContext* cx, const CallArgs& args) { |
| 1401 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1402 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1403 |
|
| 1404 |
// Step 3. |
| 1405 |
ISODateTime dateTime; |
| 1406 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1407 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1408 |
return false; |
| 1409 |
} |
| 1410 |
|
| 1411 |
// Step 4. |
| 1412 |
return CalendarMonthCode(cx, zonedDateTime.calendar(), dateTime.date, |
| 1413 |
args.rval()); |
| 1414 |
} |
| 1415 |
|
| 1416 |
/** |
| 1417 |
* get Temporal.ZonedDateTime.prototype.monthCode |
| 1418 |
*/ |
| 1419 |
static bool ZonedDateTime_monthCode(JSContext* cx, unsigned argc, Value* vp) { |
| 1420 |
// Steps 1-2. |
| 1421 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1422 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_monthCode>(cx, |
| 1423 |
args); |
| 1424 |
} |
| 1425 |
|
| 1426 |
/** |
| 1427 |
* get Temporal.ZonedDateTime.prototype.day |
| 1428 |
*/ |
| 1429 |
static bool ZonedDateTime_day(JSContext* cx, const CallArgs& args) { |
| 1430 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1431 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1432 |
|
| 1433 |
// Step 3. |
| 1434 |
ISODateTime dateTime; |
| 1435 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1436 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1437 |
return false; |
| 1438 |
} |
| 1439 |
|
| 1440 |
// Step 4. |
| 1441 |
return CalendarDay(cx, zonedDateTime.calendar(), dateTime.date, args.rval()); |
| 1442 |
} |
| 1443 |
|
| 1444 |
/** |
| 1445 |
* get Temporal.ZonedDateTime.prototype.day |
| 1446 |
*/ |
| 1447 |
static bool ZonedDateTime_day(JSContext* cx, unsigned argc, Value* vp) { |
| 1448 |
// Steps 1-2. |
| 1449 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1450 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_day>(cx, args); |
| 1451 |
} |
| 1452 |
|
| 1453 |
/** |
| 1454 |
* get Temporal.ZonedDateTime.prototype.hour |
| 1455 |
*/ |
| 1456 |
static bool ZonedDateTime_hour(JSContext* cx, const CallArgs& args) { |
| 1457 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1458 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1459 |
|
| 1460 |
// Step 3. |
| 1461 |
ISODateTime dateTime; |
| 1462 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1463 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1464 |
return false; |
| 1465 |
} |
| 1466 |
|
| 1467 |
// Step 4. |
| 1468 |
args.rval().setInt32(dateTime.time.hour); |
| 1469 |
return true; |
| 1470 |
} |
| 1471 |
|
| 1472 |
/** |
| 1473 |
* get Temporal.ZonedDateTime.prototype.hour |
| 1474 |
*/ |
| 1475 |
static bool ZonedDateTime_hour(JSContext* cx, unsigned argc, Value* vp) { |
| 1476 |
// Steps 1-2. |
| 1477 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1478 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_hour>(cx, args); |
| 1479 |
} |
| 1480 |
|
| 1481 |
/** |
| 1482 |
* get Temporal.ZonedDateTime.prototype.minute |
| 1483 |
*/ |
| 1484 |
static bool ZonedDateTime_minute(JSContext* cx, const CallArgs& args) { |
| 1485 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1486 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1487 |
|
| 1488 |
// Step 3. |
| 1489 |
ISODateTime dateTime; |
| 1490 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1491 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1492 |
return false; |
| 1493 |
} |
| 1494 |
|
| 1495 |
// Step 4. |
| 1496 |
args.rval().setInt32(dateTime.time.minute); |
| 1497 |
return true; |
| 1498 |
} |
| 1499 |
|
| 1500 |
/** |
| 1501 |
* get Temporal.ZonedDateTime.prototype.minute |
| 1502 |
*/ |
| 1503 |
static bool ZonedDateTime_minute(JSContext* cx, unsigned argc, Value* vp) { |
| 1504 |
// Steps 1-2. |
| 1505 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1506 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_minute>(cx, args); |
| 1507 |
} |
| 1508 |
|
| 1509 |
/** |
| 1510 |
* get Temporal.ZonedDateTime.prototype.second |
| 1511 |
*/ |
| 1512 |
static bool ZonedDateTime_second(JSContext* cx, const CallArgs& args) { |
| 1513 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1514 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1515 |
|
| 1516 |
// Step 3. |
| 1517 |
ISODateTime dateTime; |
| 1518 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1519 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1520 |
return false; |
| 1521 |
} |
| 1522 |
|
| 1523 |
// Step 4. |
| 1524 |
args.rval().setInt32(dateTime.time.second); |
| 1525 |
return true; |
| 1526 |
} |
| 1527 |
|
| 1528 |
/** |
| 1529 |
* get Temporal.ZonedDateTime.prototype.second |
| 1530 |
*/ |
| 1531 |
static bool ZonedDateTime_second(JSContext* cx, unsigned argc, Value* vp) { |
| 1532 |
// Steps 1-2. |
| 1533 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1534 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_second>(cx, args); |
| 1535 |
} |
| 1536 |
|
| 1537 |
/** |
| 1538 |
* get Temporal.ZonedDateTime.prototype.millisecond |
| 1539 |
*/ |
| 1540 |
static bool ZonedDateTime_millisecond(JSContext* cx, const CallArgs& args) { |
| 1541 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1542 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1543 |
|
| 1544 |
// Step 3. |
| 1545 |
ISODateTime dateTime; |
| 1546 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1547 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1548 |
return false; |
| 1549 |
} |
| 1550 |
|
| 1551 |
// Step 4. |
| 1552 |
args.rval().setInt32(dateTime.time.millisecond); |
| 1553 |
return true; |
| 1554 |
} |
| 1555 |
|
| 1556 |
/** |
| 1557 |
* get Temporal.ZonedDateTime.prototype.millisecond |
| 1558 |
*/ |
| 1559 |
static bool ZonedDateTime_millisecond(JSContext* cx, unsigned argc, Value* vp) { |
| 1560 |
// Steps 1-2. |
| 1561 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1562 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_millisecond>(cx, |
| 1563 |
args); |
| 1564 |
} |
| 1565 |
|
| 1566 |
/** |
| 1567 |
* get Temporal.ZonedDateTime.prototype.microsecond |
| 1568 |
*/ |
| 1569 |
static bool ZonedDateTime_microsecond(JSContext* cx, const CallArgs& args) { |
| 1570 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1571 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1572 |
|
| 1573 |
// Step 3. |
| 1574 |
ISODateTime dateTime; |
| 1575 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1576 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1577 |
return false; |
| 1578 |
} |
| 1579 |
|
| 1580 |
// Step 4. |
| 1581 |
args.rval().setInt32(dateTime.time.microsecond); |
| 1582 |
return true; |
| 1583 |
} |
| 1584 |
|
| 1585 |
/** |
| 1586 |
* get Temporal.ZonedDateTime.prototype.microsecond |
| 1587 |
*/ |
| 1588 |
static bool ZonedDateTime_microsecond(JSContext* cx, unsigned argc, Value* vp) { |
| 1589 |
// Steps 1-2. |
| 1590 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1591 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_microsecond>(cx, |
| 1592 |
args); |
| 1593 |
} |
| 1594 |
|
| 1595 |
/** |
| 1596 |
* get Temporal.ZonedDateTime.prototype.nanosecond |
| 1597 |
*/ |
| 1598 |
static bool ZonedDateTime_nanosecond(JSContext* cx, const CallArgs& args) { |
| 1599 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1600 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1601 |
|
| 1602 |
// Step 3. |
| 1603 |
ISODateTime dateTime; |
| 1604 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1605 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1606 |
return false; |
| 1607 |
} |
| 1608 |
|
| 1609 |
// Step 4. |
| 1610 |
args.rval().setInt32(dateTime.time.nanosecond); |
| 1611 |
return true; |
| 1612 |
} |
| 1613 |
|
| 1614 |
/** |
| 1615 |
* get Temporal.ZonedDateTime.prototype.nanosecond |
| 1616 |
*/ |
| 1617 |
static bool ZonedDateTime_nanosecond(JSContext* cx, unsigned argc, Value* vp) { |
| 1618 |
// Steps 1-2. |
| 1619 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1620 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_nanosecond>(cx, |
| 1621 |
args); |
| 1622 |
} |
| 1623 |
|
| 1624 |
/** |
| 1625 |
* get Temporal.ZonedDateTime.prototype.epochMilliseconds |
| 1626 |
*/ |
| 1627 |
static bool ZonedDateTime_epochMilliseconds(JSContext* cx, |
| 1628 |
const CallArgs& args) { |
| 1629 |
auto* zonedDateTime = &args.thisv().toObject().as<ZonedDateTimeObject>(); |
| 1630 |
|
| 1631 |
// Step 3. |
| 1632 |
auto epochNs = zonedDateTime->epochNanoseconds(); |
| 1633 |
|
| 1634 |
// Steps 4-5. |
| 1635 |
args.rval().setNumber(epochNs.floorToMilliseconds()); |
| 1636 |
return true; |
| 1637 |
} |
| 1638 |
|
| 1639 |
/** |
| 1640 |
* get Temporal.ZonedDateTime.prototype.epochMilliseconds |
| 1641 |
*/ |
| 1642 |
static bool ZonedDateTime_epochMilliseconds(JSContext* cx, unsigned argc, |
| 1643 |
Value* vp) { |
| 1644 |
// Steps 1-2. |
| 1645 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1646 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_epochMilliseconds>( |
| 1647 |
cx, args); |
| 1648 |
} |
| 1649 |
|
| 1650 |
/** |
| 1651 |
* get Temporal.ZonedDateTime.prototype.epochNanoseconds |
| 1652 |
*/ |
| 1653 |
static bool ZonedDateTime_epochNanoseconds(JSContext* cx, |
| 1654 |
const CallArgs& args) { |
| 1655 |
auto* zonedDateTime = &args.thisv().toObject().as<ZonedDateTimeObject>(); |
| 1656 |
|
| 1657 |
// Step 3. |
| 1658 |
auto* nanoseconds = ToBigInt(cx, zonedDateTime->epochNanoseconds()); |
| 1659 |
if (!nanoseconds) { |
| 1660 |
return false; |
| 1661 |
} |
| 1662 |
|
| 1663 |
args.rval().setBigInt(nanoseconds); |
| 1664 |
return true; |
| 1665 |
} |
| 1666 |
|
| 1667 |
/** |
| 1668 |
* get Temporal.ZonedDateTime.prototype.epochNanoseconds |
| 1669 |
*/ |
| 1670 |
static bool ZonedDateTime_epochNanoseconds(JSContext* cx, unsigned argc, |
| 1671 |
Value* vp) { |
| 1672 |
// Steps 1-2. |
| 1673 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1674 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_epochNanoseconds>( |
| 1675 |
cx, args); |
| 1676 |
} |
| 1677 |
|
| 1678 |
/** |
| 1679 |
* get Temporal.ZonedDateTime.prototype.dayOfWeek |
| 1680 |
*/ |
| 1681 |
static bool ZonedDateTime_dayOfWeek(JSContext* cx, const CallArgs& args) { |
| 1682 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1683 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1684 |
|
| 1685 |
// Step 3. |
| 1686 |
ISODateTime dateTime; |
| 1687 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1688 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1689 |
return false; |
| 1690 |
} |
| 1691 |
|
| 1692 |
// Step 4. |
| 1693 |
return CalendarDayOfWeek(cx, zonedDateTime.calendar(), dateTime.date, |
| 1694 |
args.rval()); |
| 1695 |
} |
| 1696 |
|
| 1697 |
/** |
| 1698 |
* get Temporal.ZonedDateTime.prototype.dayOfWeek |
| 1699 |
*/ |
| 1700 |
static bool ZonedDateTime_dayOfWeek(JSContext* cx, unsigned argc, Value* vp) { |
| 1701 |
// Steps 1-2. |
| 1702 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1703 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_dayOfWeek>(cx, |
| 1704 |
args); |
| 1705 |
} |
| 1706 |
|
| 1707 |
/** |
| 1708 |
* get Temporal.ZonedDateTime.prototype.dayOfYear |
| 1709 |
*/ |
| 1710 |
static bool ZonedDateTime_dayOfYear(JSContext* cx, const CallArgs& args) { |
| 1711 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1712 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1713 |
|
| 1714 |
// Step 3. |
| 1715 |
ISODateTime dateTime; |
| 1716 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1717 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1718 |
return false; |
| 1719 |
} |
| 1720 |
|
| 1721 |
// Step 4. |
| 1722 |
return CalendarDayOfYear(cx, zonedDateTime.calendar(), dateTime.date, |
| 1723 |
args.rval()); |
| 1724 |
} |
| 1725 |
|
| 1726 |
/** |
| 1727 |
* get Temporal.ZonedDateTime.prototype.dayOfYear |
| 1728 |
*/ |
| 1729 |
static bool ZonedDateTime_dayOfYear(JSContext* cx, unsigned argc, Value* vp) { |
| 1730 |
// Steps 1-2. |
| 1731 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1732 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_dayOfYear>(cx, |
| 1733 |
args); |
| 1734 |
} |
| 1735 |
|
| 1736 |
/** |
| 1737 |
* get Temporal.ZonedDateTime.prototype.weekOfYear |
| 1738 |
*/ |
| 1739 |
static bool ZonedDateTime_weekOfYear(JSContext* cx, const CallArgs& args) { |
| 1740 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1741 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1742 |
|
| 1743 |
// Step 3. |
| 1744 |
ISODateTime dateTime; |
| 1745 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1746 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1747 |
return false; |
| 1748 |
} |
| 1749 |
|
| 1750 |
// Steps 4-6. |
| 1751 |
return CalendarWeekOfYear(cx, zonedDateTime.calendar(), dateTime.date, |
| 1752 |
args.rval()); |
| 1753 |
} |
| 1754 |
|
| 1755 |
/** |
| 1756 |
* get Temporal.ZonedDateTime.prototype.weekOfYear |
| 1757 |
*/ |
| 1758 |
static bool ZonedDateTime_weekOfYear(JSContext* cx, unsigned argc, Value* vp) { |
| 1759 |
// Steps 1-2. |
| 1760 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1761 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_weekOfYear>(cx, |
| 1762 |
args); |
| 1763 |
} |
| 1764 |
|
| 1765 |
/** |
| 1766 |
* get Temporal.ZonedDateTime.prototype.yearOfWeek |
| 1767 |
*/ |
| 1768 |
static bool ZonedDateTime_yearOfWeek(JSContext* cx, const CallArgs& args) { |
| 1769 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1770 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1771 |
|
| 1772 |
// Step 3. |
| 1773 |
ISODateTime dateTime; |
| 1774 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1775 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1776 |
return false; |
| 1777 |
} |
| 1778 |
|
| 1779 |
// Steps 4-6. |
| 1780 |
return CalendarYearOfWeek(cx, zonedDateTime.calendar(), dateTime.date, |
| 1781 |
args.rval()); |
| 1782 |
} |
| 1783 |
|
| 1784 |
/** |
| 1785 |
* get Temporal.ZonedDateTime.prototype.yearOfWeek |
| 1786 |
*/ |
| 1787 |
static bool ZonedDateTime_yearOfWeek(JSContext* cx, unsigned argc, Value* vp) { |
| 1788 |
// Steps 1-2. |
| 1789 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1790 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_yearOfWeek>(cx, |
| 1791 |
args); |
| 1792 |
} |
| 1793 |
|
| 1794 |
/** |
| 1795 |
* get Temporal.ZonedDateTime.prototype.hoursInDay |
| 1796 |
*/ |
| 1797 |
static bool ZonedDateTime_hoursInDay(JSContext* cx, const CallArgs& args) { |
| 1798 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1799 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1800 |
|
| 1801 |
// Step 3. |
| 1802 |
auto timeZone = zonedDateTime.timeZone(); |
| 1803 |
|
| 1804 |
// Step 4. |
| 1805 |
ISODateTime dateTime; |
| 1806 |
if (!GetISODateTimeFor(cx, timeZone, zonedDateTime.epochNanoseconds(), |
| 1807 |
&dateTime)) { |
| 1808 |
return false; |
| 1809 |
} |
| 1810 |
|
| 1811 |
// Step 5. |
| 1812 |
const auto& today = dateTime.date; |
| 1813 |
|
| 1814 |
// Step 6. |
| 1815 |
auto tomorrow = BalanceISODate(today, 1); |
| 1816 |
if (!ISODateWithinLimits(tomorrow)) { |
| 1817 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1818 |
JSMSG_TEMPORAL_PLAIN_DATE_INVALID); |
| 1819 |
return false; |
| 1820 |
} |
| 1821 |
|
| 1822 |
// Step 7. |
| 1823 |
EpochNanoseconds todayNs; |
| 1824 |
if (!GetStartOfDay(cx, timeZone, today, &todayNs)) { |
| 1825 |
return false; |
| 1826 |
} |
| 1827 |
|
| 1828 |
// Step 8. |
| 1829 |
EpochNanoseconds tomorrowNs; |
| 1830 |
if (!GetStartOfDay(cx, timeZone, tomorrow, &tomorrowNs)) { |
| 1831 |
return false; |
| 1832 |
} |
| 1833 |
|
| 1834 |
// Step 9. |
| 1835 |
auto diff = tomorrowNs - todayNs; |
| 1836 |
MOZ_ASSERT(diff.abs() <= EpochDuration::fromDays(2), |
| 1837 |
"maximum day length for repeated days doesn't exceed two days"); |
| 1838 |
|
| 1839 |
static_assert(EpochDuration::fromDays(2).toNanoseconds() < Int128{INT64_MAX}, |
| 1840 |
"two days in nanoseconds fits into int64_t"); |
| 1841 |
|
| 1842 |
// Step 10. (Inlined TotalTimeDuration) |
| 1843 |
constexpr auto nsPerHour = ToNanoseconds(TemporalUnit::Hour); |
| 1844 |
args.rval().setNumber( |
| 1845 |
FractionToDouble(int64_t(diff.toNanoseconds()), nsPerHour)); |
| 1846 |
return true; |
| 1847 |
} |
| 1848 |
|
| 1849 |
/** |
| 1850 |
* get Temporal.ZonedDateTime.prototype.hoursInDay |
| 1851 |
*/ |
| 1852 |
static bool ZonedDateTime_hoursInDay(JSContext* cx, unsigned argc, Value* vp) { |
| 1853 |
// Steps 1-2. |
| 1854 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1855 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_hoursInDay>(cx, |
| 1856 |
args); |
| 1857 |
} |
| 1858 |
|
| 1859 |
/** |
| 1860 |
* get Temporal.ZonedDateTime.prototype.daysInWeek |
| 1861 |
*/ |
| 1862 |
static bool ZonedDateTime_daysInWeek(JSContext* cx, const CallArgs& args) { |
| 1863 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1864 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1865 |
|
| 1866 |
// Step 3. |
| 1867 |
ISODateTime dateTime; |
| 1868 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1869 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1870 |
return false; |
| 1871 |
} |
| 1872 |
|
| 1873 |
// Step 4. |
| 1874 |
return CalendarDaysInWeek(cx, zonedDateTime.calendar(), dateTime.date, |
| 1875 |
args.rval()); |
| 1876 |
} |
| 1877 |
|
| 1878 |
/** |
| 1879 |
* get Temporal.ZonedDateTime.prototype.daysInWeek |
| 1880 |
*/ |
| 1881 |
static bool ZonedDateTime_daysInWeek(JSContext* cx, unsigned argc, Value* vp) { |
| 1882 |
// Steps 1-2. |
| 1883 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1884 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_daysInWeek>(cx, |
| 1885 |
args); |
| 1886 |
} |
| 1887 |
|
| 1888 |
/** |
| 1889 |
* get Temporal.ZonedDateTime.prototype.daysInMonth |
| 1890 |
*/ |
| 1891 |
static bool ZonedDateTime_daysInMonth(JSContext* cx, const CallArgs& args) { |
| 1892 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1893 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1894 |
|
| 1895 |
// Step 3. |
| 1896 |
ISODateTime dateTime; |
| 1897 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1898 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1899 |
return false; |
| 1900 |
} |
| 1901 |
|
| 1902 |
// Step 4. |
| 1903 |
return CalendarDaysInMonth(cx, zonedDateTime.calendar(), dateTime.date, |
| 1904 |
args.rval()); |
| 1905 |
} |
| 1906 |
|
| 1907 |
/** |
| 1908 |
* get Temporal.ZonedDateTime.prototype.daysInMonth |
| 1909 |
*/ |
| 1910 |
static bool ZonedDateTime_daysInMonth(JSContext* cx, unsigned argc, Value* vp) { |
| 1911 |
// Steps 1-2. |
| 1912 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1913 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_daysInMonth>(cx, |
| 1914 |
args); |
| 1915 |
} |
| 1916 |
|
| 1917 |
/** |
| 1918 |
* get Temporal.ZonedDateTime.prototype.daysInYear |
| 1919 |
*/ |
| 1920 |
static bool ZonedDateTime_daysInYear(JSContext* cx, const CallArgs& args) { |
| 1921 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1922 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1923 |
|
| 1924 |
// Step 3. |
| 1925 |
ISODateTime dateTime; |
| 1926 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1927 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1928 |
return false; |
| 1929 |
} |
| 1930 |
|
| 1931 |
// Step 4. |
| 1932 |
return CalendarDaysInYear(cx, zonedDateTime.calendar(), dateTime.date, |
| 1933 |
args.rval()); |
| 1934 |
} |
| 1935 |
|
| 1936 |
/** |
| 1937 |
* get Temporal.ZonedDateTime.prototype.daysInYear |
| 1938 |
*/ |
| 1939 |
static bool ZonedDateTime_daysInYear(JSContext* cx, unsigned argc, Value* vp) { |
| 1940 |
// Steps 1-2. |
| 1941 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1942 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_daysInYear>(cx, |
| 1943 |
args); |
| 1944 |
} |
| 1945 |
|
| 1946 |
/** |
| 1947 |
* get Temporal.ZonedDateTime.prototype.monthsInYear |
| 1948 |
*/ |
| 1949 |
static bool ZonedDateTime_monthsInYear(JSContext* cx, const CallArgs& args) { |
| 1950 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1951 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1952 |
|
| 1953 |
// Step 3. |
| 1954 |
ISODateTime dateTime; |
| 1955 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1956 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1957 |
return false; |
| 1958 |
} |
| 1959 |
|
| 1960 |
// Step 4. |
| 1961 |
return CalendarMonthsInYear(cx, zonedDateTime.calendar(), dateTime.date, |
| 1962 |
args.rval()); |
| 1963 |
} |
| 1964 |
|
| 1965 |
/** |
| 1966 |
* get Temporal.ZonedDateTime.prototype.monthsInYear |
| 1967 |
*/ |
| 1968 |
static bool ZonedDateTime_monthsInYear(JSContext* cx, unsigned argc, |
| 1969 |
Value* vp) { |
| 1970 |
// Steps 1-2. |
| 1971 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1972 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_monthsInYear>( |
| 1973 |
cx, args); |
| 1974 |
} |
| 1975 |
|
| 1976 |
/** |
| 1977 |
* get Temporal.ZonedDateTime.prototype.inLeapYear |
| 1978 |
*/ |
| 1979 |
static bool ZonedDateTime_inLeapYear(JSContext* cx, const CallArgs& args) { |
| 1980 |
Rooted<ZonedDateTime> zonedDateTime( |
| 1981 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 1982 |
|
| 1983 |
// Step 3. |
| 1984 |
ISODateTime dateTime; |
| 1985 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 1986 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 1987 |
return false; |
| 1988 |
} |
| 1989 |
|
| 1990 |
// Step 4. |
| 1991 |
return CalendarInLeapYear(cx, zonedDateTime.calendar(), dateTime.date, |
| 1992 |
args.rval()); |
| 1993 |
} |
| 1994 |
|
| 1995 |
/** |
| 1996 |
* get Temporal.ZonedDateTime.prototype.inLeapYear |
| 1997 |
*/ |
| 1998 |
static bool ZonedDateTime_inLeapYear(JSContext* cx, unsigned argc, Value* vp) { |
| 1999 |
// Steps 1-2. |
| 2000 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2001 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_inLeapYear>(cx, |
| 2002 |
args); |
| 2003 |
} |
| 2004 |
|
| 2005 |
/** |
| 2006 |
* get Temporal.ZonedDateTime.prototype.offsetNanoseconds |
| 2007 |
*/ |
| 2008 |
static bool ZonedDateTime_offsetNanoseconds(JSContext* cx, |
| 2009 |
const CallArgs& args) { |
| 2010 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2011 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2012 |
|
| 2013 |
// Step 3. |
| 2014 |
int64_t offsetNanoseconds; |
| 2015 |
if (!GetOffsetNanosecondsFor(cx, zonedDateTime.timeZone(), |
| 2016 |
zonedDateTime.epochNanoseconds(), |
| 2017 |
&offsetNanoseconds)) { |
| 2018 |
return false; |
| 2019 |
} |
| 2020 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 2021 |
|
| 2022 |
args.rval().setNumber(offsetNanoseconds); |
| 2023 |
return true; |
| 2024 |
} |
| 2025 |
|
| 2026 |
/** |
| 2027 |
* get Temporal.ZonedDateTime.prototype.offsetNanoseconds |
| 2028 |
*/ |
| 2029 |
static bool ZonedDateTime_offsetNanoseconds(JSContext* cx, unsigned argc, |
| 2030 |
Value* vp) { |
| 2031 |
// Steps 1-2. |
| 2032 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2033 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_offsetNanoseconds>( |
| 2034 |
cx, args); |
| 2035 |
} |
| 2036 |
|
| 2037 |
/** |
| 2038 |
* get Temporal.ZonedDateTime.prototype.offset |
| 2039 |
*/ |
| 2040 |
static bool ZonedDateTime_offset(JSContext* cx, const CallArgs& args) { |
| 2041 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2042 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2043 |
|
| 2044 |
// Step 3. |
| 2045 |
int64_t offsetNanoseconds; |
| 2046 |
if (!GetOffsetNanosecondsFor(cx, zonedDateTime.timeZone(), |
| 2047 |
zonedDateTime.epochNanoseconds(), |
| 2048 |
&offsetNanoseconds)) { |
| 2049 |
return false; |
| 2050 |
} |
| 2051 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 2052 |
|
| 2053 |
// Step 4. |
| 2054 |
JSString* str = FormatUTCOffsetNanoseconds(cx, offsetNanoseconds); |
| 2055 |
if (!str) { |
| 2056 |
return false; |
| 2057 |
} |
| 2058 |
|
| 2059 |
args.rval().setString(str); |
| 2060 |
return true; |
| 2061 |
} |
| 2062 |
|
| 2063 |
/** |
| 2064 |
* get Temporal.ZonedDateTime.prototype.offset |
| 2065 |
*/ |
| 2066 |
static bool ZonedDateTime_offset(JSContext* cx, unsigned argc, Value* vp) { |
| 2067 |
// Steps 1-2. |
| 2068 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2069 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_offset>(cx, args); |
| 2070 |
} |
| 2071 |
|
| 2072 |
/** |
| 2073 |
* Temporal.ZonedDateTime.prototype.with ( temporalZonedDateTimeLike [ , options |
| 2074 |
* ] ) |
| 2075 |
*/ |
| 2076 |
static bool ZonedDateTime_with(JSContext* cx, const CallArgs& args) { |
| 2077 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2078 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2079 |
|
| 2080 |
// Step 3. |
| 2081 |
Rooted<JSObject*> temporalZonedDateTimeLike( |
| 2082 |
cx, |
| 2083 |
RequireObjectArg(cx, "temporalZonedDateTimeLike", "with", args.get(0))); |
| 2084 |
if (!temporalZonedDateTimeLike) { |
| 2085 |
return false; |
| 2086 |
} |
| 2087 |
if (!ThrowIfTemporalLikeObject(cx, temporalZonedDateTimeLike)) { |
| 2088 |
return false; |
| 2089 |
} |
| 2090 |
|
| 2091 |
// Step 4. |
| 2092 |
const auto& epochNs = zonedDateTime.epochNanoseconds(); |
| 2093 |
|
| 2094 |
// Step 5. |
| 2095 |
auto timeZone = zonedDateTime.timeZone(); |
| 2096 |
|
| 2097 |
// Step 6. |
| 2098 |
auto calendar = zonedDateTime.calendar(); |
| 2099 |
|
| 2100 |
// Step 7. |
| 2101 |
int64_t offsetNanoseconds; |
| 2102 |
if (!GetOffsetNanosecondsFor(cx, timeZone, epochNs, &offsetNanoseconds)) { |
| 2103 |
return false; |
| 2104 |
} |
| 2105 |
|
| 2106 |
// Step 8. |
| 2107 |
auto dateTime = GetISODateTimeFor(epochNs, offsetNanoseconds); |
| 2108 |
MOZ_ASSERT(ISODateTimeWithinLimits(dateTime)); |
| 2109 |
|
| 2110 |
// Step 9. |
| 2111 |
Rooted<PlainDate> date(cx, PlainDate{dateTime.date, calendar}); |
| 2112 |
Rooted<CalendarFields> fields(cx); |
| 2113 |
if (!ISODateToFields(cx, date, &fields)) { |
| 2114 |
return false; |
| 2115 |
} |
| 2116 |
|
| 2117 |
// Steps 10-16. |
| 2118 |
fields.setHour(dateTime.time.hour); |
| 2119 |
fields.setMinute(dateTime.time.minute); |
| 2120 |
fields.setSecond(dateTime.time.second); |
| 2121 |
fields.setMillisecond(dateTime.time.millisecond); |
| 2122 |
fields.setMicrosecond(dateTime.time.microsecond); |
| 2123 |
fields.setNanosecond(dateTime.time.nanosecond); |
| 2124 |
fields.setOffset(OffsetField{offsetNanoseconds}); |
| 2125 |
|
| 2126 |
// Step 17. |
| 2127 |
Rooted<CalendarFields> partialZonedDateTime(cx); |
| 2128 |
if (!PreparePartialCalendarFields(cx, calendar, temporalZonedDateTimeLike, |
| 2129 |
{ |
| 2130 |
CalendarField::Year, |
| 2131 |
CalendarField::Month, |
| 2132 |
CalendarField::MonthCode, |
| 2133 |
CalendarField::Day, |
| 2134 |
CalendarField::Hour, |
| 2135 |
CalendarField::Minute, |
| 2136 |
CalendarField::Second, |
| 2137 |
CalendarField::Millisecond, |
| 2138 |
CalendarField::Microsecond, |
| 2139 |
CalendarField::Nanosecond, |
| 2140 |
CalendarField::Offset, |
| 2141 |
}, |
| 2142 |
&partialZonedDateTime)) { |
| 2143 |
return false; |
| 2144 |
} |
| 2145 |
MOZ_ASSERT(!partialZonedDateTime.keys().isEmpty()); |
| 2146 |
|
| 2147 |
// Step 18. |
| 2148 |
fields = CalendarMergeFields(calendar, fields, partialZonedDateTime); |
| 2149 |
|
| 2150 |
// Steps 19-22. |
| 2151 |
auto disambiguation = TemporalDisambiguation::Compatible; |
| 2152 |
auto offset = TemporalOffset::Prefer; |
| 2153 |
auto overflow = TemporalOverflow::Constrain; |
| 2154 |
if (args.hasDefined(1)) { |
| 2155 |
// Step 19. |
| 2156 |
Rooted<JSObject*> options(cx, |
| 2157 |
RequireObjectArg(cx, "options", "with", args[1])); |
| 2158 |
if (!options) { |
| 2159 |
return false; |
| 2160 |
} |
| 2161 |
|
| 2162 |
// Step 20. |
| 2163 |
if (!GetTemporalDisambiguationOption(cx, options, &disambiguation)) { |
| 2164 |
return false; |
| 2165 |
} |
| 2166 |
|
| 2167 |
// Step 21. |
| 2168 |
if (!GetTemporalOffsetOption(cx, options, &offset)) { |
| 2169 |
return false; |
| 2170 |
} |
| 2171 |
|
| 2172 |
// Step 22. |
| 2173 |
if (!GetTemporalOverflowOption(cx, options, &overflow)) { |
| 2174 |
return false; |
| 2175 |
} |
| 2176 |
} |
| 2177 |
|
| 2178 |
// Step 23. |
| 2179 |
ISODateTime dateTimeResult; |
| 2180 |
if (!InterpretTemporalDateTimeFields(cx, calendar, fields, overflow, |
| 2181 |
&dateTimeResult)) { |
| 2182 |
return false; |
| 2183 |
} |
| 2184 |
|
| 2185 |
// Step 24. |
| 2186 |
int64_t newOffsetNanoseconds = int64_t(fields.offset()); |
| 2187 |
|
| 2188 |
// Step 25. |
| 2189 |
EpochNanoseconds epochNanoseconds; |
| 2190 |
if (!InterpretISODateTimeOffset( |
| 2191 |
cx, dateTimeResult, OffsetBehaviour::Option, newOffsetNanoseconds, |
| 2192 |
timeZone, disambiguation, offset, MatchBehaviour::MatchExactly, |
| 2193 |
&epochNanoseconds)) { |
| 2194 |
return false; |
| 2195 |
} |
| 2196 |
|
| 2197 |
// Step 26. |
| 2198 |
auto* result = |
| 2199 |
CreateTemporalZonedDateTime(cx, epochNanoseconds, timeZone, calendar); |
| 2200 |
if (!result) { |
| 2201 |
return false; |
| 2202 |
} |
| 2203 |
|
| 2204 |
args.rval().setObject(*result); |
| 2205 |
return true; |
| 2206 |
} |
| 2207 |
|
| 2208 |
/** |
| 2209 |
* Temporal.ZonedDateTime.prototype.with ( temporalZonedDateTimeLike [ , options |
| 2210 |
* ] ) |
| 2211 |
*/ |
| 2212 |
static bool ZonedDateTime_with(JSContext* cx, unsigned argc, Value* vp) { |
| 2213 |
// Steps 1-2. |
| 2214 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2215 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_with>(cx, args); |
| 2216 |
} |
| 2217 |
|
| 2218 |
/** |
| 2219 |
* Temporal.ZonedDateTime.prototype.withPlainTime ( [ plainTimeLike ] ) |
| 2220 |
*/ |
| 2221 |
static bool ZonedDateTime_withPlainTime(JSContext* cx, const CallArgs& args) { |
| 2222 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2223 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2224 |
|
| 2225 |
// Step 3. |
| 2226 |
auto timeZone = zonedDateTime.timeZone(); |
| 2227 |
|
| 2228 |
// Step 4. |
| 2229 |
auto calendar = zonedDateTime.calendar(); |
| 2230 |
|
| 2231 |
// Step 5. |
| 2232 |
ISODateTime isoDateTime; |
| 2233 |
if (!GetISODateTimeFor(cx, timeZone, zonedDateTime.epochNanoseconds(), |
| 2234 |
&isoDateTime)) { |
| 2235 |
return false; |
| 2236 |
} |
| 2237 |
|
| 2238 |
// Steps 6-7. |
| 2239 |
EpochNanoseconds epochNs; |
| 2240 |
if (!args.hasDefined(0)) { |
| 2241 |
// Step 6.a. |
| 2242 |
if (!GetStartOfDay(cx, timeZone, isoDateTime.date, &epochNs)) { |
| 2243 |
return false; |
| 2244 |
} |
| 2245 |
} else { |
| 2246 |
// Step 7.a. |
| 2247 |
Time time; |
| 2248 |
if (!ToTemporalTime(cx, args[0], &time)) { |
| 2249 |
return false; |
| 2250 |
} |
| 2251 |
|
| 2252 |
// Step 7.b. |
| 2253 |
auto resultISODateTime = ISODateTime{isoDateTime.date, time}; |
| 2254 |
if (!ISODateTimeWithinLimits(resultISODateTime)) { |
| 2255 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2256 |
JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID); |
| 2257 |
return false; |
| 2258 |
} |
| 2259 |
|
| 2260 |
// Step 7.c. |
| 2261 |
if (!GetEpochNanosecondsFor(cx, timeZone, resultISODateTime, |
| 2262 |
TemporalDisambiguation::Compatible, &epochNs)) { |
| 2263 |
return false; |
| 2264 |
} |
| 2265 |
} |
| 2266 |
|
| 2267 |
// Step 8. |
| 2268 |
auto* result = CreateTemporalZonedDateTime(cx, epochNs, timeZone, calendar); |
| 2269 |
if (!result) { |
| 2270 |
return false; |
| 2271 |
} |
| 2272 |
|
| 2273 |
args.rval().setObject(*result); |
| 2274 |
return true; |
| 2275 |
} |
| 2276 |
|
| 2277 |
/** |
| 2278 |
* Temporal.ZonedDateTime.prototype.withPlainTime ( [ plainTimeLike ] ) |
| 2279 |
*/ |
| 2280 |
static bool ZonedDateTime_withPlainTime(JSContext* cx, unsigned argc, |
| 2281 |
Value* vp) { |
| 2282 |
// Steps 1-2. |
| 2283 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2284 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_withPlainTime>( |
| 2285 |
cx, args); |
| 2286 |
} |
| 2287 |
|
| 2288 |
/** |
| 2289 |
* Temporal.ZonedDateTime.prototype.withTimeZone ( timeZoneLike ) |
| 2290 |
*/ |
| 2291 |
static bool ZonedDateTime_withTimeZone(JSContext* cx, const CallArgs& args) { |
| 2292 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2293 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2294 |
|
| 2295 |
// Step 3. |
| 2296 |
Rooted<TimeZoneValue> timeZone(cx); |
| 2297 |
if (!ToTemporalTimeZone(cx, args.get(0), &timeZone)) { |
| 2298 |
return false; |
| 2299 |
} |
| 2300 |
|
| 2301 |
// Step 4. |
| 2302 |
auto* result = CreateTemporalZonedDateTime( |
| 2303 |
cx, zonedDateTime.epochNanoseconds(), timeZone, zonedDateTime.calendar()); |
| 2304 |
if (!result) { |
| 2305 |
return false; |
| 2306 |
} |
| 2307 |
|
| 2308 |
args.rval().setObject(*result); |
| 2309 |
return true; |
| 2310 |
} |
| 2311 |
|
| 2312 |
/** |
| 2313 |
* Temporal.ZonedDateTime.prototype.withTimeZone ( timeZoneLike ) |
| 2314 |
*/ |
| 2315 |
static bool ZonedDateTime_withTimeZone(JSContext* cx, unsigned argc, |
| 2316 |
Value* vp) { |
| 2317 |
// Steps 1-2. |
| 2318 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2319 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_withTimeZone>( |
| 2320 |
cx, args); |
| 2321 |
} |
| 2322 |
|
| 2323 |
/** |
| 2324 |
* Temporal.ZonedDateTime.prototype.withCalendar ( calendarLike ) |
| 2325 |
*/ |
| 2326 |
static bool ZonedDateTime_withCalendar(JSContext* cx, const CallArgs& args) { |
| 2327 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2328 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2329 |
|
| 2330 |
// Step 3. |
| 2331 |
Rooted<CalendarValue> calendar(cx); |
| 2332 |
if (!ToTemporalCalendar(cx, args.get(0), &calendar)) { |
| 2333 |
return false; |
| 2334 |
} |
| 2335 |
|
| 2336 |
// Step 4. |
| 2337 |
auto* result = CreateTemporalZonedDateTime( |
| 2338 |
cx, zonedDateTime.epochNanoseconds(), zonedDateTime.timeZone(), calendar); |
| 2339 |
if (!result) { |
| 2340 |
return false; |
| 2341 |
} |
| 2342 |
|
| 2343 |
args.rval().setObject(*result); |
| 2344 |
return true; |
| 2345 |
} |
| 2346 |
|
| 2347 |
/** |
| 2348 |
* Temporal.ZonedDateTime.prototype.withCalendar ( calendarLike ) |
| 2349 |
*/ |
| 2350 |
static bool ZonedDateTime_withCalendar(JSContext* cx, unsigned argc, |
| 2351 |
Value* vp) { |
| 2352 |
// Steps 1-2. |
| 2353 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2354 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_withCalendar>( |
| 2355 |
cx, args); |
| 2356 |
} |
| 2357 |
|
| 2358 |
/** |
| 2359 |
* Temporal.ZonedDateTime.prototype.add ( temporalDurationLike [ , options ] ) |
| 2360 |
*/ |
| 2361 |
static bool ZonedDateTime_add(JSContext* cx, const CallArgs& args) { |
| 2362 |
// Step 3. |
| 2363 |
return AddDurationToZonedDateTime(cx, TemporalAddDuration::Add, args); |
| 2364 |
} |
| 2365 |
|
| 2366 |
/** |
| 2367 |
* Temporal.ZonedDateTime.prototype.add ( temporalDurationLike [ , options ] ) |
| 2368 |
*/ |
| 2369 |
static bool ZonedDateTime_add(JSContext* cx, unsigned argc, Value* vp) { |
| 2370 |
// Steps 1-2. |
| 2371 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2372 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_add>(cx, args); |
| 2373 |
} |
| 2374 |
|
| 2375 |
/** |
| 2376 |
* Temporal.ZonedDateTime.prototype.subtract ( temporalDurationLike [ , options |
| 2377 |
* ] ) |
| 2378 |
*/ |
| 2379 |
static bool ZonedDateTime_subtract(JSContext* cx, const CallArgs& args) { |
| 2380 |
// Step 3. |
| 2381 |
return AddDurationToZonedDateTime(cx, TemporalAddDuration::Subtract, args); |
| 2382 |
} |
| 2383 |
|
| 2384 |
/** |
| 2385 |
* Temporal.ZonedDateTime.prototype.subtract ( temporalDurationLike [ , options |
| 2386 |
* ] ) |
| 2387 |
*/ |
| 2388 |
static bool ZonedDateTime_subtract(JSContext* cx, unsigned argc, Value* vp) { |
| 2389 |
// Steps 1-2. |
| 2390 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2391 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_subtract>(cx, |
| 2392 |
args); |
| 2393 |
} |
| 2394 |
|
| 2395 |
/** |
| 2396 |
* Temporal.ZonedDateTime.prototype.until ( other [ , options ] ) |
| 2397 |
*/ |
| 2398 |
static bool ZonedDateTime_until(JSContext* cx, const CallArgs& args) { |
| 2399 |
// Step 3. |
| 2400 |
return DifferenceTemporalZonedDateTime(cx, TemporalDifference::Until, args); |
| 2401 |
} |
| 2402 |
|
| 2403 |
/** |
| 2404 |
* Temporal.ZonedDateTime.prototype.until ( other [ , options ] ) |
| 2405 |
*/ |
| 2406 |
static bool ZonedDateTime_until(JSContext* cx, unsigned argc, Value* vp) { |
| 2407 |
// Steps 1-2. |
| 2408 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2409 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_until>(cx, args); |
| 2410 |
} |
| 2411 |
|
| 2412 |
/** |
| 2413 |
* Temporal.ZonedDateTime.prototype.since ( other [ , options ] ) |
| 2414 |
*/ |
| 2415 |
static bool ZonedDateTime_since(JSContext* cx, const CallArgs& args) { |
| 2416 |
// Step 3. |
| 2417 |
return DifferenceTemporalZonedDateTime(cx, TemporalDifference::Since, args); |
| 2418 |
} |
| 2419 |
|
| 2420 |
/** |
| 2421 |
* Temporal.ZonedDateTime.prototype.since ( other [ , options ] ) |
| 2422 |
*/ |
| 2423 |
static bool ZonedDateTime_since(JSContext* cx, unsigned argc, Value* vp) { |
| 2424 |
// Steps 1-2. |
| 2425 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2426 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_since>(cx, args); |
| 2427 |
} |
| 2428 |
|
| 2429 |
/** |
| 2430 |
* Temporal.ZonedDateTime.prototype.round ( roundTo ) |
| 2431 |
*/ |
| 2432 |
static bool ZonedDateTime_round(JSContext* cx, const CallArgs& args) { |
| 2433 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2434 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2435 |
|
| 2436 |
// Steps 3-12. |
| 2437 |
auto smallestUnit = TemporalUnit::Auto; |
| 2438 |
auto roundingMode = TemporalRoundingMode::HalfExpand; |
| 2439 |
auto roundingIncrement = Increment{1}; |
| 2440 |
if (args.get(0).isString()) { |
| 2441 |
// Step 4. (Not applicable in our implementation.) |
| 2442 |
|
| 2443 |
// Step 9. |
| 2444 |
Rooted<JSString*> paramString(cx, args[0].toString()); |
| 2445 |
if (!GetTemporalUnitValuedOption( |
| 2446 |
cx, paramString, TemporalUnitKey::SmallestUnit, |
| 2447 |
TemporalUnitGroup::DayTime, &smallestUnit)) { |
| 2448 |
return false; |
| 2449 |
} |
| 2450 |
|
| 2451 |
// Steps 6-8 and 10-12. (Implicit) |
| 2452 |
} else { |
| 2453 |
// Steps 3 and 5.a |
| 2454 |
Rooted<JSObject*> roundTo( |
| 2455 |
cx, RequireObjectArg(cx, "roundTo", "round", args.get(0))); |
| 2456 |
if (!roundTo) { |
| 2457 |
return false; |
| 2458 |
} |
| 2459 |
|
| 2460 |
// Steps 6-7. |
| 2461 |
if (!GetRoundingIncrementOption(cx, roundTo, &roundingIncrement)) { |
| 2462 |
return false; |
| 2463 |
} |
| 2464 |
|
| 2465 |
// Step 8. |
| 2466 |
if (!GetRoundingModeOption(cx, roundTo, &roundingMode)) { |
| 2467 |
return false; |
| 2468 |
} |
| 2469 |
|
| 2470 |
// Step 9. |
| 2471 |
if (!GetTemporalUnitValuedOption(cx, roundTo, TemporalUnitKey::SmallestUnit, |
| 2472 |
TemporalUnitGroup::DayTime, |
| 2473 |
&smallestUnit)) { |
| 2474 |
return false; |
| 2475 |
} |
| 2476 |
|
| 2477 |
if (smallestUnit == TemporalUnit::Auto) { |
| 2478 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2479 |
JSMSG_TEMPORAL_MISSING_OPTION, "smallestUnit"); |
| 2480 |
return false; |
| 2481 |
} |
| 2482 |
|
| 2483 |
MOZ_ASSERT(TemporalUnit::Day <= smallestUnit && |
| 2484 |
smallestUnit <= TemporalUnit::Nanosecond); |
| 2485 |
|
| 2486 |
// Steps 10-11. |
| 2487 |
auto maximum = Increment{1}; |
| 2488 |
bool inclusive = true; |
| 2489 |
if (smallestUnit > TemporalUnit::Day) { |
| 2490 |
maximum = MaximumTemporalDurationRoundingIncrement(smallestUnit); |
| 2491 |
inclusive = false; |
| 2492 |
} |
| 2493 |
|
| 2494 |
// Step 12. |
| 2495 |
if (!ValidateTemporalRoundingIncrement(cx, roundingIncrement, maximum, |
| 2496 |
inclusive)) { |
| 2497 |
return false; |
| 2498 |
} |
| 2499 |
} |
| 2500 |
|
| 2501 |
// Step 13. |
| 2502 |
if (smallestUnit == TemporalUnit::Nanosecond && |
| 2503 |
roundingIncrement == Increment{1}) { |
| 2504 |
// Step 13.a. |
| 2505 |
auto* result = CreateTemporalZonedDateTime( |
| 2506 |
cx, zonedDateTime.epochNanoseconds(), zonedDateTime.timeZone(), |
| 2507 |
zonedDateTime.calendar()); |
| 2508 |
if (!result) { |
| 2509 |
return false; |
| 2510 |
} |
| 2511 |
|
| 2512 |
args.rval().setObject(*result); |
| 2513 |
return true; |
| 2514 |
} |
| 2515 |
|
| 2516 |
// Step 14. |
| 2517 |
auto thisNs = zonedDateTime.epochNanoseconds(); |
| 2518 |
|
| 2519 |
// Step 15. |
| 2520 |
auto timeZone = zonedDateTime.timeZone(); |
| 2521 |
|
| 2522 |
// Step 16. |
| 2523 |
auto calendar = zonedDateTime.calendar(); |
| 2524 |
|
| 2525 |
// Step 17. |
| 2526 |
ISODateTime isoDateTime; |
| 2527 |
if (!GetISODateTimeFor(cx, timeZone, thisNs, &isoDateTime)) { |
| 2528 |
return false; |
| 2529 |
} |
| 2530 |
|
| 2531 |
// Steps 18-19. |
| 2532 |
EpochNanoseconds epochNanoseconds; |
| 2533 |
if (smallestUnit == TemporalUnit::Day) { |
| 2534 |
// Step 18.a. |
| 2535 |
const auto& dateStart = isoDateTime.date; |
| 2536 |
|
| 2537 |
// Step 18.b. |
| 2538 |
auto dateEnd = BalanceISODate(dateStart, 1); |
| 2539 |
if (!ISODateWithinLimits(dateEnd)) { |
| 2540 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2541 |
JSMSG_TEMPORAL_PLAIN_DATE_INVALID); |
| 2542 |
return false; |
| 2543 |
} |
| 2544 |
|
| 2545 |
// Step 18.c. |
| 2546 |
EpochNanoseconds startNs; |
| 2547 |
if (!GetStartOfDay(cx, timeZone, dateStart, &startNs)) { |
| 2548 |
return false; |
| 2549 |
} |
| 2550 |
|
| 2551 |
// Step 18.d. |
| 2552 |
MOZ_ASSERT(thisNs >= startNs); |
| 2553 |
|
| 2554 |
// Step 18.e. |
| 2555 |
EpochNanoseconds endNs; |
| 2556 |
if (!GetStartOfDay(cx, timeZone, dateEnd, &endNs)) { |
| 2557 |
return false; |
| 2558 |
} |
| 2559 |
|
| 2560 |
// Step 18.f. |
| 2561 |
MOZ_ASSERT(thisNs < endNs); |
| 2562 |
|
| 2563 |
// Step 18.g. |
| 2564 |
auto dayLengthNs = endNs - startNs; |
| 2565 |
MOZ_ASSERT(IsValidEpochDuration(dayLengthNs)); |
| 2566 |
MOZ_ASSERT(dayLengthNs > EpochDuration{}, "dayLengthNs is positive"); |
| 2567 |
|
| 2568 |
// Step 18.h. (Inlined TimeDurationFromEpochNanosecondsDifference) |
| 2569 |
auto dayProgressNs = thisNs - startNs; |
| 2570 |
MOZ_ASSERT(IsValidEpochDuration(dayProgressNs)); |
| 2571 |
MOZ_ASSERT(dayProgressNs >= EpochDuration{}, |
| 2572 |
"dayProgressNs is non-negative"); |
| 2573 |
|
| 2574 |
MOZ_ASSERT(startNs <= thisNs && thisNs < endNs); |
| 2575 |
MOZ_ASSERT(dayProgressNs < dayLengthNs); |
| 2576 |
MOZ_ASSERT(dayLengthNs <= EpochDuration::fromDays(2), |
| 2577 |
"maximum day length for repeated days"); |
| 2578 |
|
| 2579 |
// Step 18.i. (Inlined RoundTimeDurationToIncrement) |
| 2580 |
auto rounded = RoundNumberToIncrement( |
| 2581 |
static_cast<int64_t>(dayProgressNs.toNanoseconds()), |
| 2582 |
static_cast<int64_t>(dayLengthNs.toNanoseconds()), roundingMode); |
| 2583 |
auto roundedDaysNs = EpochDuration::fromNanoseconds(rounded); |
| 2584 |
MOZ_ASSERT(roundedDaysNs == EpochDuration{} || |
| 2585 |
roundedDaysNs == dayLengthNs); |
| 2586 |
MOZ_ASSERT(IsValidEpochDuration(roundedDaysNs)); |
| 2587 |
|
| 2588 |
// Step 18.j. (Inlined AddTimeDurationToEpochNanoseconds) |
| 2589 |
epochNanoseconds = startNs + roundedDaysNs; |
| 2590 |
MOZ_ASSERT(epochNanoseconds == startNs || epochNanoseconds == endNs); |
| 2591 |
} else { |
| 2592 |
// Step 19.a. |
| 2593 |
auto roundResult = RoundISODateTime(isoDateTime, roundingIncrement, |
| 2594 |
smallestUnit, roundingMode); |
| 2595 |
|
| 2596 |
// Step 19.b. |
| 2597 |
int64_t offsetNanoseconds; |
| 2598 |
if (!GetOffsetNanosecondsFor(cx, timeZone, thisNs, &offsetNanoseconds)) { |
| 2599 |
return false; |
| 2600 |
} |
| 2601 |
MOZ_ASSERT(std::abs(offsetNanoseconds) < ToNanoseconds(TemporalUnit::Day)); |
| 2602 |
|
| 2603 |
// Step 19.c. |
| 2604 |
if (!InterpretISODateTimeOffset( |
| 2605 |
cx, roundResult, OffsetBehaviour::Option, offsetNanoseconds, |
| 2606 |
timeZone, TemporalDisambiguation::Compatible, |
| 2607 |
TemporalOffset::Prefer, MatchBehaviour::MatchExactly, |
| 2608 |
&epochNanoseconds)) { |
| 2609 |
return false; |
| 2610 |
} |
| 2611 |
} |
| 2612 |
MOZ_ASSERT(IsValidEpochNanoseconds(epochNanoseconds)); |
| 2613 |
|
| 2614 |
// Step 20. |
| 2615 |
auto* result = |
| 2616 |
CreateTemporalZonedDateTime(cx, epochNanoseconds, timeZone, calendar); |
| 2617 |
if (!result) { |
| 2618 |
return false; |
| 2619 |
} |
| 2620 |
|
| 2621 |
args.rval().setObject(*result); |
| 2622 |
return true; |
| 2623 |
} |
| 2624 |
|
| 2625 |
/** |
| 2626 |
* Temporal.ZonedDateTime.prototype.round ( roundTo ) |
| 2627 |
*/ |
| 2628 |
static bool ZonedDateTime_round(JSContext* cx, unsigned argc, Value* vp) { |
| 2629 |
// Steps 1-2. |
| 2630 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2631 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_round>(cx, args); |
| 2632 |
} |
| 2633 |
|
| 2634 |
/** |
| 2635 |
* Temporal.ZonedDateTime.prototype.equals ( other ) |
| 2636 |
*/ |
| 2637 |
static bool ZonedDateTime_equals(JSContext* cx, const CallArgs& args) { |
| 2638 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2639 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2640 |
|
| 2641 |
// Step 3. |
| 2642 |
Rooted<ZonedDateTime> other(cx); |
| 2643 |
if (!ToTemporalZonedDateTime(cx, args.get(0), &other)) { |
| 2644 |
return false; |
| 2645 |
} |
| 2646 |
|
| 2647 |
// Steps 4-6. |
| 2648 |
bool equals = zonedDateTime.epochNanoseconds() == other.epochNanoseconds() && |
| 2649 |
TimeZoneEquals(zonedDateTime.timeZone(), other.timeZone()) && |
| 2650 |
CalendarEquals(zonedDateTime.calendar(), other.calendar()); |
| 2651 |
|
| 2652 |
args.rval().setBoolean(equals); |
| 2653 |
return true; |
| 2654 |
} |
| 2655 |
|
| 2656 |
/** |
| 2657 |
* Temporal.ZonedDateTime.prototype.equals ( other ) |
| 2658 |
*/ |
| 2659 |
static bool ZonedDateTime_equals(JSContext* cx, unsigned argc, Value* vp) { |
| 2660 |
// Steps 1-2. |
| 2661 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2662 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_equals>(cx, args); |
| 2663 |
} |
| 2664 |
|
| 2665 |
/** |
| 2666 |
* Temporal.ZonedDateTime.prototype.toString ( [ options ] ) |
| 2667 |
*/ |
| 2668 |
static bool ZonedDateTime_toString(JSContext* cx, const CallArgs& args) { |
| 2669 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2670 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2671 |
|
| 2672 |
SecondsStringPrecision precision = {Precision::Auto(), |
| 2673 |
TemporalUnit::Nanosecond, Increment{1}}; |
| 2674 |
auto roundingMode = TemporalRoundingMode::Trunc; |
| 2675 |
auto showCalendar = ShowCalendar::Auto; |
| 2676 |
auto showTimeZone = ShowTimeZoneName::Auto; |
| 2677 |
auto showOffset = ShowOffset::Auto; |
| 2678 |
if (args.hasDefined(0)) { |
| 2679 |
// Step 3. |
| 2680 |
Rooted<JSObject*> options( |
| 2681 |
cx, RequireObjectArg(cx, "options", "toString", args[0])); |
| 2682 |
if (!options) { |
| 2683 |
return false; |
| 2684 |
} |
| 2685 |
|
| 2686 |
// Steps 4-5. |
| 2687 |
if (!GetTemporalShowCalendarNameOption(cx, options, &showCalendar)) { |
| 2688 |
return false; |
| 2689 |
} |
| 2690 |
|
| 2691 |
// Step 6. |
| 2692 |
auto digits = Precision::Auto(); |
| 2693 |
if (!GetTemporalFractionalSecondDigitsOption(cx, options, &digits)) { |
| 2694 |
return false; |
| 2695 |
} |
| 2696 |
|
| 2697 |
// Step 7. |
| 2698 |
if (!GetTemporalShowOffsetOption(cx, options, &showOffset)) { |
| 2699 |
return false; |
| 2700 |
} |
| 2701 |
|
| 2702 |
// Step 8. |
| 2703 |
if (!GetRoundingModeOption(cx, options, &roundingMode)) { |
| 2704 |
return false; |
| 2705 |
} |
| 2706 |
|
| 2707 |
// Step 9. |
| 2708 |
auto smallestUnit = TemporalUnit::Auto; |
| 2709 |
if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit, |
| 2710 |
TemporalUnitGroup::Time, &smallestUnit)) { |
| 2711 |
return false; |
| 2712 |
} |
| 2713 |
|
| 2714 |
// Step 10. |
| 2715 |
if (smallestUnit == TemporalUnit::Hour) { |
| 2716 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 2717 |
JSMSG_TEMPORAL_INVALID_UNIT_OPTION, "hour", |
| 2718 |
"smallestUnit"); |
| 2719 |
return false; |
| 2720 |
} |
| 2721 |
|
| 2722 |
// Step 11. |
| 2723 |
if (!GetTemporalShowTimeZoneNameOption(cx, options, &showTimeZone)) { |
| 2724 |
return false; |
| 2725 |
} |
| 2726 |
|
| 2727 |
// Step 12. |
| 2728 |
precision = ToSecondsStringPrecision(smallestUnit, digits); |
| 2729 |
} |
| 2730 |
|
| 2731 |
// Step 13. |
| 2732 |
JSString* str = TemporalZonedDateTimeToString( |
| 2733 |
cx, zonedDateTime, precision.precision, showCalendar, showTimeZone, |
| 2734 |
showOffset, precision.increment, precision.unit, roundingMode); |
| 2735 |
if (!str) { |
| 2736 |
return false; |
| 2737 |
} |
| 2738 |
|
| 2739 |
args.rval().setString(str); |
| 2740 |
return true; |
| 2741 |
} |
| 2742 |
|
| 2743 |
/** |
| 2744 |
* Temporal.ZonedDateTime.prototype.toString ( [ options ] ) |
| 2745 |
*/ |
| 2746 |
static bool ZonedDateTime_toString(JSContext* cx, unsigned argc, Value* vp) { |
| 2747 |
// Steps 1-2. |
| 2748 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2749 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_toString>(cx, |
| 2750 |
args); |
| 2751 |
} |
| 2752 |
|
| 2753 |
/** |
| 2754 |
* Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ) |
| 2755 |
*/ |
| 2756 |
static bool ZonedDateTime_toLocaleString(JSContext* cx, const CallArgs& args) { |
| 2757 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2758 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2759 |
|
| 2760 |
// Steps 3-6. |
| 2761 |
Handle<PropertyName*> required = cx->names().any; |
| 2762 |
Handle<PropertyName*> defaults = cx->names().all; |
| 2763 |
Rooted<Value> timeZone(cx, |
| 2764 |
StringValue(zonedDateTime.timeZone().identifier())); |
| 2765 |
return TemporalObjectToLocaleString(cx, args, required, defaults, timeZone); |
| 2766 |
} |
| 2767 |
|
| 2768 |
/** |
| 2769 |
* Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ) |
| 2770 |
*/ |
| 2771 |
static bool ZonedDateTime_toLocaleString(JSContext* cx, unsigned argc, |
| 2772 |
Value* vp) { |
| 2773 |
// Steps 1-2. |
| 2774 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2775 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_toLocaleString>( |
| 2776 |
cx, args); |
| 2777 |
} |
| 2778 |
|
| 2779 |
/** |
| 2780 |
* Temporal.ZonedDateTime.prototype.toJSON ( ) |
| 2781 |
*/ |
| 2782 |
static bool ZonedDateTime_toJSON(JSContext* cx, const CallArgs& args) { |
| 2783 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2784 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2785 |
|
| 2786 |
// Step 3. |
| 2787 |
JSString* str = TemporalZonedDateTimeToString( |
| 2788 |
cx, zonedDateTime, Precision::Auto(), ShowCalendar::Auto, |
| 2789 |
ShowTimeZoneName::Auto, ShowOffset::Auto); |
| 2790 |
if (!str) { |
| 2791 |
return false; |
| 2792 |
} |
| 2793 |
|
| 2794 |
args.rval().setString(str); |
| 2795 |
return true; |
| 2796 |
} |
| 2797 |
|
| 2798 |
/** |
| 2799 |
* Temporal.ZonedDateTime.prototype.toJSON ( ) |
| 2800 |
*/ |
| 2801 |
static bool ZonedDateTime_toJSON(JSContext* cx, unsigned argc, Value* vp) { |
| 2802 |
// Steps 1-2. |
| 2803 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2804 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_toJSON>(cx, args); |
| 2805 |
} |
| 2806 |
|
| 2807 |
/** |
| 2808 |
* Temporal.ZonedDateTime.prototype.valueOf ( ) |
| 2809 |
*/ |
| 2810 |
static bool ZonedDateTime_valueOf(JSContext* cx, unsigned argc, Value* vp) { |
| 2811 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CANT_CONVERT_TO, |
| 2812 |
"ZonedDateTime", "primitive type"); |
| 2813 |
return false; |
| 2814 |
} |
| 2815 |
|
| 2816 |
/** |
| 2817 |
* Temporal.ZonedDateTime.prototype.startOfDay ( ) |
| 2818 |
*/ |
| 2819 |
static bool ZonedDateTime_startOfDay(JSContext* cx, const CallArgs& args) { |
| 2820 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2821 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2822 |
|
| 2823 |
// Step 3. |
| 2824 |
auto timeZone = zonedDateTime.timeZone(); |
| 2825 |
|
| 2826 |
// Step 4. |
| 2827 |
auto calendar = zonedDateTime.calendar(); |
| 2828 |
|
| 2829 |
// Step 5. |
| 2830 |
ISODateTime dateTime; |
| 2831 |
if (!GetISODateTimeFor(cx, timeZone, zonedDateTime.epochNanoseconds(), |
| 2832 |
&dateTime)) { |
| 2833 |
return false; |
| 2834 |
} |
| 2835 |
|
| 2836 |
// Step 6. |
| 2837 |
EpochNanoseconds epochNs; |
| 2838 |
if (!GetStartOfDay(cx, timeZone, dateTime.date, &epochNs)) { |
| 2839 |
return false; |
| 2840 |
} |
| 2841 |
|
| 2842 |
// Step 7. |
| 2843 |
auto* result = CreateTemporalZonedDateTime(cx, epochNs, timeZone, calendar); |
| 2844 |
if (!result) { |
| 2845 |
return false; |
| 2846 |
} |
| 2847 |
|
| 2848 |
args.rval().setObject(*result); |
| 2849 |
return true; |
| 2850 |
} |
| 2851 |
|
| 2852 |
/** |
| 2853 |
* Temporal.ZonedDateTime.prototype.startOfDay ( ) |
| 2854 |
*/ |
| 2855 |
static bool ZonedDateTime_startOfDay(JSContext* cx, unsigned argc, Value* vp) { |
| 2856 |
// Steps 1-2. |
| 2857 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2858 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_startOfDay>(cx, |
| 2859 |
args); |
| 2860 |
} |
| 2861 |
|
| 2862 |
/** |
| 2863 |
* Temporal.ZonedDateTime.prototype.getTimeZoneTransition ( directionParam ) |
| 2864 |
*/ |
| 2865 |
static bool ZonedDateTime_getTimeZoneTransition(JSContext* cx, |
| 2866 |
const CallArgs& args) { |
| 2867 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2868 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2869 |
|
| 2870 |
// Step 3. |
| 2871 |
auto timeZone = zonedDateTime.timeZone(); |
| 2872 |
|
| 2873 |
// Steps 4-7. |
| 2874 |
auto direction = Direction::Next; |
| 2875 |
if (args.get(0).isString()) { |
| 2876 |
// Steps 5 and 7. |
| 2877 |
Rooted<JSString*> directionString(cx, args[0].toString()); |
| 2878 |
if (!GetDirectionOption(cx, directionString, &direction)) { |
| 2879 |
return false; |
| 2880 |
} |
| 2881 |
} else { |
| 2882 |
// Steps 4 and 6. |
| 2883 |
Rooted<JSObject*> options(cx, RequireObjectArg(cx, "getTimeZoneTransition", |
| 2884 |
"direction", args.get(0))); |
| 2885 |
if (!options) { |
| 2886 |
return false; |
| 2887 |
} |
| 2888 |
|
| 2889 |
// Step 7. |
| 2890 |
if (!GetDirectionOption(cx, options, &direction)) { |
| 2891 |
return false; |
| 2892 |
} |
| 2893 |
} |
| 2894 |
|
| 2895 |
// Step 8. |
| 2896 |
if (timeZone.isOffset()) { |
| 2897 |
args.rval().setNull(); |
| 2898 |
return true; |
| 2899 |
} |
| 2900 |
|
| 2901 |
// Steps 9-10. |
| 2902 |
mozilla::Maybe<EpochNanoseconds> transition; |
| 2903 |
if (direction == Direction::Next) { |
| 2904 |
if (!GetNamedTimeZoneNextTransition( |
| 2905 |
cx, timeZone, zonedDateTime.epochNanoseconds(), &transition)) { |
| 2906 |
return false; |
| 2907 |
} |
| 2908 |
} else { |
| 2909 |
if (!GetNamedTimeZonePreviousTransition( |
| 2910 |
cx, timeZone, zonedDateTime.epochNanoseconds(), &transition)) { |
| 2911 |
return false; |
| 2912 |
} |
| 2913 |
} |
| 2914 |
|
| 2915 |
// Step 11. |
| 2916 |
if (!transition) { |
| 2917 |
args.rval().setNull(); |
| 2918 |
return true; |
| 2919 |
} |
| 2920 |
|
| 2921 |
// Step 12. |
| 2922 |
auto* result = CreateTemporalZonedDateTime(cx, *transition, timeZone, |
| 2923 |
zonedDateTime.calendar()); |
| 2924 |
if (!result) { |
| 2925 |
return false; |
| 2926 |
} |
| 2927 |
|
| 2928 |
args.rval().setObject(*result); |
| 2929 |
return true; |
| 2930 |
} |
| 2931 |
|
| 2932 |
/** |
| 2933 |
* Temporal.ZonedDateTime.prototype.getTimeZoneTransition ( directionParam ) |
| 2934 |
*/ |
| 2935 |
static bool ZonedDateTime_getTimeZoneTransition(JSContext* cx, unsigned argc, |
| 2936 |
Value* vp) { |
| 2937 |
// Steps 1-2. |
| 2938 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2939 |
return CallNonGenericMethod<IsZonedDateTime, |
| 2940 |
ZonedDateTime_getTimeZoneTransition>(cx, args); |
| 2941 |
} |
| 2942 |
|
| 2943 |
/** |
| 2944 |
* Temporal.ZonedDateTime.prototype.toInstant ( ) |
| 2945 |
*/ |
| 2946 |
static bool ZonedDateTime_toInstant(JSContext* cx, const CallArgs& args) { |
| 2947 |
auto* zonedDateTime = &args.thisv().toObject().as<ZonedDateTimeObject>(); |
| 2948 |
auto epochNs = zonedDateTime->epochNanoseconds(); |
| 2949 |
|
| 2950 |
// Step 3. |
| 2951 |
auto* result = CreateTemporalInstant(cx, epochNs); |
| 2952 |
if (!result) { |
| 2953 |
return false; |
| 2954 |
} |
| 2955 |
|
| 2956 |
args.rval().setObject(*result); |
| 2957 |
return true; |
| 2958 |
} |
| 2959 |
|
| 2960 |
/** |
| 2961 |
* Temporal.ZonedDateTime.prototype.toInstant ( ) |
| 2962 |
*/ |
| 2963 |
static bool ZonedDateTime_toInstant(JSContext* cx, unsigned argc, Value* vp) { |
| 2964 |
// Steps 1-2. |
| 2965 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 2966 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_toInstant>(cx, |
| 2967 |
args); |
| 2968 |
} |
| 2969 |
|
| 2970 |
/** |
| 2971 |
* Temporal.ZonedDateTime.prototype.toPlainDate ( ) |
| 2972 |
*/ |
| 2973 |
static bool ZonedDateTime_toPlainDate(JSContext* cx, const CallArgs& args) { |
| 2974 |
Rooted<ZonedDateTime> zonedDateTime( |
| 2975 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 2976 |
|
| 2977 |
// Step 3. |
| 2978 |
ISODateTime temporalDateTime; |
| 2979 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 2980 |
zonedDateTime.epochNanoseconds(), &temporalDateTime)) { |
| 2981 |
return false; |
| 2982 |
} |
| 2983 |
|
| 2984 |
// Step 4. |
| 2985 |
auto* result = |
| 2986 |
CreateTemporalDate(cx, temporalDateTime.date, zonedDateTime.calendar()); |
| 2987 |
if (!result) { |
| 2988 |
return false; |
| 2989 |
} |
| 2990 |
|
| 2991 |
args.rval().setObject(*result); |
| 2992 |
return true; |
| 2993 |
} |
| 2994 |
|
| 2995 |
/** |
| 2996 |
* Temporal.ZonedDateTime.prototype.toPlainDate ( ) |
| 2997 |
*/ |
| 2998 |
static bool ZonedDateTime_toPlainDate(JSContext* cx, unsigned argc, Value* vp) { |
| 2999 |
// Steps 1-2. |
| 3000 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 3001 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_toPlainDate>(cx, |
| 3002 |
args); |
| 3003 |
} |
| 3004 |
|
| 3005 |
/** |
| 3006 |
* Temporal.ZonedDateTime.prototype.toPlainTime ( ) |
| 3007 |
*/ |
| 3008 |
static bool ZonedDateTime_toPlainTime(JSContext* cx, const CallArgs& args) { |
| 3009 |
Rooted<ZonedDateTime> zonedDateTime( |
| 3010 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 3011 |
|
| 3012 |
// Step 3. |
| 3013 |
ISODateTime temporalDateTime; |
| 3014 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 3015 |
zonedDateTime.epochNanoseconds(), &temporalDateTime)) { |
| 3016 |
return false; |
| 3017 |
} |
| 3018 |
|
| 3019 |
// Step 4. |
| 3020 |
auto* result = CreateTemporalTime(cx, temporalDateTime.time); |
| 3021 |
if (!result) { |
| 3022 |
return false; |
| 3023 |
} |
| 3024 |
|
| 3025 |
args.rval().setObject(*result); |
| 3026 |
return true; |
| 3027 |
} |
| 3028 |
|
| 3029 |
/** |
| 3030 |
* Temporal.ZonedDateTime.prototype.toPlainTime ( ) |
| 3031 |
*/ |
| 3032 |
static bool ZonedDateTime_toPlainTime(JSContext* cx, unsigned argc, Value* vp) { |
| 3033 |
// Steps 1-2. |
| 3034 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 3035 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_toPlainTime>(cx, |
| 3036 |
args); |
| 3037 |
} |
| 3038 |
|
| 3039 |
/** |
| 3040 |
* Temporal.ZonedDateTime.prototype.toPlainDateTime ( ) |
| 3041 |
*/ |
| 3042 |
static bool ZonedDateTime_toPlainDateTime(JSContext* cx, const CallArgs& args) { |
| 3043 |
Rooted<ZonedDateTime> zonedDateTime( |
| 3044 |
cx, ZonedDateTime{&args.thisv().toObject().as<ZonedDateTimeObject>()}); |
| 3045 |
|
| 3046 |
// Step 3. |
| 3047 |
ISODateTime dateTime; |
| 3048 |
if (!GetISODateTimeFor(cx, zonedDateTime.timeZone(), |
| 3049 |
zonedDateTime.epochNanoseconds(), &dateTime)) { |
| 3050 |
return false; |
| 3051 |
} |
| 3052 |
MOZ_ASSERT(ISODateTimeWithinLimits(dateTime)); |
| 3053 |
|
| 3054 |
// Step 4. |
| 3055 |
auto* result = CreateTemporalDateTime(cx, dateTime, zonedDateTime.calendar()); |
| 3056 |
if (!result) { |
| 3057 |
return false; |
| 3058 |
} |
| 3059 |
|
| 3060 |
args.rval().setObject(*result); |
| 3061 |
return true; |
| 3062 |
} |
| 3063 |
|
| 3064 |
/** |
| 3065 |
* Temporal.ZonedDateTime.prototype.toPlainDateTime ( ) |
| 3066 |
*/ |
| 3067 |
static bool ZonedDateTime_toPlainDateTime(JSContext* cx, unsigned argc, |
| 3068 |
Value* vp) { |
| 3069 |
// Steps 1-2. |
| 3070 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 3071 |
return CallNonGenericMethod<IsZonedDateTime, ZonedDateTime_toPlainDateTime>( |
| 3072 |
cx, args); |
| 3073 |
} |
| 3074 |
|
| 3075 |
const JSClass ZonedDateTimeObject::class_ = { |
| 3076 |
"Temporal.ZonedDateTime", |
| 3077 |
JSCLASS_HAS_RESERVED_SLOTS(ZonedDateTimeObject::SLOT_COUNT) | |
| 3078 |
JSCLASS_HAS_CACHED_PROTO(JSProto_ZonedDateTime), |
| 3079 |
JS_NULL_CLASS_OPS, |
| 3080 |
&ZonedDateTimeObject::classSpec_, |
| 3081 |
}; |
| 3082 |
|
| 3083 |
const JSClass& ZonedDateTimeObject::protoClass_ = PlainObject::class_; |
| 3084 |
|
| 3085 |
static const JSFunctionSpec ZonedDateTime_methods[] = { |
| 3086 |
JS_FN("from", ZonedDateTime_from, 1, 0), |
| 3087 |
JS_FN("compare", ZonedDateTime_compare, 2, 0), |
| 3088 |
JS_FS_END, |
| 3089 |
}; |
| 3090 |
|
| 3091 |
static const JSFunctionSpec ZonedDateTime_prototype_methods[] = { |
| 3092 |
JS_FN("with", ZonedDateTime_with, 1, 0), |
| 3093 |
JS_FN("withPlainTime", ZonedDateTime_withPlainTime, 0, 0), |
| 3094 |
JS_FN("withTimeZone", ZonedDateTime_withTimeZone, 1, 0), |
| 3095 |
JS_FN("withCalendar", ZonedDateTime_withCalendar, 1, 0), |
| 3096 |
JS_FN("add", ZonedDateTime_add, 1, 0), |
| 3097 |
JS_FN("subtract", ZonedDateTime_subtract, 1, 0), |
| 3098 |
JS_FN("until", ZonedDateTime_until, 1, 0), |
| 3099 |
JS_FN("since", ZonedDateTime_since, 1, 0), |
| 3100 |
JS_FN("round", ZonedDateTime_round, 1, 0), |
| 3101 |
JS_FN("equals", ZonedDateTime_equals, 1, 0), |
| 3102 |
JS_FN("toString", ZonedDateTime_toString, 0, 0), |
| 3103 |
JS_FN("toLocaleString", ZonedDateTime_toLocaleString, 0, 0), |
| 3104 |
JS_FN("toJSON", ZonedDateTime_toJSON, 0, 0), |
| 3105 |
JS_FN("valueOf", ZonedDateTime_valueOf, 0, 0), |
| 3106 |
JS_FN("startOfDay", ZonedDateTime_startOfDay, 0, 0), |
| 3107 |
JS_FN("getTimeZoneTransition", ZonedDateTime_getTimeZoneTransition, 1, 0), |
| 3108 |
JS_FN("toInstant", ZonedDateTime_toInstant, 0, 0), |
| 3109 |
JS_FN("toPlainDate", ZonedDateTime_toPlainDate, 0, 0), |
| 3110 |
JS_FN("toPlainTime", ZonedDateTime_toPlainTime, 0, 0), |
| 3111 |
JS_FN("toPlainDateTime", ZonedDateTime_toPlainDateTime, 0, 0), |
| 3112 |
JS_FS_END, |
| 3113 |
}; |
| 3114 |
|
| 3115 |
static const JSPropertySpec ZonedDateTime_prototype_properties[] = { |
| 3116 |
JS_PSG("calendarId", ZonedDateTime_calendarId, 0), |
| 3117 |
JS_PSG("timeZoneId", ZonedDateTime_timeZoneId, 0), |
| 3118 |
JS_PSG("era", ZonedDateTime_era, 0), |
| 3119 |
JS_PSG("eraYear", ZonedDateTime_eraYear, 0), |
| 3120 |
JS_PSG("year", ZonedDateTime_year, 0), |
| 3121 |
JS_PSG("month", ZonedDateTime_month, 0), |
| 3122 |
JS_PSG("monthCode", ZonedDateTime_monthCode, 0), |
| 3123 |
JS_PSG("day", ZonedDateTime_day, 0), |
| 3124 |
JS_PSG("hour", ZonedDateTime_hour, 0), |
| 3125 |
JS_PSG("minute", ZonedDateTime_minute, 0), |
| 3126 |
JS_PSG("second", ZonedDateTime_second, 0), |
| 3127 |
JS_PSG("millisecond", ZonedDateTime_millisecond, 0), |
| 3128 |
JS_PSG("microsecond", ZonedDateTime_microsecond, 0), |
| 3129 |
JS_PSG("nanosecond", ZonedDateTime_nanosecond, 0), |
| 3130 |
JS_PSG("epochMilliseconds", ZonedDateTime_epochMilliseconds, 0), |
| 3131 |
JS_PSG("epochNanoseconds", ZonedDateTime_epochNanoseconds, 0), |
| 3132 |
JS_PSG("dayOfWeek", ZonedDateTime_dayOfWeek, 0), |
| 3133 |
JS_PSG("dayOfYear", ZonedDateTime_dayOfYear, 0), |
| 3134 |
JS_PSG("weekOfYear", ZonedDateTime_weekOfYear, 0), |
| 3135 |
JS_PSG("yearOfWeek", ZonedDateTime_yearOfWeek, 0), |
| 3136 |
JS_PSG("hoursInDay", ZonedDateTime_hoursInDay, 0), |
| 3137 |
JS_PSG("daysInWeek", ZonedDateTime_daysInWeek, 0), |
| 3138 |
JS_PSG("daysInMonth", ZonedDateTime_daysInMonth, 0), |
| 3139 |
JS_PSG("daysInYear", ZonedDateTime_daysInYear, 0), |
| 3140 |
JS_PSG("monthsInYear", ZonedDateTime_monthsInYear, 0), |
| 3141 |
JS_PSG("inLeapYear", ZonedDateTime_inLeapYear, 0), |
| 3142 |
JS_PSG("offsetNanoseconds", ZonedDateTime_offsetNanoseconds, 0), |
| 3143 |
JS_PSG("offset", ZonedDateTime_offset, 0), |
| 3144 |
JS_STRING_SYM_PS(toStringTag, "Temporal.ZonedDateTime", JSPROP_READONLY), |
| 3145 |
JS_PS_END, |
| 3146 |
}; |
| 3147 |
|
| 3148 |
const ClassSpec ZonedDateTimeObject::classSpec_ = { |
| 3149 |
GenericCreateConstructor<ZonedDateTimeConstructor, 2, |
| 3150 |
gc::AllocKind::FUNCTION>, |
| 3151 |
GenericCreatePrototype<ZonedDateTimeObject>, |
| 3152 |
ZonedDateTime_methods, |
| 3153 |
nullptr, |
| 3154 |
ZonedDateTime_prototype_methods, |
| 3155 |
ZonedDateTime_prototype_properties, |
| 3156 |
nullptr, |
| 3157 |
ClassSpec::DontDefineConstructor, |
| 3158 |
}; |