Now.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.plainDate, plain_date, 1, attr);
  32. define_native_function(vm.names.plainDateISO, plain_date_iso, 0, attr);
  33. }
  34. // 2.1.1 Temporal.Now.timeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.timezone
  35. JS_DEFINE_NATIVE_FUNCTION(Now::time_zone)
  36. {
  37. // 1. Return ! SystemTimeZone().
  38. return system_time_zone(global_object);
  39. }
  40. // 2.1.2 Temporal.Now.instant ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.instant
  41. JS_DEFINE_NATIVE_FUNCTION(Now::instant)
  42. {
  43. // 1. Return ! SystemInstant().
  44. return system_instant(global_object);
  45. }
  46. // 2.1.3 Temporal.Now.plainDateTime ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindatetime
  47. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_time)
  48. {
  49. auto calendar = vm.argument(0);
  50. auto temporal_time_zone_like = vm.argument(1);
  51. // 1. Return ? SystemDateTime(temporalTimeZoneLike, calendar).
  52. return system_date_time(global_object, temporal_time_zone_like, calendar);
  53. }
  54. // 2.1.7 Temporal.Now.plainDate ( calendar [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindate
  55. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date)
  56. {
  57. auto calendar = vm.argument(0);
  58. auto temporal_time_zone_like = vm.argument(1);
  59. // 1. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar).
  60. auto* date_time = system_date_time(global_object, temporal_time_zone_like, calendar);
  61. if (vm.exception())
  62. return {};
  63. // 2. Return ? CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]).
  64. return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar());
  65. }
  66. // 2.1.8 Temporal.Now.plainDateISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindateiso
  67. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_iso)
  68. {
  69. auto temporal_time_zone_like = vm.argument(0);
  70. // 1. Let calendar be ? GetISO8601Calendar().
  71. auto* calendar = get_iso8601_calendar(global_object);
  72. if (vm.exception())
  73. return {};
  74. // 2. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar).
  75. auto* date_time = system_date_time(global_object, temporal_time_zone_like, calendar);
  76. if (vm.exception())
  77. return {};
  78. // 3. Return ? CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]).
  79. return create_temporal_date(global_object, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar());
  80. }
  81. // 2.2.1 SystemTimeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemtimezone
  82. TimeZone* system_time_zone(GlobalObject& global_object)
  83. {
  84. // 1. Let identifier be ! DefaultTimeZone().
  85. auto identifier = default_time_zone();
  86. // 2. Return ! CreateTemporalTimeZone(identifier).
  87. return create_temporal_time_zone(global_object, identifier);
  88. }
  89. // 2.2.2 SystemUTCEpochNanoseconds ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemutcepochnanoseconds
  90. BigInt* system_utc_epoch_nanoseconds(GlobalObject& global_object)
  91. {
  92. // 1. Let ns be the approximate current UTC date and time, in nanoseconds since the epoch.
  93. struct timespec now;
  94. clock_gettime(CLOCK_REALTIME, &now);
  95. Checked<i64> ns_timestamp;
  96. ns_timestamp += now.tv_sec;
  97. ns_timestamp *= 1'000'000'000;
  98. ns_timestamp += now.tv_nsec;
  99. if (ns_timestamp.has_overflow()) {
  100. // TODO: Deal with this before 2262-04-21T00:47:16Z.
  101. VERIFY_NOT_REACHED();
  102. }
  103. auto ns = Crypto::SignedBigInteger::create_from(ns_timestamp.value());
  104. // 2. Set ns to the result of clamping ns between −8.64 × 10^21 and 8.64 × 10^21.
  105. // Uhh, these don't even fit in an i64... ¯\_(ツ)_/¯
  106. // 3. Return ℤ(ns).
  107. return js_bigint(global_object.heap(), move(ns));
  108. }
  109. // 2.2.3 SystemInstant ( ), https://tc39.es/proposal-temporal/#sec-temporal-systeminstant
  110. Instant* system_instant(GlobalObject& global_object)
  111. {
  112. // 1. Let ns be ! SystemUTCEpochNanoseconds().
  113. auto* ns = system_utc_epoch_nanoseconds(global_object);
  114. // 2. Return ! CreateTemporalInstant(ns).
  115. return create_temporal_instant(global_object, *ns);
  116. }
  117. // 2.2.4 SystemDateTime ( temporalTimeZoneLike, calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-systemdatetime
  118. PlainDateTime* system_date_time(GlobalObject& global_object, Value temporal_time_zone_like, Value calendar_like)
  119. {
  120. auto& vm = global_object.vm();
  121. Object* time_zone;
  122. // 1. If temporalTimeZoneLike is undefined, then
  123. if (temporal_time_zone_like.is_undefined()) {
  124. // a. Let timeZone be ! SystemTimeZone().
  125. time_zone = system_time_zone(global_object);
  126. } else {
  127. // a. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike).
  128. time_zone = to_temporal_time_zone(global_object, temporal_time_zone_like);
  129. if (vm.exception())
  130. return {};
  131. }
  132. // 3. Let calendar be ? ToTemporalCalendar(calendarLike).
  133. auto* calendar = to_temporal_calendar(global_object, calendar_like);
  134. if (vm.exception())
  135. return {};
  136. // 4. Let instant be ! SystemInstant().
  137. auto* instant = system_instant(global_object);
  138. // 5. Return ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar).
  139. return builtin_time_zone_get_plain_date_time_for(global_object, *time_zone, *instant, *calendar);
  140. }
  141. }