PlainTime.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Assertions.h>
  9. #include <LibJS/Runtime/AbstractOperations.h>
  10. #include <LibJS/Runtime/Temporal/Duration.h>
  11. #include <LibJS/Runtime/Temporal/Instant.h>
  12. #include <LibJS/Runtime/Temporal/PlainTime.h>
  13. #include <math.h>
  14. namespace JS::Temporal {
  15. // FIXME: We should add a generic floor() method to our BigInt classes. But for now, since we know we are only dividing
  16. // by powers of 10, we can implement a very situationally specific method to compute the floor of a division.
  17. static TimeDuration big_floor(TimeDuration const& numerator, Crypto::UnsignedBigInteger const& denominator)
  18. {
  19. auto result = numerator.divided_by(denominator);
  20. if (result.remainder.is_zero())
  21. return result.quotient;
  22. if (!result.quotient.is_negative() && result.remainder.is_positive())
  23. return result.quotient;
  24. return result.quotient.minus(TimeDuration { 1 });
  25. }
  26. // 4.5.2 CreateTimeRecord ( hour, minute, second, millisecond, microsecond, nanosecond [ , deltaDays ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtimerecord
  27. Time create_time_record(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, double delta_days)
  28. {
  29. // 1. If deltaDays is not present, set deltaDays to 0.
  30. // 2. Assert: IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond).
  31. VERIFY(is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond));
  32. // 3. Return Time Record { [[Days]]: deltaDays, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
  33. return {
  34. .days = delta_days,
  35. .hour = static_cast<u8>(hour),
  36. .minute = static_cast<u8>(minute),
  37. .second = static_cast<u8>(second),
  38. .millisecond = static_cast<u16>(millisecond),
  39. .microsecond = static_cast<u16>(microsecond),
  40. .nanosecond = static_cast<u16>(nanosecond),
  41. };
  42. }
  43. // 4.5.3 MidnightTimeRecord ( ), https://tc39.es/proposal-temporal/#sec-temporal-midnighttimerecord
  44. Time midnight_time_record()
  45. {
  46. // 1. Return Time Record { [[Days]]: 0, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
  47. return { .days = 0, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
  48. }
  49. // 4.5.4 NoonTimeRecord ( ), https://tc39.es/proposal-temporal/#sec-temporal-noontimerecord
  50. Time noon_time_record()
  51. {
  52. // 1. Return Time Record { [[Days]]: 0, [[Hour]]: 12, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
  53. return { .days = 0, .hour = 12, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 };
  54. }
  55. // 4.5.5 DifferenceTime ( time1, time2 ), https://tc39.es/proposal-temporal/#sec-temporal-differencetime
  56. TimeDuration difference_time(Time const& time1, Time const& time2)
  57. {
  58. // 1. Let hours be time2.[[Hour]] - time1.[[Hour]].
  59. auto hours = static_cast<double>(time2.hour) - static_cast<double>(time1.hour);
  60. // 2. Let minutes be time2.[[Minute]] - time1.[[Minute]].
  61. auto minutes = static_cast<double>(time2.minute) - static_cast<double>(time1.minute);
  62. // 3. Let seconds be time2.[[Second]] - time1.[[Second]].
  63. auto seconds = static_cast<double>(time2.second) - static_cast<double>(time1.second);
  64. // 4. Let milliseconds be time2.[[Millisecond]] - time1.[[Millisecond]].
  65. auto milliseconds = static_cast<double>(time2.millisecond) - static_cast<double>(time1.millisecond);
  66. // 5. Let microseconds be time2.[[Microsecond]] - time1.[[Microsecond]].
  67. auto microseconds = static_cast<double>(time2.microsecond) - static_cast<double>(time1.microsecond);
  68. // 6. Let nanoseconds be time2.[[Nanosecond]] - time1.[[Nanosecond]].
  69. auto nanoseconds = static_cast<double>(time2.nanosecond) - static_cast<double>(time1.nanosecond);
  70. // 7. Let timeDuration be TimeDurationFromComponents(hours, minutes, seconds, milliseconds, microseconds, nanoseconds).
  71. auto time_duration = time_duration_from_components(hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
  72. // 8. Assert: abs(timeDuration) < nsPerDay.
  73. VERIFY(time_duration.unsigned_value() < NANOSECONDS_PER_DAY);
  74. // 9. Return timeDuration.
  75. return time_duration;
  76. }
  77. // 4.5.8 RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulatetime
  78. ThrowCompletionOr<Time> regulate_time(VM& vm, double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, Overflow overflow)
  79. {
  80. switch (overflow) {
  81. // 1. If overflow is CONSTRAIN, then
  82. case Overflow::Constrain:
  83. // a. Set hour to the result of clamping hour between 0 and 23.
  84. hour = clamp(hour, 0, 23);
  85. // b. Set minute to the result of clamping minute between 0 and 59.
  86. minute = clamp(minute, 0, 59);
  87. // c. Set second to the result of clamping second between 0 and 59.
  88. second = clamp(second, 0, 59);
  89. // d. Set millisecond to the result of clamping millisecond between 0 and 999.
  90. millisecond = clamp(millisecond, 0, 999);
  91. // e. Set microsecond to the result of clamping microsecond between 0 and 999.
  92. microsecond = clamp(microsecond, 0, 999);
  93. // f. Set nanosecond to the result of clamping nanosecond between 0 and 999.
  94. nanosecond = clamp(nanosecond, 0, 999);
  95. break;
  96. // 2. Else,
  97. case Overflow::Reject:
  98. // a. Assert: overflow is REJECT.
  99. // b. If IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond) is false, throw a RangeError exception.
  100. if (!is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond))
  101. return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidPlainTime);
  102. break;
  103. }
  104. // 3. Return CreateTimeRecord(hour, minute, second, millisecond, microsecond,nanosecond).
  105. return create_time_record(hour, minute, second, millisecond, microsecond, nanosecond);
  106. }
  107. // 4.5.9 IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidtime
  108. bool is_valid_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  109. {
  110. // 1. If hour < 0 or hour > 23, then
  111. if (hour < 0 || hour > 23) {
  112. // a. Return false.
  113. return false;
  114. }
  115. // 2. If minute < 0 or minute > 59, then
  116. if (minute < 0 || minute > 59) {
  117. // a. Return false.
  118. return false;
  119. }
  120. // 3. If second < 0 or second > 59, then
  121. if (second < 0 || second > 59) {
  122. // a. Return false.
  123. return false;
  124. }
  125. // 4. If millisecond < 0 or millisecond > 999, then
  126. if (millisecond < 0 || millisecond > 999) {
  127. // a. Return false.
  128. return false;
  129. }
  130. // 5. If microsecond < 0 or microsecond > 999, then
  131. if (microsecond < 0 || microsecond > 999) {
  132. // a. Return false.
  133. return false;
  134. }
  135. // 6. If nanosecond < 0 or nanosecond > 999, then
  136. if (nanosecond < 0 || nanosecond > 999) {
  137. // a. Return false.
  138. return false;
  139. }
  140. // 7. Return true.
  141. return true;
  142. }
  143. // 4.5.10 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  144. Time balance_time(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond)
  145. {
  146. // 1. Set microsecond to microsecond + floor(nanosecond / 1000).
  147. microsecond += floor(nanosecond / 1000.0);
  148. // 2. Set nanosecond to nanosecond modulo 1000.
  149. nanosecond = modulo(nanosecond, 1000.0);
  150. // 3. Set millisecond to millisecond + floor(microsecond / 1000).
  151. millisecond += floor(microsecond / 1000.0);
  152. // 4. Set microsecond to microsecond modulo 1000.
  153. microsecond = modulo(microsecond, 1000.0);
  154. // 5. Set second to second + floor(millisecond / 1000).
  155. second += floor(millisecond / 1000.0);
  156. // 6. Set millisecond to millisecond modulo 1000.
  157. millisecond = modulo(millisecond, 1000.0);
  158. // 7. Set minute to minute + floor(second / 60).
  159. minute += floor(second / 60.0);
  160. // 8. Set second to second modulo 60.
  161. second = modulo(second, 60.0);
  162. // 9. Set hour to hour + floor(minute / 60).
  163. hour += floor(minute / 60.0);
  164. // 10. Set minute to minute modulo 60.
  165. minute = modulo(minute, 60.0);
  166. // 11. Let deltaDays be floor(hour / 24).
  167. auto delta_days = floor(hour / 24.0);
  168. // 12. Set hour to hour modulo 24.
  169. hour = modulo(hour, 24.0);
  170. // 13. Return CreateTimeRecord(hour, minute, second, millisecond, microsecond, nanosecond, deltaDays).
  171. return create_time_record(hour, minute, second, millisecond, microsecond, nanosecond, delta_days);
  172. }
  173. // 4.5.10 BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/proposal-temporal/#sec-temporal-balancetime
  174. Time balance_time(double hour, double minute, double second, double millisecond, double microsecond, TimeDuration const& nanosecond_value)
  175. {
  176. // 1. Set microsecond to microsecond + floor(nanosecond / 1000).
  177. auto microsecond_value = TimeDuration { microsecond }.plus(big_floor(nanosecond_value, NANOSECONDS_PER_MICROSECOND));
  178. // 2. Set nanosecond to nanosecond modulo 1000.
  179. auto nanosecond = modulo(nanosecond_value, NANOSECONDS_PER_MICROSECOND).to_double();
  180. // 3. Set millisecond to millisecond + floor(microsecond / 1000).
  181. auto millisecond_value = TimeDuration { millisecond }.plus(big_floor(microsecond_value, MICROSECONDS_PER_MILLISECOND));
  182. // 4. Set microsecond to microsecond modulo 1000.
  183. microsecond = modulo(microsecond_value, MICROSECONDS_PER_MILLISECOND).to_double();
  184. // 5. Set second to second + floor(millisecond / 1000).
  185. auto second_value = TimeDuration { second }.plus(big_floor(millisecond_value, MILLISECONDS_PER_SECOND));
  186. // 6. Set millisecond to millisecond modulo 1000.
  187. millisecond = modulo(millisecond_value, MILLISECONDS_PER_SECOND).to_double();
  188. // 7. Set minute to minute + floor(second / 60).
  189. auto minute_value = TimeDuration { minute }.plus(big_floor(second_value, SECONDS_PER_MINUTE));
  190. // 8. Set second to second modulo 60.
  191. second = modulo(second_value, SECONDS_PER_MINUTE).to_double();
  192. // 9. Set hour to hour + floor(minute / 60).
  193. auto hour_value = TimeDuration { hour }.plus(big_floor(minute_value, MINUTES_PER_HOUR));
  194. // 10. Set minute to minute modulo 60.
  195. minute = modulo(minute_value, MINUTES_PER_HOUR).to_double();
  196. // 11. Let deltaDays be floor(hour / 24).
  197. auto delta_days = big_floor(hour_value, HOURS_PER_DAY).to_double();
  198. // 12. Set hour to hour modulo 24.
  199. hour = modulo(hour_value, HOURS_PER_DAY).to_double();
  200. // 13. Return CreateTimeRecord(hour, minute, second, millisecond, microsecond, nanosecond, deltaDays).
  201. return create_time_record(hour, minute, second, millisecond, microsecond, nanosecond, delta_days);
  202. }
  203. // 4.5.14 CompareTimeRecord ( time1, time2 ), https://tc39.es/proposal-temporal/#sec-temporal-comparetimerecord
  204. i8 compare_time_record(Time const& time1, Time const& time2)
  205. {
  206. // 1. If time1.[[Hour]] > time2.[[Hour]], return 1.
  207. if (time1.hour > time2.hour)
  208. return 1;
  209. // 2. If time1.[[Hour]] < time2.[[Hour]], return -1.
  210. if (time1.hour < time2.hour)
  211. return -1;
  212. // 3. If time1.[[Minute]] > time2.[[Minute]], return 1.
  213. if (time1.minute > time2.minute)
  214. return 1;
  215. // 4. If time1.[[Minute]] < time2.[[Minute]], return -1.
  216. if (time1.minute < time2.minute)
  217. return -1;
  218. // 5. If time1.[[Second]] > time2.[[Second]], return 1.
  219. if (time1.second > time2.second)
  220. return 1;
  221. // 6. If time1.[[Second]] < time2.[[Second]], return -1.
  222. if (time1.second < time2.second)
  223. return -1;
  224. // 7. If time1.[[Millisecond]] > time2.[[Millisecond]], return 1.
  225. if (time1.millisecond > time2.millisecond)
  226. return 1;
  227. // 8. If time1.[[Millisecond]] < time2.[[Millisecond]], return -1.
  228. if (time1.millisecond < time2.millisecond)
  229. return -1;
  230. // 9. If time1.[[Microsecond]] > time2.[[Microsecond]], return 1.
  231. if (time1.microsecond > time2.microsecond)
  232. return 1;
  233. // 10. If time1.[[Microsecond]] < time2.[[Microsecond]], return -1.
  234. if (time1.microsecond < time2.microsecond)
  235. return -1;
  236. // 11. If time1.[[Nanosecond]] > time2.[[Nanosecond]], return 1.
  237. if (time1.nanosecond > time2.nanosecond)
  238. return 1;
  239. // 12. If time1.[[Nanosecond]] < time2.[[Nanosecond]], return -1.
  240. if (time1.nanosecond < time2.nanosecond)
  241. return -1;
  242. // 13. Return 0.
  243. return 0;
  244. }
  245. // 4.5.15 AddTime ( time, timeDuration ), https://tc39.es/proposal-temporal/#sec-temporal-addtime
  246. Time add_time(Time const& time, TimeDuration const& time_duration)
  247. {
  248. auto nanoseconds = time_duration.plus(TimeDuration { static_cast<i64>(time.nanosecond) });
  249. // 1. Return BalanceTime(time.[[Hour]], time.[[Minute]], time.[[Second]], time.[[Millisecond]], time.[[Microsecond]], time.[[Nanosecond]] + timeDuration).
  250. return balance_time(time.hour, time.minute, time.second, time.millisecond, time.microsecond, nanoseconds);
  251. }
  252. }