Date.h 3.1 KB

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