Now.cpp 8.2 KB

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