Now.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCrypto/BigInt/SignedBigInteger.h>
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Temporal/Calendar.h>
  10. #include <LibJS/Runtime/Temporal/Instant.h>
  11. #include <LibJS/Runtime/Temporal/Now.h>
  12. #include <LibJS/Runtime/Temporal/PlainDate.h>
  13. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  14. #include <LibJS/Runtime/Temporal/PlainTime.h>
  15. #include <LibJS/Runtime/Temporal/TimeZone.h>
  16. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  17. #include <time.h>
  18. namespace JS::Temporal {
  19. // 2 The Temporal.Now Object, https://tc39.es/proposal-temporal/#sec-temporal-now-object
  20. Now::Now(GlobalObject& global_object)
  21. : Object(*global_object.object_prototype())
  22. {
  23. }
  24. void Now::initialize(GlobalObject& global_object)
  25. {
  26. Object::initialize(global_object);
  27. auto& vm = this->vm();
  28. // 2.1.1 Temporal.Now [ @@toStringTag ], https://tc39.es/proposal-temporal/#sec-temporal-now-@@tostringtag
  29. define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Temporal.Now"), Attribute::Configurable);
  30. u8 attr = Attribute::Writable | Attribute::Configurable;
  31. define_native_function(vm.names.timeZone, time_zone, 0, attr);
  32. define_native_function(vm.names.instant, instant, 0, attr);
  33. define_native_function(vm.names.plainDateTime, plain_date_time, 1, attr);
  34. define_native_function(vm.names.plainDateTimeISO, plain_date_time_iso, 0, attr);
  35. define_native_function(vm.names.zonedDateTime, zoned_date_time, 1, attr);
  36. define_native_function(vm.names.zonedDateTimeISO, zoned_date_time_iso, 0, attr);
  37. define_native_function(vm.names.plainDate, plain_date, 1, attr);
  38. define_native_function(vm.names.plainDateISO, plain_date_iso, 0, attr);
  39. define_native_function(vm.names.plainTimeISO, plain_time_iso, 0, attr);
  40. }
  41. // 2.2.1 Temporal.Now.timeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.timezone
  42. JS_DEFINE_NATIVE_FUNCTION(Now::time_zone)
  43. {
  44. // 1. Return ! SystemTimeZone().
  45. return system_time_zone(global_object);
  46. }
  47. // 2.2.2 Temporal.Now.instant ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.instant
  48. JS_DEFINE_NATIVE_FUNCTION(Now::instant)
  49. {
  50. // 1. Return ! SystemInstant().
  51. return system_instant(global_object);
  52. }
  53. // 2.2.3 Temporal.Now.plainDateTime ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindatetime
  54. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_time)
  55. {
  56. auto calendar = vm.argument(0);
  57. auto temporal_time_zone_like = vm.argument(1);
  58. // 1. Return ? SystemDateTime(temporalTimeZoneLike, calendar).
  59. return TRY(system_date_time(global_object, temporal_time_zone_like, calendar));
  60. }
  61. // 2.2.4 Temporal.Now.plainDateTimeISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindatetimeiso
  62. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_time_iso)
  63. {
  64. auto temporal_time_zone_like = vm.argument(0);
  65. // 1, Let calendar be ! GetISO8601Calendar().
  66. auto* calendar = get_iso8601_calendar(global_object);
  67. // 2. Return ? SystemDateTime(temporalTimeZoneLike, calendar).
  68. return TRY(system_date_time(global_object, temporal_time_zone_like, calendar));
  69. }
  70. // 2.2.5 Temporal.Now.zonedDateTime ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.zoneddatetime
  71. JS_DEFINE_NATIVE_FUNCTION(Now::zoned_date_time)
  72. {
  73. auto calendar = vm.argument(0);
  74. auto temporal_time_zone_like = vm.argument(1);
  75. // 1. Return ? SystemZonedDateTime(temporalTimeZoneLike, calendar).
  76. return TRY(system_zoned_date_time(global_object, temporal_time_zone_like, calendar));
  77. }
  78. // 2.2.6 Temporal.Now.zonedDateTimeISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.zoneddatetimeiso
  79. JS_DEFINE_NATIVE_FUNCTION(Now::zoned_date_time_iso)
  80. {
  81. auto temporal_time_zone_like = vm.argument(0);
  82. // 1, Let calendar be ! GetISO8601Calendar().
  83. auto* calendar = get_iso8601_calendar(global_object);
  84. // 2. Return ? SystemZonedDateTime(temporalTimeZoneLike, calendar).
  85. return TRY(system_zoned_date_time(global_object, temporal_time_zone_like, calendar));
  86. }
  87. // 2.2.7 Temporal.Now.plainDate ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindate
  88. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date)
  89. {
  90. auto calendar = vm.argument(0);
  91. auto temporal_time_zone_like = vm.argument(1);
  92. // 1. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar).
  93. auto* date_time = TRY(system_date_time(global_object, temporal_time_zone_like, calendar));
  94. // 2. Return ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]).
  95. return TRY(create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()));
  96. }
  97. // 2.2.8 Temporal.Now.plainDateISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindateiso
  98. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_iso)
  99. {
  100. auto temporal_time_zone_like = vm.argument(0);
  101. // 1. Let calendar be ! GetISO8601Calendar().
  102. auto* calendar = get_iso8601_calendar(global_object);
  103. // 2. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar).
  104. auto* date_time = TRY(system_date_time(global_object, temporal_time_zone_like, calendar));
  105. // 3. Return ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]).
  106. return TRY(create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()));
  107. }
  108. // 2.2.9 Temporal.Now.plainTimeISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaintimeiso
  109. JS_DEFINE_NATIVE_FUNCTION(Now::plain_time_iso)
  110. {
  111. auto temporal_time_zone_like = vm.argument(0);
  112. // 1. Let calendar be ! GetISO8601Calendar().
  113. auto* calendar = get_iso8601_calendar(global_object);
  114. // 2. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar).
  115. auto* date_time = TRY(system_date_time(global_object, temporal_time_zone_like, calendar));
  116. // 3. Return ! CreateTemporalTime(dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]]).
  117. return MUST(create_temporal_time(global_object, date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond()));
  118. }
  119. // 2.3.1 SystemTimeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemtimezone
  120. TimeZone* system_time_zone(GlobalObject& global_object)
  121. {
  122. // 1. Let identifier be ! DefaultTimeZone().
  123. auto identifier = default_time_zone();
  124. // 2. Return ! CreateTemporalTimeZone(identifier).
  125. return MUST(create_temporal_time_zone(global_object, identifier));
  126. }
  127. // 2.3.2 SystemUTCEpochNanoseconds ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemutcepochnanoseconds
  128. BigInt* system_utc_epoch_nanoseconds(GlobalObject& global_object)
  129. {
  130. // 1. Let ns be the approximate current UTC date and time, in nanoseconds since the epoch.
  131. struct timespec now;
  132. clock_gettime(CLOCK_REALTIME, &now);
  133. Checked<i64> ns_timestamp;
  134. ns_timestamp += now.tv_sec;
  135. ns_timestamp *= 1'000'000'000;
  136. ns_timestamp += now.tv_nsec;
  137. if (ns_timestamp.has_overflow()) {
  138. // TODO: Deal with this before 2262-04-21T00:47:16Z.
  139. VERIFY_NOT_REACHED();
  140. }
  141. auto ns = Crypto::SignedBigInteger::create_from(ns_timestamp.value());
  142. // 2. Set ns to the result of clamping ns between −8.64 × 10^21 and 8.64 × 10^21.
  143. // Uhh, these don't even fit in an i64... ¯\_(ツ)_/¯
  144. // 3. Return ℤ(ns).
  145. return js_bigint(global_object.heap(), move(ns));
  146. }
  147. // 2.3.3 SystemInstant ( ), https://tc39.es/proposal-temporal/#sec-temporal-systeminstant
  148. Instant* system_instant(GlobalObject& global_object)
  149. {
  150. // 1. Let ns be ! SystemUTCEpochNanoseconds().
  151. auto* ns = system_utc_epoch_nanoseconds(global_object);
  152. // 2. Return ! CreateTemporalInstant(ns).
  153. return MUST(create_temporal_instant(global_object, *ns));
  154. }
  155. // 2.3.4 SystemDateTime ( temporalTimeZoneLike, calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-systemdatetime
  156. ThrowCompletionOr<PlainDateTime*> system_date_time(GlobalObject& global_object, Value temporal_time_zone_like, Value calendar_like)
  157. {
  158. Object* time_zone;
  159. // 1. If temporalTimeZoneLike is undefined, then
  160. if (temporal_time_zone_like.is_undefined()) {
  161. // a. Let timeZone be ! SystemTimeZone().
  162. time_zone = system_time_zone(global_object);
  163. }
  164. // 2. Else,
  165. else {
  166. // a. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike).
  167. time_zone = TRY(to_temporal_time_zone(global_object, temporal_time_zone_like));
  168. }
  169. // 3. Let calendar be ? ToTemporalCalendar(calendarLike).
  170. auto* calendar = TRY(to_temporal_calendar(global_object, calendar_like));
  171. // 4. Let instant be ! SystemInstant().
  172. auto* instant = system_instant(global_object);
  173. // 5. Return ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar).
  174. return builtin_time_zone_get_plain_date_time_for(global_object, time_zone, *instant, *calendar);
  175. }
  176. // 2.3.5 SystemZonedDateTime ( temporalTimeZoneLike, calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-systemzoneddatetime
  177. ThrowCompletionOr<ZonedDateTime*> system_zoned_date_time(GlobalObject& global_object, Value temporal_time_zone_like, Value calendar_like)
  178. {
  179. Object* time_zone;
  180. // 1. If temporalTimeZoneLike is undefined, then
  181. if (temporal_time_zone_like.is_undefined()) {
  182. // a. Let timeZone be ! SystemTimeZone().
  183. time_zone = system_time_zone(global_object);
  184. }
  185. // 2. Else,
  186. else {
  187. // a. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike).
  188. time_zone = TRY(to_temporal_time_zone(global_object, temporal_time_zone_like));
  189. }
  190. // 3. Let calendar be ? ToTemporalCalendar(calendarLike).
  191. auto* calendar = TRY(to_temporal_calendar(global_object, calendar_like));
  192. // 4. Let ns be ! SystemUTCEpochNanoseconds().
  193. auto* ns = system_utc_epoch_nanoseconds(global_object);
  194. // 5. Return ? CreateTemporalZonedDateTime(ns, timeZone, calendar).
  195. return create_temporal_zoned_date_time(global_object, *ns, *time_zone, *calendar);
  196. }
  197. }