summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormat.h
blob: 7e9ac0171069a085e9fe28a73f42879a661a3d58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Array.h>
#include <AK/DeprecatedString.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Intl/AbstractOperations.h>
#include <LibJS/Runtime/Object.h>
#include <LibLocale/DateTimeFormat.h>

namespace JS::Intl {

class DateTimeFormat final
    : public Object
    , public ::Locale::CalendarPattern {
    JS_OBJECT(DateTimeFormat, Object);

    using Patterns = ::Locale::CalendarPattern;

public:
    enum class Style {
        Full,
        Long,
        Medium,
        Short,
    };

    static constexpr auto relevant_extension_keys()
    {
        // 11.2.3 Internal slots, https://tc39.es/ecma402/#sec-intl.datetimeformat-internal-slots
        // The value of the [[RelevantExtensionKeys]] internal slot is « "ca", "hc", "nu" ».
        return AK::Array { "ca"sv, "hc"sv, "nu"sv };
    }

    virtual ~DateTimeFormat() override = default;

    DeprecatedString const& locale() const { return m_locale; }
    void set_locale(DeprecatedString locale) { m_locale = move(locale); }

    DeprecatedString const& data_locale() const { return m_data_locale; }
    void set_data_locale(DeprecatedString data_locale) { m_data_locale = move(data_locale); }

    DeprecatedString const& calendar() const { return m_calendar; }
    void set_calendar(DeprecatedString calendar) { m_calendar = move(calendar); }

    DeprecatedString const& numbering_system() const { return m_numbering_system; }
    void set_numbering_system(DeprecatedString numbering_system) { m_numbering_system = move(numbering_system); }

    bool has_hour_cycle() const { return m_hour_cycle.has_value(); }
    ::Locale::HourCycle hour_cycle() const { return *m_hour_cycle; }
    StringView hour_cycle_string() const { return ::Locale::hour_cycle_to_string(*m_hour_cycle); }
    void set_hour_cycle(::Locale::HourCycle hour_cycle) { m_hour_cycle = hour_cycle; }
    void clear_hour_cycle() { m_hour_cycle.clear(); }

    DeprecatedString const& time_zone() const { return m_time_zone; }
    void set_time_zone(DeprecatedString time_zone) { m_time_zone = move(time_zone); }

    bool has_date_style() const { return m_date_style.has_value(); }
    Style date_style() const { return *m_date_style; };
    StringView date_style_string() const { return style_to_string(*m_date_style); };
    void set_date_style(StringView style) { m_date_style = style_from_string(style); };

    bool has_time_style() const { return m_time_style.has_value(); }
    Style time_style() const { return *m_time_style; };
    StringView time_style_string() const { return style_to_string(*m_time_style); };
    void set_time_style(StringView style) { m_time_style = style_from_string(style); };

    DeprecatedString const& pattern() const { return Patterns::pattern; };
    void set_pattern(DeprecatedString pattern) { Patterns::pattern = move(pattern); }

    Span<::Locale::CalendarRangePattern const> range_patterns() const { return m_range_patterns.span(); };
    void set_range_patterns(Vector<::Locale::CalendarRangePattern> range_patterns) { m_range_patterns = move(range_patterns); }

    bool has_era() const { return Patterns::era.has_value(); }
    ::Locale::CalendarPatternStyle era() const { return *Patterns::era; };
    StringView era_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::era); }

    bool has_year() const { return Patterns::year.has_value(); }
    ::Locale::CalendarPatternStyle year() const { return *Patterns::year; };
    StringView year_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::year); }

    bool has_month() const { return Patterns::month.has_value(); }
    ::Locale::CalendarPatternStyle month() const { return *Patterns::month; };
    StringView month_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::month); }

    bool has_weekday() const { return Patterns::weekday.has_value(); }
    ::Locale::CalendarPatternStyle weekday() const { return *Patterns::weekday; };
    StringView weekday_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::weekday); }

    bool has_day() const { return Patterns::day.has_value(); }
    ::Locale::CalendarPatternStyle day() const { return *Patterns::day; };
    StringView day_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::day); }

    bool has_day_period() const { return Patterns::day_period.has_value(); }
    ::Locale::CalendarPatternStyle day_period() const { return *Patterns::day_period; };
    StringView day_period_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::day_period); }

    bool has_hour() const { return Patterns::hour.has_value(); }
    ::Locale::CalendarPatternStyle hour() const { return *Patterns::hour; };
    StringView hour_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::hour); }

    bool has_minute() const { return Patterns::minute.has_value(); }
    ::Locale::CalendarPatternStyle minute() const { return *Patterns::minute; };
    StringView minute_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::minute); }

    bool has_second() const { return Patterns::second.has_value(); }
    ::Locale::CalendarPatternStyle second() const { return *Patterns::second; };
    StringView second_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::second); }

    bool has_fractional_second_digits() const { return Patterns::fractional_second_digits.has_value(); }
    u8 fractional_second_digits() const { return *Patterns::fractional_second_digits; };

    bool has_time_zone_name() const { return Patterns::time_zone_name.has_value(); }
    ::Locale::CalendarPatternStyle time_zone_name() const { return *Patterns::time_zone_name; };
    StringView time_zone_name_string() const { return ::Locale::calendar_pattern_style_to_string(*Patterns::time_zone_name); }

    NativeFunction* bound_format() const { return m_bound_format; }
    void set_bound_format(NativeFunction* bound_format) { m_bound_format = bound_format; }

private:
    DateTimeFormat(Object& prototype);

    static Style style_from_string(StringView style);
    static StringView style_to_string(Style style);

    virtual void visit_edges(Visitor&) override;

    DeprecatedString m_locale;                               // [[Locale]]
    DeprecatedString m_calendar;                             // [[Calendar]]
    DeprecatedString m_numbering_system;                     // [[NumberingSystem]]
    Optional<::Locale::HourCycle> m_hour_cycle;              // [[HourCycle]]
    DeprecatedString m_time_zone;                            // [[TimeZone]]
    Optional<Style> m_date_style;                            // [[DateStyle]]
    Optional<Style> m_time_style;                            // [[TimeStyle]]
    Vector<::Locale::CalendarRangePattern> m_range_patterns; // [[RangePatterns]]
    NativeFunction* m_bound_format { nullptr };              // [[BoundFormat]]

    DeprecatedString m_data_locale;
};

enum class OptionRequired {
    Any,
    Date,
    Time,
};

enum class OptionDefaults {
    All,
    Date,
    Time,
};

// Table 8: Record returned by ToLocalTime, https://tc39.es/ecma402/#table-datetimeformat-tolocaltime-record
// Note: [[InDST]] is not included here - it is handled by LibUnicode / LibTimeZone.
struct LocalTime {
    AK::Time time_since_epoch() const
    {
        return AK::Time::from_timestamp(year, month + 1, day + 1, hour, minute, second, millisecond);
    }

    int weekday { 0 };     // [[Weekday]]
    ::Locale::Era era {};  // [[Era]]
    i32 year { 0 };        // [[Year]]
    Value related_year {}; // [[RelatedYear]]
    Value year_name {};    // [[YearName]]
    u8 month { 0 };        // [[Month]]
    u8 day { 0 };          // [[Day]]
    u8 hour { 0 };         // [[Hour]]
    u8 minute { 0 };       // [[Minute]]
    u8 second { 0 };       // [[Second]]
    u16 millisecond { 0 }; // [[Millisecond]]
};

ThrowCompletionOr<Object*> to_date_time_options(VM&, Value options_value, OptionRequired, OptionDefaults);
Optional<::Locale::CalendarPattern> date_time_style_format(StringView data_locale, DateTimeFormat& date_time_format);
Optional<::Locale::CalendarPattern> basic_format_matcher(::Locale::CalendarPattern const& options, Vector<::Locale::CalendarPattern> formats);
Optional<::Locale::CalendarPattern> best_fit_format_matcher(::Locale::CalendarPattern const& options, Vector<::Locale::CalendarPattern> formats);
ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(VM&, DateTimeFormat&, Vector<PatternPartition> pattern_parts, double time, ::Locale::CalendarPattern const* range_format_options);
ThrowCompletionOr<Vector<PatternPartition>> partition_date_time_pattern(VM&, DateTimeFormat&, double time);
ThrowCompletionOr<DeprecatedString> format_date_time(VM&, DateTimeFormat&, double time);
ThrowCompletionOr<Array*> format_date_time_to_parts(VM&, DateTimeFormat&, double time);
ThrowCompletionOr<Vector<PatternPartitionWithSource>> partition_date_time_range_pattern(VM&, DateTimeFormat&, double start, double end);
ThrowCompletionOr<DeprecatedString> format_date_time_range(VM&, DateTimeFormat&, double start, double end);
ThrowCompletionOr<Array*> format_date_time_range_to_parts(VM&, DateTimeFormat&, double start, double end);
ThrowCompletionOr<LocalTime> to_local_time(VM&, Crypto::SignedBigInteger const& epoch_ns, StringView calendar, StringView time_zone);

template<typename Callback>
ThrowCompletionOr<void> for_each_calendar_field(VM& vm, ::Locale::CalendarPattern& pattern, Callback&& callback)
{
    constexpr auto narrow_short_long = AK::Array { "narrow"sv, "short"sv, "long"sv };
    constexpr auto two_digit_numeric = AK::Array { "2-digit"sv, "numeric"sv };
    constexpr auto two_digit_numeric_narrow_short_long = AK::Array { "2-digit"sv, "numeric"sv, "narrow"sv, "short"sv, "long"sv };
    constexpr auto time_zone = AK::Array { "short"sv, "long"sv, "shortOffset"sv, "longOffset"sv, "shortGeneric"sv, "longGeneric"sv };

    // Table 6: Components of date and time formats, https://tc39.es/ecma402/#table-datetimeformat-components
    TRY(callback(pattern.weekday, vm.names.weekday, narrow_short_long));
    TRY(callback(pattern.era, vm.names.era, narrow_short_long));
    TRY(callback(pattern.year, vm.names.year, two_digit_numeric));
    TRY(callback(pattern.month, vm.names.month, two_digit_numeric_narrow_short_long));
    TRY(callback(pattern.day, vm.names.day, two_digit_numeric));
    TRY(callback(pattern.day_period, vm.names.dayPeriod, narrow_short_long));
    TRY(callback(pattern.hour, vm.names.hour, two_digit_numeric));
    TRY(callback(pattern.minute, vm.names.minute, two_digit_numeric));
    TRY(callback(pattern.second, vm.names.second, two_digit_numeric));
    TRY(callback(pattern.fractional_second_digits, vm.names.fractionalSecondDigits, Empty {}));
    TRY(callback(pattern.time_zone_name, vm.names.timeZoneName, time_zone));

    return {};
}

}