DateTimeFormat.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Array.h>
  7. #include <LibJS/Runtime/Date.h>
  8. #include <LibJS/Runtime/Intl/DateTimeFormat.h>
  9. #include <LibJS/Runtime/NativeFunction.h>
  10. #include <math.h>
  11. namespace JS::Intl {
  12. JS_DEFINE_ALLOCATOR(DateTimeFormat);
  13. // 11 DateTimeFormat Objects, https://tc39.es/ecma402/#datetimeformat-objects
  14. DateTimeFormat::DateTimeFormat(Object& prototype)
  15. : Object(ConstructWithPrototypeTag::Tag, prototype)
  16. {
  17. }
  18. void DateTimeFormat::visit_edges(Cell::Visitor& visitor)
  19. {
  20. Base::visit_edges(visitor);
  21. visitor.visit(m_bound_format);
  22. }
  23. // 11.5.5 FormatDateTimePattern ( dateTimeFormat, patternParts, x, rangeFormatOptions ), https://tc39.es/ecma402/#sec-formatdatetimepattern
  24. ThrowCompletionOr<Vector<::Locale::DateTimeFormat::Partition>> format_date_time_pattern(VM& vm, DateTimeFormat& date_time_format, double time)
  25. {
  26. // 1. Let x be TimeClip(x).
  27. time = time_clip(time);
  28. // 2. If x is NaN, throw a RangeError exception.
  29. if (isnan(time))
  30. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidTime);
  31. return date_time_format.formatter().format_to_parts(time);
  32. }
  33. // 11.5.6 PartitionDateTimePattern ( dateTimeFormat, x ), https://tc39.es/ecma402/#sec-partitiondatetimepattern
  34. ThrowCompletionOr<Vector<::Locale::DateTimeFormat::Partition>> partition_date_time_pattern(VM& vm, DateTimeFormat& date_time_format, double time)
  35. {
  36. // 1. Let patternParts be PartitionPattern(dateTimeFormat.[[Pattern]]).
  37. // 2. Let result be ? FormatDateTimePattern(dateTimeFormat, patternParts, x, undefined).
  38. return format_date_time_pattern(vm, date_time_format, time);
  39. }
  40. // 11.5.7 FormatDateTime ( dateTimeFormat, x ), https://tc39.es/ecma402/#sec-formatdatetime
  41. ThrowCompletionOr<String> format_date_time(VM& vm, DateTimeFormat& date_time_format, double time)
  42. {
  43. // 1. Let parts be ? PartitionDateTimePattern(dateTimeFormat, x).
  44. {
  45. // NOTE: We short-circuit PartitionDateTimePattern as we do not need individual partitions. But we must still
  46. // perform the time clip and NaN sanity checks from its call to FormatDateTimePattern.
  47. // 1. Let x be TimeClip(x).
  48. time = time_clip(time);
  49. // 2. If x is NaN, throw a RangeError exception.
  50. if (isnan(time))
  51. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidTime);
  52. }
  53. // 2. Let result be the empty String.
  54. // 3. For each Record { [[Type]], [[Value]] } part in parts, do
  55. // a. Set result to the string-concatenation of result and part.[[Value]].
  56. // 4. Return result.
  57. return date_time_format.formatter().format(time);
  58. }
  59. // 11.5.8 FormatDateTimeToParts ( dateTimeFormat, x ), https://tc39.es/ecma402/#sec-formatdatetimetoparts
  60. ThrowCompletionOr<NonnullGCPtr<Array>> format_date_time_to_parts(VM& vm, DateTimeFormat& date_time_format, double time)
  61. {
  62. auto& realm = *vm.current_realm();
  63. // 1. Let parts be ? PartitionDateTimePattern(dateTimeFormat, x).
  64. auto parts = TRY(partition_date_time_pattern(vm, date_time_format, time));
  65. // 2. Let result be ! ArrayCreate(0).
  66. auto result = MUST(Array::create(realm, 0));
  67. // 3. Let n be 0.
  68. size_t n = 0;
  69. // 4. For each Record { [[Type]], [[Value]] } part in parts, do
  70. for (auto& part : parts) {
  71. // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
  72. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  73. // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
  74. MUST(object->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, part.type)));
  75. // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
  76. MUST(object->create_data_property_or_throw(vm.names.value, PrimitiveString::create(vm, move(part.value))));
  77. // d. Perform ! CreateDataProperty(result, ! ToString(n), O).
  78. MUST(result->create_data_property_or_throw(n, object));
  79. // e. Increment n by 1.
  80. ++n;
  81. }
  82. // 5. Return result.
  83. return result;
  84. }
  85. // 11.5.9 PartitionDateTimeRangePattern ( dateTimeFormat, x, y ), https://tc39.es/ecma402/#sec-partitiondatetimerangepattern
  86. ThrowCompletionOr<Vector<::Locale::DateTimeFormat::Partition>> partition_date_time_range_pattern(VM& vm, DateTimeFormat& date_time_format, double start, double end)
  87. {
  88. // 1. Let x be TimeClip(x).
  89. start = time_clip(start);
  90. // 2. If x is NaN, throw a RangeError exception.
  91. if (isnan(start))
  92. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidTime);
  93. // 3. Let y be TimeClip(y).
  94. end = time_clip(end);
  95. // 4. If y is NaN, throw a RangeError exception.
  96. if (isnan(end))
  97. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidTime);
  98. return date_time_format.formatter().format_range_to_parts(start, end);
  99. }
  100. // 11.5.10 FormatDateTimeRange ( dateTimeFormat, x, y ), https://tc39.es/ecma402/#sec-formatdatetimerange
  101. ThrowCompletionOr<String> format_date_time_range(VM& vm, DateTimeFormat& date_time_format, double start, double end)
  102. {
  103. {
  104. // NOTE: We short-circuit PartitionDateTimeRangePattern as we do not need individual partitions. But we must
  105. // still perform the time clip and NaN sanity checks from its its first steps.
  106. // 1. Let x be TimeClip(x).
  107. start = time_clip(start);
  108. // 2. If x is NaN, throw a RangeError exception.
  109. if (isnan(start))
  110. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidTime);
  111. // 3. Let y be TimeClip(y).
  112. end = time_clip(end);
  113. // 4. If y is NaN, throw a RangeError exception.
  114. if (isnan(end))
  115. return vm.throw_completion<RangeError>(ErrorType::IntlInvalidTime);
  116. }
  117. // 1. Let parts be ? PartitionDateTimeRangePattern(dateTimeFormat, x, y).
  118. // 2. Let result be the empty String.
  119. // 3. For each Record { [[Type]], [[Value]], [[Source]] } part in parts, do
  120. // a. Set result to the string-concatenation of result and part.[[Value]].
  121. // 4. Return result.
  122. return date_time_format.formatter().format_range(start, end);
  123. }
  124. // 11.5.11 FormatDateTimeRangeToParts ( dateTimeFormat, x, y ), https://tc39.es/ecma402/#sec-formatdatetimerangetoparts
  125. ThrowCompletionOr<NonnullGCPtr<Array>> format_date_time_range_to_parts(VM& vm, DateTimeFormat& date_time_format, double start, double end)
  126. {
  127. auto& realm = *vm.current_realm();
  128. // 1. Let parts be ? PartitionDateTimeRangePattern(dateTimeFormat, x, y).
  129. auto parts = TRY(partition_date_time_range_pattern(vm, date_time_format, start, end));
  130. // 2. Let result be ! ArrayCreate(0).
  131. auto result = MUST(Array::create(realm, 0));
  132. // 3. Let n be 0.
  133. size_t n = 0;
  134. // 4. For each Record { [[Type]], [[Value]], [[Source]] } part in parts, do
  135. for (auto& part : parts) {
  136. // a. Let O be OrdinaryObjectCreate(%ObjectPrototype%).
  137. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  138. // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
  139. MUST(object->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, part.type)));
  140. // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
  141. MUST(object->create_data_property_or_throw(vm.names.value, PrimitiveString::create(vm, move(part.value))));
  142. // d. Perform ! CreateDataPropertyOrThrow(O, "source", part.[[Source]]).
  143. MUST(object->create_data_property_or_throw(vm.names.source, PrimitiveString::create(vm, part.source)));
  144. // e. Perform ! CreateDataProperty(result, ! ToString(n), O).
  145. MUST(result->create_data_property_or_throw(n, object));
  146. // f. Increment n by 1.
  147. ++n;
  148. }
  149. // 5. Return result.
  150. return result;
  151. }
  152. }