2020-03-29 20:21:56 -03:00
|
|
|
/*
|
2022-05-06 14:10:34 -03:00
|
|
|
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
2024-11-20 22:06:42 -03:00
|
|
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@ladybird.org>
|
2020-03-29 20:21:56 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-29 20:21:56 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-05-28 15:40:53 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-01-28 13:26:03 -03:00
|
|
|
#include <LibCrypto/BigInt/SignedBigInteger.h>
|
2025-07-19 17:49:30 -03:00
|
|
|
#include <LibJS/Export.h>
|
2020-03-29 20:21:56 -03:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2024-06-25 16:27:20 -03:00
|
|
|
#include <LibUnicode/TimeZone.h>
|
2020-03-29 20:21:56 -03:00
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
2025-07-19 17:49:30 -03:00
|
|
|
class JS_API Date final : public Object {
|
2020-06-21 10:14:02 -03:00
|
|
|
JS_OBJECT(Date, Object);
|
2024-11-14 12:01:23 -03:00
|
|
|
GC_DECLARE_ALLOCATOR(Date);
|
2020-06-21 10:14:02 -03:00
|
|
|
|
2020-03-29 20:21:56 -03:00
|
|
|
public:
|
2024-11-14 12:01:23 -03:00
|
|
|
static GC::Ref<Date> create(Realm&, double date_value);
|
2020-04-17 14:07:59 -03:00
|
|
|
|
2024-01-09 20:01:28 -03:00
|
|
|
// Out of line to ensure we have a key function
|
|
|
|
|
virtual ~Date() override;
|
2020-03-29 20:21:56 -03:00
|
|
|
|
2022-01-14 19:42:42 -03:00
|
|
|
double date_value() const { return m_date_value; }
|
|
|
|
|
void set_date_value(double value) { m_date_value = value; }
|
|
|
|
|
|
2026-06-22 08:06:30 -03:00
|
|
|
ErrorOr<Utf16String> iso_date_string() const;
|
2020-08-20 17:29:27 -03:00
|
|
|
|
2020-03-29 20:21:56 -03:00
|
|
|
private:
|
2022-08-28 18:51:28 -03:00
|
|
|
Date(double date_value, Object& prototype);
|
|
|
|
|
|
2025-03-25 05:34:06 -03:00
|
|
|
virtual bool is_date() const final { return true; }
|
|
|
|
|
|
2022-01-14 19:42:42 -03:00
|
|
|
double m_date_value { 0 }; // [[DateValue]]
|
2020-03-29 20:21:56 -03:00
|
|
|
};
|
|
|
|
|
|
2025-03-25 05:34:06 -03:00
|
|
|
template<>
|
|
|
|
|
inline bool Object::fast_is<Date>() const { return is_date(); }
|
|
|
|
|
|
2023-10-03 13:14:59 -03:00
|
|
|
// 21.4.1.22 Time Zone Identifier Record, https://tc39.es/ecma262/#sec-time-zone-identifier-record
|
|
|
|
|
struct TimeZoneIdentifier {
|
2024-06-25 12:06:08 -03:00
|
|
|
String identifier; // [[Identifier]]
|
|
|
|
|
String primary_identifier; // [[PrimaryIdentifier]]
|
2023-10-03 13:14:59 -03:00
|
|
|
};
|
|
|
|
|
|
2022-05-06 14:10:34 -03:00
|
|
|
// https://tc39.es/ecma262/#eqn-HoursPerDay
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double hours_per_day = 24;
|
2022-05-06 14:10:34 -03:00
|
|
|
// https://tc39.es/ecma262/#eqn-MinutesPerHour
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double minutes_per_hour = 60;
|
2022-05-06 14:10:34 -03:00
|
|
|
// https://tc39.es/ecma262/#eqn-SecondsPerMinute
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double seconds_per_minute = 60;
|
2022-05-06 14:10:34 -03:00
|
|
|
// https://tc39.es/ecma262/#eqn-msPerSecond
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double ms_per_second = 1'000;
|
2022-05-06 14:10:34 -03:00
|
|
|
// https://tc39.es/ecma262/#eqn-msPerMinute
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double ms_per_minute = 60'000;
|
2022-05-06 14:10:34 -03:00
|
|
|
// https://tc39.es/ecma262/#eqn-msPerHour
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double ms_per_hour = 3'600'000;
|
2022-05-06 14:10:34 -03:00
|
|
|
// https://tc39.es/ecma262/#eqn-msPerDay
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double ms_per_day = 86'400'000;
|
2022-05-06 14:28:56 -03:00
|
|
|
// https://tc39.es/proposal-temporal/#eqn-nsPerDay
|
2023-01-28 13:12:54 -03:00
|
|
|
constexpr inline double ns_per_day = 86'400'000'000'000;
|
|
|
|
|
extern Crypto::SignedBigInteger const ns_per_day_bigint;
|
2022-05-06 14:10:34 -03:00
|
|
|
|
2026-04-07 06:51:00 -03:00
|
|
|
// https://tc39.es/ecma262/#sec-time-values-and-time-range
|
|
|
|
|
// A time value supports a [...] range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds
|
|
|
|
|
constexpr inline double max_time_value = 8.64E15;
|
|
|
|
|
|
2025-07-19 14:41:08 -03:00
|
|
|
double day(double);
|
|
|
|
|
double time_within_day(double);
|
|
|
|
|
u16 days_in_year(i32);
|
|
|
|
|
double day_from_year(i32);
|
|
|
|
|
double time_from_year(i32);
|
2025-07-19 17:49:30 -03:00
|
|
|
JS_API i32 year_from_time(double);
|
2025-07-19 14:41:08 -03:00
|
|
|
u16 day_within_year(double);
|
|
|
|
|
bool in_leap_year(double);
|
2025-07-19 17:49:30 -03:00
|
|
|
JS_API u8 month_from_time(double);
|
|
|
|
|
JS_API u8 date_from_time(double);
|
2025-07-19 14:41:08 -03:00
|
|
|
u8 week_day(double);
|
2025-07-19 17:49:30 -03:00
|
|
|
JS_API u8 hour_from_time(double);
|
|
|
|
|
JS_API u8 min_from_time(double);
|
|
|
|
|
JS_API u8 sec_from_time(double);
|
|
|
|
|
JS_API u16 ms_from_time(double);
|
2025-07-19 14:41:08 -03:00
|
|
|
Crypto::SignedBigInteger get_utc_epoch_nanoseconds(Temporal::ISODateTime const&);
|
2026-01-14 11:07:20 -03:00
|
|
|
i64 clip_bigint_to_sane_time(Crypto::SignedBigInteger const& value);
|
|
|
|
|
i64 clip_double_to_sane_time(double value);
|
2025-07-19 14:41:08 -03:00
|
|
|
Vector<Crypto::SignedBigInteger> get_named_time_zone_epoch_nanoseconds(StringView time_zone_identifier, Temporal::ISODateTime const&);
|
|
|
|
|
Unicode::TimeZoneOffset get_named_time_zone_offset_nanoseconds(StringView time_zone_identifier, Crypto::SignedBigInteger const& epoch_nanoseconds);
|
|
|
|
|
Unicode::TimeZoneOffset get_named_time_zone_offset_milliseconds(StringView time_zone_identifier, double epoch_milliseconds);
|
|
|
|
|
String system_time_zone_identifier();
|
2025-07-19 17:49:30 -03:00
|
|
|
JS_API void clear_system_time_zone_cache();
|
2025-07-19 14:41:08 -03:00
|
|
|
double local_time(double time);
|
|
|
|
|
double utc_time(double time);
|
2025-07-19 17:49:30 -03:00
|
|
|
JS_API double make_time(double hour, double min, double sec, double ms);
|
|
|
|
|
JS_API double make_day(double year, double month, double date);
|
|
|
|
|
JS_API double make_date(double day, double time);
|
2025-07-19 14:41:08 -03:00
|
|
|
double time_clip(double time);
|
|
|
|
|
bool is_offset_time_zone_identifier(StringView offset_string);
|
|
|
|
|
ThrowCompletionOr<double> parse_date_time_utc_offset(VM&, StringView offset_string);
|
|
|
|
|
double parse_date_time_utc_offset(StringView offset_string);
|
|
|
|
|
double parse_date_time_utc_offset(Temporal::TimeZoneOffset const&);
|
2021-07-11 15:04:11 -03:00
|
|
|
|
2020-03-29 20:21:56 -03:00
|
|
|
}
|