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