Date.h 3.2 KB

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