Now.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/Instant.h>
  9. #include <LibJS/Runtime/Temporal/Now.h>
  10. #include <LibJS/Runtime/Temporal/TimeZone.h>
  11. #include <time.h>
  12. namespace JS::Temporal {
  13. // 2 The Temporal.now Object, https://tc39.es/proposal-temporal/#sec-temporal-now-object
  14. Now::Now(GlobalObject& global_object)
  15. : Object(*global_object.object_prototype())
  16. {
  17. }
  18. void Now::initialize(GlobalObject& global_object)
  19. {
  20. Object::initialize(global_object);
  21. auto& vm = this->vm();
  22. u8 attr = Attribute::Writable | Attribute::Configurable;
  23. define_native_function(vm.names.timeZone, time_zone, 0, attr);
  24. define_native_function(vm.names.instant, instant, 0, attr);
  25. }
  26. // 2.1.1 Temporal.now.timeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.timezone
  27. JS_DEFINE_NATIVE_FUNCTION(Now::time_zone)
  28. {
  29. // 1. Return ? SystemTimeZone().
  30. return system_time_zone(global_object);
  31. }
  32. // 2.1.2 Temporal.now.instant ( ), https://tc39.es/proposal-temporal/#sec-temporal.now.instant
  33. JS_DEFINE_NATIVE_FUNCTION(Now::instant)
  34. {
  35. // 1. Return ? SystemInstant().
  36. return system_instant(global_object);
  37. }
  38. // 2.2.1 SystemTimeZone ( ), https://tc39.es/proposal-temporal/#sec-temporal-systemtimezone
  39. Object* system_time_zone(GlobalObject& global_object)
  40. {
  41. // 1. Let identifier be ! DefaultTimeZone().
  42. auto identifier = default_time_zone();
  43. // 2. Return ? CreateTemporalTimeZone(identifier).
  44. return create_temporal_time_zone(global_object, identifier);
  45. }
  46. // 2.2.2 SystemUTCEpochNanoseconds ( )
  47. BigInt* system_utc_epoch_nanoseconds(GlobalObject& global_object)
  48. {
  49. // 1. Let ns be the approximate current UTC date and time, in nanoseconds since the epoch.
  50. struct timespec now;
  51. clock_gettime(CLOCK_REALTIME, &now);
  52. Checked<i64> ns_timestamp;
  53. ns_timestamp += now.tv_sec;
  54. ns_timestamp *= 1'000'000'000;
  55. ns_timestamp += now.tv_nsec;
  56. if (ns_timestamp.has_overflow()) {
  57. // TODO: Deal with this before 2262-04-21T00:47:16Z.
  58. VERIFY_NOT_REACHED();
  59. }
  60. auto ns = Crypto::SignedBigInteger::create_from(ns_timestamp.value());
  61. // 2. Set ns to the result of clamping ns between −8.64 × 10^21 and 8.64 × 10^21.
  62. // Uhh, these don't even fit in an i64... ¯\_(ツ)_/¯
  63. // 3. Return ℤ(ns).
  64. return js_bigint(global_object.heap(), move(ns));
  65. }
  66. // 2.2.3 SystemInstant ( )
  67. Object* system_instant(GlobalObject& global_object)
  68. {
  69. // 1. Let ns be ! SystemUTCEpochNanoseconds().
  70. auto* ns = system_utc_epoch_nanoseconds(global_object);
  71. // 2. Return ? CreateTemporalInstant(ns).
  72. return create_temporal_instant(global_object, *ns);
  73. }
  74. }