Now.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. void Now::initialize(Realm& realm)
  26. {
  27. 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(), PrimitiveString::create(vm, "Temporal.Now"_string), 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. }
  42. // 2.2.1 Temporal.Now.timeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.timezone
  43. JS_DEFINE_NATIVE_FUNCTION(Now::time_zone)
  44. {
  45. // 1. Return ! SystemTimeZone().
  46. return system_time_zone(vm);
  47. }
  48. // 2.2.2 Temporal.Now.instant ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.instant
  49. JS_DEFINE_NATIVE_FUNCTION(Now::instant)
  50. {
  51. // 1. Return ! SystemInstant().
  52. return system_instant(vm);
  53. }
  54. // 2.2.3 Temporal.Now.plainDateTime ( calendarLike [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindatetime
  55. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_time)
  56. {
  57. auto calendar_like = vm.argument(0);
  58. auto temporal_time_zone_like = vm.argument(1);
  59. // 1. Return ? SystemDateTime(temporalTimeZoneLike, calendarLike).
  60. return TRY(system_date_time(vm, temporal_time_zone_like, calendar_like));
  61. }
  62. // 2.2.4 Temporal.Now.plainDateTimeISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindatetimeiso
  63. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date_time_iso)
  64. {
  65. auto temporal_time_zone_like = vm.argument(0);
  66. // 1, Let calendar be ! GetISO8601Calendar().
  67. auto* calendar = get_iso8601_calendar(vm);
  68. // 2. Return ? SystemDateTime(temporalTimeZoneLike, calendar).
  69. return TRY(system_date_time(vm, temporal_time_zone_like, calendar));
  70. }
  71. // 2.2.5 Temporal.Now.zonedDateTime ( calendarLike [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.zoneddatetime
  72. JS_DEFINE_NATIVE_FUNCTION(Now::zoned_date_time)
  73. {
  74. auto calendar_like = vm.argument(0);
  75. auto temporal_time_zone_like = vm.argument(1);
  76. // 1. Return ? SystemZonedDateTime(temporalTimeZoneLike, calendarLike).
  77. return TRY(system_zoned_date_time(vm, temporal_time_zone_like, calendar_like));
  78. }
  79. // 2.2.6 Temporal.Now.zonedDateTimeISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.zoneddatetimeiso
  80. JS_DEFINE_NATIVE_FUNCTION(Now::zoned_date_time_iso)
  81. {
  82. auto temporal_time_zone_like = vm.argument(0);
  83. // 1, Let calendar be ! GetISO8601Calendar().
  84. auto* calendar = get_iso8601_calendar(vm);
  85. // 2. Return ? SystemZonedDateTime(temporalTimeZoneLike, calendar).
  86. return TRY(system_zoned_date_time(vm, temporal_time_zone_like, calendar));
  87. }
  88. // 2.2.7 Temporal.Now.plainDate ( calendarLike [ , temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaindate
  89. JS_DEFINE_NATIVE_FUNCTION(Now::plain_date)
  90. {
  91. auto calendar_like = vm.argument(0);
  92. auto temporal_time_zone_like = vm.argument(1);
  93. // 1. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendarLike).
  94. auto* date_time = TRY(system_date_time(vm, temporal_time_zone_like, calendar_like));
  95. // 2. Return ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]).
  96. return MUST(create_temporal_date(vm, 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(vm);
  104. // 2. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar).
  105. auto* date_time = TRY(system_date_time(vm, temporal_time_zone_like, calendar));
  106. // 3. Return ! CreateTemporalDate(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[Calendar]]).
  107. return MUST(create_temporal_date(vm, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->calendar()));
  108. }
  109. // 2.2.9 Temporal.Now.plainTimeISO ( [ temporalTimeZoneLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.now.plaintimeiso
  110. JS_DEFINE_NATIVE_FUNCTION(Now::plain_time_iso)
  111. {
  112. auto temporal_time_zone_like = vm.argument(0);
  113. // 1. Let calendar be ! GetISO8601Calendar().
  114. auto* calendar = get_iso8601_calendar(vm);
  115. // 2. Let dateTime be ? SystemDateTime(temporalTimeZoneLike, calendar).
  116. auto* date_time = TRY(system_date_time(vm, temporal_time_zone_like, calendar));
  117. // 3. Return ! CreateTemporalTime(dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]]).
  118. 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()));
  119. }
  120. // 2.3.1 SystemTimeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemtimezone
  121. TimeZone* system_time_zone(VM& vm)
  122. {
  123. // 1. Let identifier be ! DefaultTimeZone().
  124. auto identifier = system_time_zone_identifier();
  125. // 2. Return ! CreateTemporalTimeZone(identifier).
  126. // FIXME: Propagate possible OOM error
  127. return MUST(create_temporal_time_zone(vm, identifier));
  128. }
  129. // 2.3.2 SystemUTCEpochNanoseconds ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemutcepochnanoseconds
  130. BigInt* system_utc_epoch_nanoseconds(VM& vm)
  131. {
  132. // 1. Let ns be the approximate current UTC date and time, in nanoseconds since the epoch.
  133. auto now = AK::UnixDateTime::now().nanoseconds_since_epoch();
  134. auto ns = Crypto::SignedBigInteger { now };
  135. // 2. Set ns to the result of clamping ns between nsMinInstant and nsMaxInstant.
  136. // NOTE: Duration::to_nanoseconds() already clamps between -(2^63) and 2^63 - 1, the range of an i64,
  137. // if an overflow occurs during seconds -> nanoseconds conversion.
  138. // 3. Return ℤ(ns).
  139. return BigInt::create(vm, move(ns));
  140. }
  141. // 2.3.3 SystemInstant ( ), https://tc39.es/proposal-temporal/#sec-temporal-systeminstant
  142. Instant* system_instant(VM& vm)
  143. {
  144. // 1. Let ns be ! SystemUTCEpochNanoseconds().
  145. auto* ns = system_utc_epoch_nanoseconds(vm);
  146. // 2. Return ! CreateTemporalInstant(ns).
  147. return MUST(create_temporal_instant(vm, *ns));
  148. }
  149. // 2.3.4 SystemDateTime ( temporalTimeZoneLike, calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-systemdatetime
  150. ThrowCompletionOr<PlainDateTime*> system_date_time(VM& vm, Value temporal_time_zone_like, Value calendar_like)
  151. {
  152. Object* time_zone;
  153. // 1. If temporalTimeZoneLike is undefined, then
  154. if (temporal_time_zone_like.is_undefined()) {
  155. // a. Let timeZone be ! SystemTimeZone().
  156. time_zone = system_time_zone(vm);
  157. }
  158. // 2. Else,
  159. else {
  160. // a. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike).
  161. time_zone = TRY(to_temporal_time_zone(vm, temporal_time_zone_like));
  162. }
  163. // 3. Let calendar be ? ToTemporalCalendar(calendarLike).
  164. auto* calendar = TRY(to_temporal_calendar(vm, calendar_like));
  165. // 4. Let instant be ! SystemInstant().
  166. auto* instant = system_instant(vm);
  167. // 5. Return ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar).
  168. return builtin_time_zone_get_plain_date_time_for(vm, time_zone, *instant, *calendar);
  169. }
  170. // 2.3.5 SystemZonedDateTime ( temporalTimeZoneLike, calendarLike ), https://tc39.es/proposal-temporal/#sec-temporal-systemzoneddatetime
  171. ThrowCompletionOr<ZonedDateTime*> system_zoned_date_time(VM& vm, Value temporal_time_zone_like, Value calendar_like)
  172. {
  173. Object* time_zone;
  174. // 1. If temporalTimeZoneLike is undefined, then
  175. if (temporal_time_zone_like.is_undefined()) {
  176. // a. Let timeZone be ! SystemTimeZone().
  177. time_zone = system_time_zone(vm);
  178. }
  179. // 2. Else,
  180. else {
  181. // a. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike).
  182. time_zone = TRY(to_temporal_time_zone(vm, temporal_time_zone_like));
  183. }
  184. // 3. Let calendar be ? ToTemporalCalendar(calendarLike).
  185. auto* calendar = TRY(to_temporal_calendar(vm, calendar_like));
  186. // 4. Let ns be ! SystemUTCEpochNanoseconds().
  187. auto* ns = system_utc_epoch_nanoseconds(vm);
  188. // 5. Return ? CreateTemporalZonedDateTime(ns, timeZone, calendar).
  189. return create_temporal_zoned_date_time(vm, *ns, *time_zone, *calendar);
  190. }
  191. }