Now.cpp 10 KB

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