Now.cpp 10 KB

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