DurationFormat.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. /*
  2. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringBuilder.h>
  8. #include <LibJS/Runtime/AbstractOperations.h>
  9. #include <LibJS/Runtime/GlobalObject.h>
  10. #include <LibJS/Runtime/Intl/DurationFormat.h>
  11. #include <LibJS/Runtime/Intl/ListFormat.h>
  12. #include <LibJS/Runtime/Intl/ListFormatConstructor.h>
  13. #include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
  14. #include <LibJS/Runtime/Intl/PluralRules.h>
  15. #include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
  16. #include <LibJS/Runtime/Intl/RelativeTimeFormat.h>
  17. #include <LibJS/Runtime/Temporal/AbstractOperations.h>
  18. #include <LibJS/Runtime/ValueInlines.h>
  19. namespace JS::Intl {
  20. JS_DEFINE_ALLOCATOR(DurationFormat);
  21. // 1 DurationFormat Objects, https://tc39.es/proposal-intl-duration-format/#durationformat-objects
  22. DurationFormat::DurationFormat(Object& prototype)
  23. : Object(ConstructWithPrototypeTag::Tag, prototype)
  24. {
  25. }
  26. DurationFormat::Style DurationFormat::style_from_string(StringView style)
  27. {
  28. if (style == "long"sv)
  29. return Style::Long;
  30. if (style == "short"sv)
  31. return Style::Short;
  32. if (style == "narrow"sv)
  33. return Style::Narrow;
  34. if (style == "digital"sv)
  35. return Style::Digital;
  36. VERIFY_NOT_REACHED();
  37. }
  38. StringView DurationFormat::style_to_string(Style style)
  39. {
  40. switch (style) {
  41. case Style::Long:
  42. return "long"sv;
  43. case Style::Short:
  44. return "short"sv;
  45. case Style::Narrow:
  46. return "narrow"sv;
  47. case Style::Digital:
  48. return "digital"sv;
  49. default:
  50. VERIFY_NOT_REACHED();
  51. }
  52. }
  53. DurationFormat::ValueStyle DurationFormat::date_style_from_string(StringView date_style)
  54. {
  55. if (date_style == "long"sv)
  56. return ValueStyle::Long;
  57. if (date_style == "short"sv)
  58. return ValueStyle::Short;
  59. if (date_style == "narrow"sv)
  60. return ValueStyle::Narrow;
  61. VERIFY_NOT_REACHED();
  62. }
  63. DurationFormat::ValueStyle DurationFormat::time_style_from_string(StringView time_style)
  64. {
  65. if (time_style == "long"sv)
  66. return ValueStyle::Long;
  67. if (time_style == "short"sv)
  68. return ValueStyle::Short;
  69. if (time_style == "narrow"sv)
  70. return ValueStyle::Narrow;
  71. if (time_style == "numeric"sv)
  72. return ValueStyle::Numeric;
  73. if (time_style == "2-digit"sv)
  74. return ValueStyle::TwoDigit;
  75. if (time_style == "fractional"sv)
  76. return ValueStyle::Fractional;
  77. VERIFY_NOT_REACHED();
  78. }
  79. DurationFormat::ValueStyle DurationFormat::sub_second_style_from_string(StringView sub_second_style)
  80. {
  81. if (sub_second_style == "long"sv)
  82. return ValueStyle::Long;
  83. if (sub_second_style == "short"sv)
  84. return ValueStyle::Short;
  85. if (sub_second_style == "narrow"sv)
  86. return ValueStyle::Narrow;
  87. if (sub_second_style == "numeric"sv)
  88. return ValueStyle::Numeric;
  89. if (sub_second_style == "fractional"sv)
  90. return ValueStyle::Fractional;
  91. VERIFY_NOT_REACHED();
  92. }
  93. DurationFormat::Display DurationFormat::display_from_string(StringView display)
  94. {
  95. if (display == "auto"sv)
  96. return Display::Auto;
  97. if (display == "always"sv)
  98. return Display::Always;
  99. VERIFY_NOT_REACHED();
  100. }
  101. StringView DurationFormat::value_style_to_string(ValueStyle value_style)
  102. {
  103. switch (value_style) {
  104. case ValueStyle::Long:
  105. return "long"sv;
  106. case ValueStyle::Short:
  107. return "short"sv;
  108. case ValueStyle::Narrow:
  109. return "narrow"sv;
  110. case ValueStyle::Numeric:
  111. return "numeric"sv;
  112. case ValueStyle::TwoDigit:
  113. return "2-digit"sv;
  114. case ValueStyle::Fractional:
  115. return "fractional"sv;
  116. }
  117. VERIFY_NOT_REACHED();
  118. }
  119. StringView DurationFormat::display_to_string(Display display)
  120. {
  121. switch (display) {
  122. case Display::Auto:
  123. return "auto"sv;
  124. case Display::Always:
  125. return "always"sv;
  126. default:
  127. VERIFY_NOT_REACHED();
  128. }
  129. }
  130. static NonnullGCPtr<NumberFormat> construct_number_format(VM& vm, DurationFormat const& duration_format, NonnullGCPtr<Object> options)
  131. {
  132. auto& realm = *vm.current_realm();
  133. auto number_format = MUST(construct(vm, realm.intrinsics().intl_number_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), options));
  134. return static_cast<NumberFormat&>(*number_format);
  135. }
  136. static NonnullGCPtr<ListFormat> construct_list_format(VM& vm, DurationFormat const& duration_format, NonnullGCPtr<Object> options)
  137. {
  138. auto& realm = *vm.current_realm();
  139. auto list_format = MUST(construct(vm, realm.intrinsics().intl_list_format_constructor(), PrimitiveString::create(vm, duration_format.locale()), options));
  140. return static_cast<ListFormat&>(*list_format);
  141. }
  142. // 1.1.3 ToDurationRecord ( input ), https://tc39.es/proposal-intl-duration-format/#sec-todurationrecord
  143. ThrowCompletionOr<Temporal::DurationRecord> to_duration_record(VM& vm, Value input)
  144. {
  145. // 1. If Type(input) is not Object, then
  146. if (!input.is_object()) {
  147. // a. If Type(input) is String, throw a RangeError exception.
  148. if (input.is_string())
  149. return vm.throw_completion<RangeError>(ErrorType::NotAnObject, input);
  150. // b. Throw a TypeError exception.
  151. return vm.throw_completion<TypeError>(ErrorType::NotAnObject, input);
  152. }
  153. auto& input_object = input.as_object();
  154. // 2. Let result be a new Duration Record with each field set to 0.
  155. Temporal::DurationRecord result = {};
  156. bool any_defined = false;
  157. auto set_duration_record_value = [&](auto const& name, auto& value_slot) -> ThrowCompletionOr<void> {
  158. auto value = TRY(input_object.get(name));
  159. if (!value.is_undefined()) {
  160. value_slot = TRY(Temporal::to_integer_if_integral(vm, value, ErrorType::TemporalInvalidDurationPropertyValueNonIntegral, name, value));
  161. any_defined = true;
  162. }
  163. return {};
  164. };
  165. // 3. Let days be ? Get(input, "days").
  166. // 4. If days is not undefined, set result.[[Days]] to ? ToIntegerIfIntegral(days).
  167. TRY(set_duration_record_value(vm.names.days, result.days));
  168. // 5. Let hours be ? Get(input, "hours").
  169. // 6. If hours is not undefined, set result.[[Hours]] to ? ToIntegerIfIntegral(hours).
  170. TRY(set_duration_record_value(vm.names.hours, result.hours));
  171. // 7. Let microseconds be ? Get(input, "microseconds").
  172. // 8. If microseconds is not undefined, set result.[[Microseconds]] to ? ToIntegerIfIntegral(microseconds).
  173. TRY(set_duration_record_value(vm.names.microseconds, result.microseconds));
  174. // 9. Let milliseconds be ? Get(input, "milliseconds").
  175. // 10. If milliseconds is not undefined, set result.[[Milliseconds]] to ? ToIntegerIfIntegral(milliseconds).
  176. TRY(set_duration_record_value(vm.names.milliseconds, result.milliseconds));
  177. // 11. Let minutes be ? Get(input, "minutes").
  178. // 12. If minutes is not undefined, set result.[[Minutes]] to ? ToIntegerIfIntegral(minutes).
  179. TRY(set_duration_record_value(vm.names.minutes, result.minutes));
  180. // 13. Let months be ? Get(input, "months").
  181. // 14. If months is not undefined, set result.[[Months]] to ? ToIntegerIfIntegral(months).
  182. TRY(set_duration_record_value(vm.names.months, result.months));
  183. // 15. Let nanoseconds be ? Get(input, "nanoseconds").
  184. // 16. If nanoseconds is not undefined, set result.[[Nanoseconds]] to ? ToIntegerIfIntegral(nanoseconds).
  185. TRY(set_duration_record_value(vm.names.nanoseconds, result.nanoseconds));
  186. // 17. Let seconds be ? Get(input, "seconds").
  187. // 18. If seconds is not undefined, set result.[[Seconds]] to ? ToIntegerIfIntegral(seconds).
  188. TRY(set_duration_record_value(vm.names.seconds, result.seconds));
  189. // 19. Let weeks be ? Get(input, "weeks").
  190. // 20. If weeks is not undefined, set result.[[Weeks]] to ? ToIntegerIfIntegral(weeks).
  191. TRY(set_duration_record_value(vm.names.weeks, result.weeks));
  192. // 21. Let years be ? Get(input, "years").
  193. // 22. If years is not undefined, set result.[[Years]] to ? ToIntegerIfIntegral(years).
  194. TRY(set_duration_record_value(vm.names.years, result.years));
  195. // 23. If years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, and nanoseconds are all undefined, throw a TypeError exception.
  196. if (!any_defined)
  197. return vm.throw_completion<TypeError>(ErrorType::TemporalInvalidDurationLikeObject);
  198. // 24. If IsValidDuration( result.[[Years]], result.[[Months]], result.[[Weeks]], result.[[Days]], result.[[Hours]], result.[[Minutes]], result.[[Seconds]], result.[[Milliseconds]], result.[[Microseconds]], result.[[Nanoseconds]]) is false, then
  199. if (!Temporal::is_valid_duration(result.years, result.months, result.weeks, result.days, result.hours, result.minutes, result.seconds, result.milliseconds, result.microseconds, result.nanoseconds)) {
  200. // a. Throw a RangeError exception.
  201. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidDurationLikeObject);
  202. }
  203. // 25. Return result.
  204. return result;
  205. }
  206. // 1.1.6 GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle, twoDigitHours ), https://tc39.es/proposal-intl-duration-format/#sec-getdurationunitoptions
  207. ThrowCompletionOr<DurationUnitOptions> get_duration_unit_options(VM& vm, String const& unit, Object const& options, StringView base_style, ReadonlySpan<StringView> styles_list, StringView digital_base, StringView previous_style, bool two_digit_hours)
  208. {
  209. // 1. Let style be ? GetOption(options, unit, string, stylesList, undefined).
  210. auto style_value = TRY(get_option(vm, options, unit.to_byte_string(), OptionType::String, styles_list, Empty {}));
  211. StringView style;
  212. // 2. Let displayDefault be "always".
  213. auto display_default = "always"sv;
  214. // 3. If style is undefined, then
  215. if (style_value.is_undefined()) {
  216. // a. If baseStyle is "digital", then
  217. if (base_style == "digital"sv) {
  218. // i. If unit is not one of "hours", "minutes", or "seconds", then
  219. if (!unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv)) {
  220. // 1. Set displayDefault to "auto".
  221. display_default = "auto"sv;
  222. }
  223. // ii. Set style to digitalBase.
  224. style = digital_base;
  225. }
  226. // b. Else,
  227. else {
  228. // i. If prevStyle is "fractional", "numeric" or "2-digit", then
  229. if (previous_style.is_one_of("fractional"sv, "numeric"sv, "2-digit"sv)) {
  230. // 1. If unit is not one of "minutes" or "seconds", then
  231. if (!unit.is_one_of("minutes"sv, "seconds"sv)) {
  232. // a. Set displayDefault to "auto".
  233. display_default = "auto"sv;
  234. }
  235. // 2. Set style to "numeric".
  236. style = "numeric"sv;
  237. }
  238. // ii. Else,
  239. else {
  240. // 1. Set displayDefault to "auto".
  241. display_default = "auto"sv;
  242. // 2. Set style to baseStyle.
  243. style = base_style;
  244. }
  245. }
  246. } else {
  247. style = style_value.as_string().utf8_string_view();
  248. }
  249. // 4. If style is "numeric", then
  250. if (style == "numeric"sv) {
  251. // a. If unit is one of "milliseconds", "microseconds", or "nanoseconds", then
  252. if (unit.is_one_of("milliseconds"sv, "microseconds"sv, "nanoseconds"sv)) {
  253. // i. Set style to "fractional".
  254. style = "fractional"sv;
  255. // ii. Set displayDefault to "auto".
  256. display_default = "auto"sv;
  257. }
  258. }
  259. // 5. Let displayField be the string-concatenation of unit and "Display".
  260. auto display_field = MUST(String::formatted("{}Display", unit));
  261. // 6. Let display be ? GetOption(options, displayField, string, « "auto", "always" », displayDefault).
  262. auto display_value = TRY(get_option(vm, options, display_field.to_byte_string(), OptionType::String, { "auto"sv, "always"sv }, display_default));
  263. auto display = display_value.as_string().utf8_string();
  264. // 7. If display is "always" and style is "fractional", then
  265. if (display == "always"sv && style == "fractional"sv) {
  266. // a. Throw a RangeError exception.
  267. return vm.throw_completion<RangeError>(ErrorType::IntlFractionalUnitsMixedWithAlwaysDisplay, unit, display_field);
  268. }
  269. // 8. If prevStyle is "fractional", then
  270. if (previous_style == "fractional"sv) {
  271. // a. If style is not "fractional", then
  272. if (style != "fractional"sv) {
  273. // i. Throw a RangeError exception.
  274. return vm.throw_completion<RangeError>(ErrorType::IntlFractionalUnitFollowedByNonFractionalUnit, unit);
  275. }
  276. }
  277. // 9. If prevStyle is "numeric" or "2-digit", then
  278. if (previous_style.is_one_of("numeric"sv, "2-digit"sv)) {
  279. // a. If style is not "fractional", "numeric" or "2-digit", then
  280. if (!style.is_one_of("fractional"sv, "numeric"sv, "2-digit"sv)) {
  281. // i. Throw a RangeError exception.
  282. return vm.throw_completion<RangeError>(ErrorType::IntlNonNumericOr2DigitAfterNumericOr2Digit);
  283. }
  284. // b. If unit is "minutes" or "seconds", then
  285. if (unit.is_one_of("minutes"sv, "seconds"sv)) {
  286. // i. Set style to "2-digit".
  287. style = "2-digit"sv;
  288. }
  289. }
  290. // 10. If unit is "hours" and twoDigitHours is true, then
  291. if (unit == "hours"sv && two_digit_hours) {
  292. // a. Set style to "2-digit".
  293. style = "2-digit"sv;
  294. }
  295. // 11. Return the Record { [[Style]]: style, [[Display]]: display }.
  296. return DurationUnitOptions { .style = MUST(String::from_utf8(style)), .display = move(display) };
  297. }
  298. // 1.1.7 AddFractionalDigits ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-addfractionaldigits
  299. double add_fractional_digits(DurationFormat const& duration_format, Temporal::DurationRecord const& duration)
  300. {
  301. // 1. Let result be 0.
  302. double result = 0;
  303. // 2. Let exponent be 3.
  304. double exponent = 3;
  305. // 3. For each row of Table 2, except the header row, in table order, do
  306. for (auto const& duration_instances_component : duration_instances_components) {
  307. // a. Let style be the value of durationFormat's internal slot whose name is the Style Slot value of the current row.
  308. auto style = (duration_format.*duration_instances_component.get_style_slot)();
  309. // b. If style is "fractional", then
  310. if (style == DurationFormat::ValueStyle::Fractional) {
  311. // i. Assert: The Unit value of the current row is "milliseconds", "microseconds", or "nanoseconds".
  312. VERIFY(duration_instances_component.unit.is_one_of("milliseconds"sv, "microseconds"sv, "nanoseconds"sv));
  313. // ii. Let value be the value of duration's field whose name is the Value Field value of the current row.
  314. auto value = duration.*duration_instances_component.value_slot;
  315. // iii. Set value to value / 10^exponent.
  316. value = value / pow(10, exponent);
  317. // iv. Set result to result + value.
  318. result += value;
  319. // v. Set exponent to exponent + 3.
  320. exponent += 3;
  321. }
  322. }
  323. // 4. Return result.
  324. return result;
  325. }
  326. // 1.1.8 NextUnitFractional ( durationFormat, unit ), https://tc39.es/proposal-intl-duration-format/#sec-nextunitfractional
  327. bool next_unit_fractional(DurationFormat const& duration_format, StringView unit)
  328. {
  329. // 1. Assert: unit is "seconds", "milliseconds", or "microseconds".
  330. VERIFY(unit.is_one_of("seconds"sv, "milliseconds"sv, "microseconds"sv));
  331. // 2. If unit is "seconds" and durationFormat.[[MillisecondsStyle]] is "fractional", return true.
  332. if (unit == "seconds"sv && duration_format.milliseconds_style() == DurationFormat::ValueStyle::Fractional)
  333. return true;
  334. // 3. Else if unit is "milliseconds" and durationFormat.[[MicrosecondsStyle]] is "fractional", return true.
  335. if (unit == "milliseconds"sv && duration_format.microseconds_style() == DurationFormat::ValueStyle::Fractional)
  336. return true;
  337. // 4. Else if unit is "microseconds" and durationFormat.[[NanosecondsStyle]] is "fractional", return true.
  338. if (unit == "microseconds"sv && duration_format.nanoseconds_style() == DurationFormat::ValueStyle::Fractional)
  339. return true;
  340. // 5. Return false.
  341. return false;
  342. }
  343. // 1.1.9 FormatNumericHours ( durationFormat, hoursValue, signDisplayed ), https://tc39.es/proposal-intl-duration-format/#sec-formatnumerichours
  344. Vector<DurationFormatPart> format_numeric_hours(VM& vm, DurationFormat const& duration_format, double hours_value, bool sign_displayed)
  345. {
  346. auto& realm = *vm.current_realm();
  347. // 1. Let hoursStyle be durationFormat.[[HoursStyle]].
  348. auto hours_style = duration_format.hours_style();
  349. // 2. Assert: hoursStyle is "numeric" or hoursStyle is "2-digit".
  350. VERIFY(hours_style == DurationFormat::ValueStyle::Numeric || hours_style == DurationFormat::ValueStyle::TwoDigit);
  351. // 3. Let result be a new empty List.
  352. Vector<DurationFormatPart> result;
  353. // 4. Let nfOpts be OrdinaryObjectCreate(null).
  354. auto number_format_options = Object::create(realm, nullptr);
  355. // 5. Let numberingSystem be durationFormat.[[NumberingSystem]].
  356. auto const& numbering_system = duration_format.numbering_system();
  357. // 6. Perform ! CreateDataPropertyOrThrow(nfOpts, "numberingSystem", numberingSystem).
  358. MUST(number_format_options->create_data_property_or_throw(vm.names.numberingSystem, PrimitiveString::create(vm, numbering_system)));
  359. // 7. If hoursStyle is "2-digit", then
  360. if (hours_style == DurationFormat::ValueStyle::TwoDigit) {
  361. // a. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumIntegerDigits", 2𝔽).
  362. MUST(number_format_options->create_data_property_or_throw(vm.names.minimumIntegerDigits, Value { 2 }));
  363. }
  364. // 8. If signDisplayed is false, then
  365. if (!sign_displayed) {
  366. // a. Perform ! CreateDataPropertyOrThrow(nfOpts, "signDisplay", "never").
  367. MUST(number_format_options->create_data_property_or_throw(vm.names.signDisplay, PrimitiveString::create(vm, "never"sv)));
  368. }
  369. // 9. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
  370. auto number_format = construct_number_format(vm, duration_format, number_format_options);
  371. // 10. Let hoursParts be ! PartitionNumberPattern(nf, hoursValue).
  372. auto hours_parts = partition_number_pattern(number_format, MathematicalValue { hours_value });
  373. // 11. For each Record { [[Type]], [[Value]] } part of hoursParts, do
  374. result.ensure_capacity(hours_parts.size());
  375. for (auto& part : hours_parts) {
  376. // a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: "hour" } to result.
  377. result.unchecked_append({ .type = part.type, .value = move(part.value), .unit = "hour"sv });
  378. }
  379. // 12. Return result.
  380. return result;
  381. }
  382. // 1.1.10 FormatNumericMinutes ( durationFormat, minutesValue, hoursDisplayed, signDisplayed ), https://tc39.es/proposal-intl-duration-format/#sec-formatnumericminutes
  383. Vector<DurationFormatPart> format_numeric_minutes(VM& vm, DurationFormat const& duration_format, double minutes_value, bool hours_displayed, bool sign_displayed)
  384. {
  385. auto& realm = *vm.current_realm();
  386. // 1. Let result be a new empty List.
  387. Vector<DurationFormatPart> result;
  388. // 2. If hoursDisplayed is true, then
  389. if (hours_displayed) {
  390. // a. Let separator be durationFormat.[[DigitalFormat]].[[HoursMinutesSeparator]].
  391. auto separator = duration_format.hours_minutes_separator();
  392. // b. Append the Record { [[Type]]: "literal", [[Value]]: separator, [[Unit]]: empty } to result.
  393. result.append({ .type = "literal"sv, .value = move(separator), .unit = {} });
  394. }
  395. // 3. Let minutesStyle be durationFormat.[[MinutesStyle]].
  396. auto minutes_style = duration_format.minutes_style();
  397. // 4. Assert: minutesStyle is "numeric" or minutesStyle is "2-digit".
  398. VERIFY(minutes_style == DurationFormat::ValueStyle::Numeric || minutes_style == DurationFormat::ValueStyle::TwoDigit);
  399. // 5. Let nfOpts be OrdinaryObjectCreate(null).
  400. auto number_format_options = Object::create(realm, nullptr);
  401. // 6. Let numberingSystem be durationFormat.[[NumberingSystem]].
  402. auto const& numbering_system = duration_format.numbering_system();
  403. // 7. Perform ! CreateDataPropertyOrThrow(nfOpts, "numberingSystem", numberingSystem).
  404. MUST(number_format_options->create_data_property_or_throw(vm.names.numberingSystem, PrimitiveString::create(vm, numbering_system)));
  405. // 8. If minutesStyle is "2-digit", then
  406. if (minutes_style == DurationFormat::ValueStyle::TwoDigit) {
  407. // a. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumIntegerDigits", 2𝔽).
  408. MUST(number_format_options->create_data_property_or_throw(vm.names.minimumIntegerDigits, Value { 2 }));
  409. }
  410. // 9. If signDisplayed is false, then
  411. if (!sign_displayed) {
  412. // a. Perform ! CreateDataPropertyOrThrow(nfOpts, "signDisplay", "never").
  413. MUST(number_format_options->create_data_property_or_throw(vm.names.signDisplay, PrimitiveString::create(vm, "never"sv)));
  414. }
  415. // 10. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
  416. auto number_format = construct_number_format(vm, duration_format, number_format_options);
  417. // 11. Let minutesParts be ! PartitionNumberPattern(nf, minutesValue).
  418. auto minutes_parts = partition_number_pattern(number_format, MathematicalValue { minutes_value });
  419. // 12. For each Record { [[Type]], [[Value]] } part of minutesParts, do
  420. result.ensure_capacity(result.size() + minutes_parts.size());
  421. for (auto& part : minutes_parts) {
  422. // a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: "minute" } to result.
  423. result.unchecked_append({ .type = part.type, .value = move(part.value), .unit = "minute"sv });
  424. }
  425. // 13. Return result.
  426. return result;
  427. }
  428. // 1.1.11 FormatNumericSeconds ( durationFormat, secondsValue, minutesDisplayed, signDisplayed ), https://tc39.es/proposal-intl-duration-format/#sec-formatnumericseconds
  429. Vector<DurationFormatPart> format_numeric_seconds(VM& vm, DurationFormat const& duration_format, double seconds_value, bool minutes_displayed, bool sign_displayed)
  430. {
  431. auto& realm = *vm.current_realm();
  432. // 1. Let secondsStyle be durationFormat.[[SecondsStyle]].
  433. auto seconds_style = duration_format.seconds_style();
  434. // 2. Assert: secondsStyle is "numeric" or secondsStyle is "2-digit".
  435. VERIFY(seconds_style == DurationFormat::ValueStyle::Numeric || seconds_style == DurationFormat::ValueStyle::TwoDigit);
  436. // 3. Let result be a new empty List.
  437. Vector<DurationFormatPart> result;
  438. // 4. If minutesDisplayed is true, then
  439. if (minutes_displayed) {
  440. // a. Let separator be durationFormat.[[DigitalFormat]].[[MinutesSecondsSeparator]].
  441. auto separator = duration_format.minutes_seconds_separator();
  442. // b. Append the Record { [[Type]]: "literal", [[Value]]: separator, [[Unit]]: empty } to result.
  443. result.append({ .type = "literal"sv, .value = move(separator), .unit = {} });
  444. }
  445. // 5. Let nfOpts be OrdinaryObjectCreate(null).
  446. auto number_format_options = Object::create(realm, nullptr);
  447. // 6. Let numberingSystem be durationFormat.[[NumberingSystem]].
  448. auto const& numbering_system = duration_format.numbering_system();
  449. // 7. Perform ! CreateDataPropertyOrThrow(nfOpts, "numberingSystem", numberingSystem).
  450. MUST(number_format_options->create_data_property_or_throw(vm.names.numberingSystem, PrimitiveString::create(vm, numbering_system)));
  451. // 8. If secondsStyle is "2-digit", then
  452. if (seconds_style == DurationFormat::ValueStyle::TwoDigit) {
  453. // a. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumIntegerDigits", 2𝔽).
  454. MUST(number_format_options->create_data_property_or_throw(vm.names.minimumIntegerDigits, Value { 2 }));
  455. }
  456. // 9. If signDisplayed is false, then
  457. if (!sign_displayed) {
  458. // a. Perform ! CreateDataPropertyOrThrow(nfOpts, "signDisplay", "never").
  459. MUST(number_format_options->create_data_property_or_throw(vm.names.signDisplay, PrimitiveString::create(vm, "never"sv)));
  460. }
  461. u8 maximum_fraction_digits = 0;
  462. u8 minimum_fraction_digits = 0;
  463. // 11. If durationFormat.[[FractionalDigits]] is undefined, then
  464. if (!duration_format.has_fractional_digits()) {
  465. // a. Let maximumFractionDigits be 9𝔽.
  466. maximum_fraction_digits = 9;
  467. // b. Let minimumFractionDigits be +0𝔽.
  468. minimum_fraction_digits = 0;
  469. }
  470. // 12. Else,
  471. else {
  472. // a. Let maximumFractionDigits be durationFormat.[[FractionalDigits]].
  473. maximum_fraction_digits = duration_format.fractional_digits();
  474. // b. Let minimumFractionDigits be durationFormat.[[FractionalDigits]].
  475. minimum_fraction_digits = duration_format.fractional_digits();
  476. }
  477. // 13. Perform ! CreateDataPropertyOrThrow(nfOpts, "maximumFractionDigits", maximumFractionDigits).
  478. MUST(number_format_options->create_data_property_or_throw(vm.names.maximumFractionDigits, Value { maximum_fraction_digits }));
  479. // 14. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumFractionDigits", minimumFractionDigits).
  480. MUST(number_format_options->create_data_property_or_throw(vm.names.minimumFractionDigits, Value { minimum_fraction_digits }));
  481. // 15. Perform ! CreateDataPropertyOrThrow(nfOpts, "roundingMode", "trunc").
  482. MUST(number_format_options->create_data_property_or_throw(vm.names.roundingMode, PrimitiveString::create(vm, "trunc"sv)));
  483. // FIXME: We obviously have to create the NumberFormat object after its options are fully initialized.
  484. // https://github.com/tc39/proposal-intl-duration-format/pull/203
  485. // 10. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
  486. auto number_format = construct_number_format(vm, duration_format, number_format_options);
  487. // 16. Let secondsParts be ! PartitionNumberPattern(nf, secondsValue).
  488. auto seconds_parts = partition_number_pattern(number_format, MathematicalValue { seconds_value });
  489. // 17. For each Record { [[Type]], [[Value]] } part of secondsParts, do
  490. result.ensure_capacity(result.size() + seconds_parts.size());
  491. for (auto& part : seconds_parts) {
  492. // a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: "second" } to result.
  493. result.unchecked_append({ .type = part.type, .value = move(part.value), .unit = "second"sv });
  494. }
  495. // 18. Return result.
  496. return result;
  497. }
  498. // 1.1.12 FormatNumericUnits ( durationFormat, duration, firstNumericUnit, signDisplayed )
  499. Vector<DurationFormatPart> format_numeric_units(VM& vm, DurationFormat const& duration_format, Temporal::DurationRecord const& duration, StringView first_numeric_unit, bool sign_displayed)
  500. {
  501. // 1. Assert: firstNumericUnit is "hours", "minutes", or "seconds".
  502. VERIFY(first_numeric_unit.is_one_of("hours"sv, "minutes"sv, "seconds"sv));
  503. // 2. Let numericPartsList be a new empty List.
  504. Vector<DurationFormatPart> numeric_parts_list;
  505. // 3. Let hoursValue be duration.[[Hours]].
  506. auto hours_value = duration.hours;
  507. // 4. Let hoursDisplay be durationFormat.[[HoursDisplay]].
  508. auto hours_display = duration_format.hours_display();
  509. // 5. Let minutesValue be duration.[[Minutes]].
  510. auto minutes_value = duration.minutes;
  511. // 6. Let minutesDisplay be durationFormat.[[MinutesDisplay]].
  512. auto minutes_display = duration_format.minutes_display();
  513. // 7. Let secondsValue be duration.[[Seconds]].
  514. auto seconds_value = duration.seconds;
  515. // 8. If duration.[[Milliseconds]] is not 0 or duration.[[Microseconds]] is not 0 or duration.[[Nanoseconds]] is not 0, then
  516. if (duration.milliseconds != 0 || duration.microseconds != 0 || duration.nanoseconds != 0) {
  517. // a. Set secondsValue to secondsValue + AddFractionalDigits(durationFormat, duration).
  518. seconds_value += add_fractional_digits(duration_format, duration);
  519. }
  520. // 9. Let secondsDisplay be durationFormat.[[SecondsDisplay]].
  521. auto seconds_display = duration_format.seconds_display();
  522. // 10. Let hoursFormatted be false.
  523. auto hours_formatted = false;
  524. // 11. If firstNumericUnit is "hours", then
  525. if (first_numeric_unit == "hours"sv) {
  526. // a. If hoursValue is not 0 or hoursDisplay is not "auto", then
  527. if (hours_value != 0 || hours_display != DurationFormat::Display::Auto) {
  528. // i. Set hoursFormatted to true.
  529. hours_formatted = true;
  530. }
  531. }
  532. // 12. If secondsValue is not 0 or secondsDisplay is not "auto", then
  533. // a. Let secondsFormatted be true.
  534. // 13. Else,
  535. // a. Let secondsFormatted be false.
  536. auto seconds_formatted = seconds_value != 0 || seconds_display != DurationFormat::Display::Auto;
  537. // 14. Let minutesFormatted be false.
  538. auto minutes_formatted = false;
  539. // 15. If firstNumericUnit is "hours" or firstNumericUnit is "minutes", then
  540. if (first_numeric_unit.is_one_of("hours"sv, "minutes"sv)) {
  541. // a. If hoursFormatted is true and secondsFormatted is true, then
  542. if (hours_formatted && seconds_formatted) {
  543. // i. Set minutesFormatted to true.
  544. minutes_formatted = true;
  545. }
  546. // b. Else if minutesValue is not 0 or minutesDisplay is not "auto", then
  547. else if (minutes_value != 0 || minutes_display != DurationFormat::Display::Auto) {
  548. // i. Set minutesFormatted to true.
  549. minutes_formatted = true;
  550. }
  551. }
  552. // 16. If hoursFormatted is true, then
  553. if (hours_formatted) {
  554. // a. Append FormatNumericHours(durationFormat, hoursValue, signDisplayed) to numericPartsList.
  555. numeric_parts_list.extend(format_numeric_hours(vm, duration_format, hours_value, sign_displayed));
  556. // b. Set signDisplayed to false.
  557. sign_displayed = hours_value < 0;
  558. }
  559. // 17. If minutesFormatted is true, then
  560. if (minutes_formatted) {
  561. // a. Append FormatNumericMinutes(durationFormat, minutesValue, hoursFormatted, signDisplayed) to numericPartsList.
  562. numeric_parts_list.extend(format_numeric_minutes(vm, duration_format, minutes_value, hours_formatted, sign_displayed));
  563. // b. Set signDisplayed to false.
  564. sign_displayed = minutes_value < 0;
  565. }
  566. // 18. If secondsFormatted is true, then
  567. if (seconds_formatted) {
  568. // a. Append FormatNumericSeconds(durationFormat, secondsValue, minutesFormatted, signDisplayed) to numericPartsList.
  569. numeric_parts_list.extend(format_numeric_seconds(vm, duration_format, seconds_value, minutes_formatted, sign_displayed));
  570. // b. Set signDisplayed to false.
  571. sign_displayed = seconds_value < 0;
  572. }
  573. // 19. Return numericPartsList.
  574. return numeric_parts_list;
  575. }
  576. // 1.1.13 ListFormatParts ( durationFormat, partitionedPartsList ), https://tc39.es/proposal-intl-duration-format/#sec-listformatparts
  577. Vector<DurationFormatPart> list_format_parts(VM& vm, DurationFormat const& duration_format, Vector<Vector<DurationFormatPart>>& partitioned_parts_list)
  578. {
  579. auto& realm = *vm.current_realm();
  580. // 1. Let lfOpts be OrdinaryObjectCreate(null).
  581. auto list_format_options = Object::create(realm, nullptr);
  582. // 2. Perform ! CreateDataPropertyOrThrow(lfOpts, "type", "unit").
  583. MUST(list_format_options->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, "unit"sv)));
  584. // 3. Let listStyle be durationFormat.[[Style]].
  585. auto list_style = duration_format.style();
  586. // 4. If listStyle is "digital", then
  587. if (list_style == DurationFormat::Style::Digital) {
  588. // a. Set listStyle to "short".
  589. list_style = DurationFormat::Style::Short;
  590. }
  591. // 5. Perform ! CreateDataPropertyOrThrow(lfOpts, "style", listStyle).
  592. auto locale_list_style = ::Locale::style_to_string(static_cast<::Locale::Style>(list_style));
  593. MUST(list_format_options->create_data_property_or_throw(vm.names.style, PrimitiveString::create(vm, locale_list_style)));
  594. // 6. Let lf be ! Construct(%ListFormat%, « durationFormat.[[Locale]], lfOpts »).
  595. auto list_format = construct_list_format(vm, duration_format, list_format_options);
  596. // 7. Let strings be a new empty List.
  597. Vector<String> strings;
  598. strings.ensure_capacity(partitioned_parts_list.size());
  599. // 8. For each element parts of partitionedPartsList, do
  600. for (auto const& parts : partitioned_parts_list) {
  601. // a. Let string be the empty String.
  602. StringBuilder string;
  603. // b. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
  604. for (auto const& part : parts) {
  605. // i. Set string to the string-concatenation of string and part.[[Value]].
  606. string.append(part.value);
  607. }
  608. // c. Append string to strings.
  609. strings.unchecked_append(MUST(string.to_string()));
  610. }
  611. // 9. Let formattedPartsList be CreatePartsFromList(lf, strings).
  612. auto formatted_parts_list = create_parts_from_list(list_format, strings);
  613. // 10. Let partitionedPartsIndex be 0.
  614. size_t partitioned_parts_index = 0;
  615. // 11. Let partitionedLength be the number of elements in partitionedPartsList.
  616. auto partitioned_length = partitioned_parts_list.size();
  617. // 12. Let flattenedPartsList be a new empty List.
  618. Vector<DurationFormatPart> flattened_parts_list;
  619. // 13. For each Record { [[Type]], [[Value]] } listPart in formattedPartsList, do
  620. for (auto& list_part : formatted_parts_list) {
  621. // a. If listPart.[[Type]] is "element", then
  622. if (list_part.type == "element"sv) {
  623. // i. Assert: partitionedPartsIndex < partitionedLength.
  624. VERIFY(partitioned_parts_index < partitioned_length);
  625. // ii. Let parts be partitionedPartsList[partitionedPartsIndex].
  626. auto& parts = partitioned_parts_list[partitioned_parts_index];
  627. // iii. For each Record { [[Type]], [[Value]], [[Unit]] } part in parts, do
  628. for (auto& part : parts) {
  629. // 1. Append part to flattenedPartsList.
  630. flattened_parts_list.append(move(part));
  631. }
  632. // iv. Set partitionedPartsIndex to partitionedPartsIndex + 1.
  633. ++partitioned_parts_index;
  634. }
  635. // b. Else,
  636. else {
  637. // i. Assert: listPart.[[Type]] is "literal".
  638. VERIFY(list_part.type == "literal"sv);
  639. // ii. Append the Record { [[Type]]: "literal", [[Value]]: listPart.[[Value]], [[Unit]]: empty } to flattenedPartsList.
  640. flattened_parts_list.append({ .type = "literal"sv, .value = move(list_part.value), .unit = {} });
  641. }
  642. }
  643. // 14. Return flattenedPartsList.
  644. return flattened_parts_list;
  645. }
  646. // 1.1.7 PartitionDurationFormatPattern ( durationFormat, duration ), https://tc39.es/proposal-intl-duration-format/#sec-partitiondurationformatpattern
  647. Vector<DurationFormatPart> partition_duration_format_pattern(VM& vm, DurationFormat const& duration_format, Temporal::DurationRecord const& duration)
  648. {
  649. auto& realm = *vm.current_realm();
  650. // 1. Let result be a new empty List.
  651. Vector<Vector<DurationFormatPart>> result;
  652. // 2. Let signDisplayed be true.
  653. auto sign_displayed = true;
  654. // 3. Let numericUnitFound be false.
  655. auto numeric_unit_found = false;
  656. // 4. While numericUnitFound is false, repeat for each row in Table 2 in table order, except the header row:
  657. for (size_t i = 0; !numeric_unit_found && i < duration_instances_components.size(); ++i) {
  658. auto const& duration_instances_component = duration_instances_components[i];
  659. // a. Let value be the value of duration's field whose name is the Value Field value of the current row.
  660. auto value = duration.*duration_instances_component.value_slot;
  661. // b. Let style be the value of durationFormat's internal slot whose name is the Style Slot value of the current row.
  662. auto style = (duration_format.*duration_instances_component.get_style_slot)();
  663. // c. Let display be the value of durationFormat's internal slot whose name is the Display Slot value of the current row.
  664. auto display = (duration_format.*duration_instances_component.get_display_slot)();
  665. // d. Let unit be the Unit value of the current row.
  666. auto unit = duration_instances_component.unit;
  667. // e. If style is "numeric" or "2-digit", then
  668. if (style == DurationFormat::ValueStyle::Numeric || style == DurationFormat::ValueStyle::TwoDigit) {
  669. // i. Append FormatNumericUnits(durationFormat, duration, unit, signDisplayed) to result.
  670. result.append(format_numeric_units(vm, duration_format, duration, unit, sign_displayed));
  671. // ii. Set numericUnitFound to true.
  672. numeric_unit_found = true;
  673. }
  674. // f. Else,
  675. else {
  676. // i. Let nfOpts be OrdinaryObjectCreate(null).
  677. auto number_format_options = Object::create(realm, nullptr);
  678. // ii. If unit is "seconds", "milliseconds", or "microseconds", then
  679. if (unit.is_one_of("seconds"sv, "milliseconds"sv, "microseconds"sv)) {
  680. // 1. If NextUnitFractional(durationFormat, unit) is true, then
  681. if (next_unit_fractional(duration_format, unit)) {
  682. // a. Set value to value + AddFractionalDigits(durationFormat, duration).
  683. value += add_fractional_digits(duration_format, duration);
  684. u8 maximum_fraction_digits = 0;
  685. u8 minimum_fraction_digits = 0;
  686. // b. If durationFormat.[[FractionalDigits]] is undefined, then
  687. if (!duration_format.has_fractional_digits()) {
  688. // a. Let maximumFractionDigits be 9𝔽.
  689. maximum_fraction_digits = 9;
  690. // b. Let minimumFractionDigits be +0𝔽.
  691. minimum_fraction_digits = 0;
  692. }
  693. // c. Else,
  694. else {
  695. // a. Let maximumFractionDigits be durationFormat.[[FractionalDigits]].
  696. maximum_fraction_digits = duration_format.fractional_digits();
  697. // b. Let minimumFractionDigits be durationFormat.[[FractionalDigits]].
  698. minimum_fraction_digits = duration_format.fractional_digits();
  699. }
  700. // d. Perform ! CreateDataPropertyOrThrow(nfOpts, "maximumFractionDigits", maximumFractionDigits).
  701. MUST(number_format_options->create_data_property_or_throw(vm.names.maximumFractionDigits, Value { maximum_fraction_digits }));
  702. // e. Perform ! CreateDataPropertyOrThrow(nfOpts, "minimumFractionDigits", minimumFractionDigits).
  703. MUST(number_format_options->create_data_property_or_throw(vm.names.minimumFractionDigits, Value { minimum_fraction_digits }));
  704. // f. Perform ! CreateDataPropertyOrThrow(nfOpts, "roundingMode", "trunc").
  705. MUST(number_format_options->create_data_property_or_throw(vm.names.roundingMode, PrimitiveString::create(vm, "trunc"sv)));
  706. // g. Set numericUnitFound to true.
  707. numeric_unit_found = true;
  708. }
  709. }
  710. // iii. If value is not 0 or display is not "auto", then
  711. if (value != 0 || display != DurationFormat::Display::Auto) {
  712. // 1. Let numberingSystem be durationFormat.[[NumberingSystem]].
  713. auto const& numbering_system = duration_format.numbering_system();
  714. // 2. Perform ! CreateDataPropertyOrThrow(nfOpts, "numberingSystem", numberingSystem).
  715. MUST(number_format_options->create_data_property_or_throw(vm.names.numberingSystem, PrimitiveString::create(vm, numbering_system)));
  716. // 3. If signDisplayed is true, then
  717. if (sign_displayed) {
  718. // a. Set signDisplayed to false.
  719. sign_displayed = false;
  720. // b. If value is 0 and DurationSign(duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]]) is -1, then
  721. if (value == 0 && Temporal::duration_sign(duration.years, duration.months, duration.weeks, duration.days, duration.hours, duration.minutes, duration.seconds, duration.milliseconds, duration.microseconds, duration.nanoseconds) == -1) {
  722. // i. Set value to negative-zero.
  723. value = -0.0;
  724. }
  725. }
  726. // 4. Else,
  727. else {
  728. // a. Perform ! CreateDataPropertyOrThrow(nfOpts, "signDisplay", "never").
  729. MUST(number_format_options->create_data_property_or_throw(vm.names.signDisplay, PrimitiveString::create(vm, "never"sv)));
  730. }
  731. // 5. Let numberFormatUnit be the NumberFormat Unit value of the current row.
  732. auto number_format_unit = duration_instances_component.number_format_unit;
  733. // 6. Perform ! CreateDataPropertyOrThrow(nfOpts, "style", "unit").
  734. MUST(number_format_options->create_data_property_or_throw(vm.names.style, PrimitiveString::create(vm, "unit"sv)));
  735. // 7. Perform ! CreateDataPropertyOrThrow(nfOpts, "unit", numberFormatUnit).
  736. MUST(number_format_options->create_data_property_or_throw(vm.names.unit, PrimitiveString::create(vm, number_format_unit)));
  737. // 8. Perform ! CreateDataPropertyOrThrow(nfOpts, "unitDisplay", style).
  738. auto locale_style = ::Locale::style_to_string(static_cast<::Locale::Style>(style));
  739. MUST(number_format_options->create_data_property_or_throw(vm.names.unitDisplay, PrimitiveString::create(vm, locale_style)));
  740. // 9. Let nf be ! Construct(%NumberFormat%, « durationFormat.[[Locale]], nfOpts »).
  741. auto number_format = construct_number_format(vm, duration_format, number_format_options);
  742. // 10. Let parts be ! PartitionNumberPattern(nf, value).
  743. auto parts = partition_number_pattern(number_format, MathematicalValue { value });
  744. // 11. Let list be a new empty List.
  745. Vector<DurationFormatPart> list;
  746. // 12. For each Record { [[Type]], [[Value]] } part of parts, do
  747. list.ensure_capacity(parts.size());
  748. for (auto& part : parts) {
  749. // a. Append the Record { [[Type]]: part.[[Type]], [[Value]]: part.[[Value]], [[Unit]]: numberFormatUnit } to list.
  750. list.unchecked_append({ .type = part.type, .value = move(part.value), .unit = number_format_unit });
  751. }
  752. // 13. Append list to result.
  753. result.append(list);
  754. }
  755. }
  756. }
  757. // 5. Return ListFormatParts(durationFormat, result).
  758. return list_format_parts(vm, duration_format, result);
  759. }
  760. }