Now.cpp 7.3 KB

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