Date.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibCrypto/BigInt/SignedBigInteger.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibUnicode/TimeZone.h>
  11. namespace JS {
  12. class Date final : public Object {
  13. JS_OBJECT(Date, Object);
  14. GC_DECLARE_ALLOCATOR(Date);
  15. public:
  16. static GC::Ref<Date> create(Realm&, double date_value);
  17. // Out of line to ensure we have a key function
  18. virtual ~Date() override;
  19. double date_value() const { return m_date_value; }
  20. void set_date_value(double value) { m_date_value = value; }
  21. ErrorOr<String> iso_date_string() const;
  22. private:
  23. Date(double date_value, Object& prototype);
  24. double m_date_value { 0 }; // [[DateValue]]
  25. };
  26. // 21.4.1.22 Time Zone Identifier Record, https://tc39.es/ecma262/#sec-time-zone-identifier-record
  27. struct TimeZoneIdentifier {
  28. String identifier; // [[Identifier]]
  29. String primary_identifier; // [[PrimaryIdentifier]]
  30. };
  31. // https://tc39.es/ecma262/#eqn-HoursPerDay
  32. constexpr inline double hours_per_day = 24;
  33. // https://tc39.es/ecma262/#eqn-MinutesPerHour
  34. constexpr inline double minutes_per_hour = 60;
  35. // https://tc39.es/ecma262/#eqn-SecondsPerMinute
  36. constexpr inline double seconds_per_minute = 60;
  37. // https://tc39.es/ecma262/#eqn-msPerSecond
  38. constexpr inline double ms_per_second = 1'000;
  39. // https://tc39.es/ecma262/#eqn-msPerMinute
  40. constexpr inline double ms_per_minute = 60'000;
  41. // https://tc39.es/ecma262/#eqn-msPerHour
  42. constexpr inline double ms_per_hour = 3'600'000;
  43. // https://tc39.es/ecma262/#eqn-msPerDay
  44. constexpr inline double ms_per_day = 86'400'000;
  45. // https://tc39.es/proposal-temporal/#eqn-nsPerDay
  46. constexpr inline double ns_per_day = 86'400'000'000'000;
  47. extern Crypto::SignedBigInteger const ns_per_day_bigint;
  48. double day(double);
  49. double time_within_day(double);
  50. u16 days_in_year(i32);
  51. double day_from_year(i32);
  52. double time_from_year(i32);
  53. i32 year_from_time(double);
  54. u16 day_within_year(double);
  55. bool in_leap_year(double);
  56. u8 month_from_time(double);
  57. u8 date_from_time(double);
  58. u8 week_day(double);
  59. u8 hour_from_time(double);
  60. u8 min_from_time(double);
  61. u8 sec_from_time(double);
  62. u16 ms_from_time(double);
  63. Crypto::SignedBigInteger get_utc_epoch_nanoseconds(i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond);
  64. Vector<Crypto::SignedBigInteger> get_named_time_zone_epoch_nanoseconds(StringView time_zone_identifier, i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond);
  65. Unicode::TimeZoneOffset get_named_time_zone_offset_nanoseconds(StringView time_zone_identifier, Crypto::SignedBigInteger const& epoch_nanoseconds);
  66. Unicode::TimeZoneOffset get_named_time_zone_offset_milliseconds(StringView time_zone_identifier, double epoch_milliseconds);
  67. String system_time_zone_identifier();
  68. void clear_system_time_zone_cache();
  69. double local_time(double time);
  70. double utc_time(double time);
  71. double make_time(double hour, double min, double sec, double ms);
  72. double make_day(double year, double month, double date);
  73. double make_date(double day, double time);
  74. double time_clip(double time);
  75. bool is_time_zone_offset_string(StringView offset_string);
  76. double parse_time_zone_offset_string(StringView offset_string);
  77. }