|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include <AK/String.h> |
| 10 | +#include <AK/StringView.h> |
| 11 | +#include <AK/Types.h> |
| 12 | +#include <AK/Vector.h> |
| 13 | +#include <LibJS/Runtime/Object.h> |
| 14 | +#include <LibUnicode/DateTimeFormat.h> |
| 15 | + |
| 16 | +namespace JS::Intl { |
| 17 | + |
| 18 | +class DateTimeFormat final |
| 19 | + : public Object |
| 20 | + , public Unicode::CalendarPattern { |
| 21 | + JS_OBJECT(DateTimeFormat, Object); |
| 22 | + |
| 23 | + using Patterns = Unicode::CalendarPattern; |
| 24 | + |
| 25 | +public: |
| 26 | + enum class Style { |
| 27 | + Full, |
| 28 | + Long, |
| 29 | + Medium, |
| 30 | + Short, |
| 31 | + }; |
| 32 | + |
| 33 | + static Vector<StringView> const& relevant_extension_keys(); // [[RelevantExtensionKeys]] |
| 34 | + |
| 35 | + DateTimeFormat(Object& prototype); |
| 36 | + virtual ~DateTimeFormat() override = default; |
| 37 | + |
| 38 | + String const& locale() const { return m_locale; } |
| 39 | + void set_locale(String locale) { m_locale = move(locale); } |
| 40 | + |
| 41 | + String const& calendar() const { return m_calendar; } |
| 42 | + void set_calendar(String calendar) { m_calendar = move(calendar); } |
| 43 | + |
| 44 | + String const& numbering_system() const { return m_numbering_system; } |
| 45 | + void set_numbering_system(String numbering_system) { m_numbering_system = move(numbering_system); } |
| 46 | + |
| 47 | + bool has_hour_cycle() const { return m_hour_cycle.has_value(); } |
| 48 | + Unicode::HourCycle hour_cycle() const { return *m_hour_cycle; } |
| 49 | + StringView hour_cycle_string() const { return Unicode::hour_cycle_to_string(*m_hour_cycle); } |
| 50 | + void set_hour_cycle(StringView hour_cycle) { m_hour_cycle = Unicode::hour_cycle_from_string(hour_cycle); } |
| 51 | + void set_hour_cycle(Unicode::HourCycle hour_cycle) { m_hour_cycle = hour_cycle; } |
| 52 | + void clear_hour_cycle() { m_hour_cycle.clear(); } |
| 53 | + |
| 54 | + String const& time_zone() const { return m_time_zone; } |
| 55 | + void set_time_zone(String time_zone) { m_time_zone = move(time_zone); } |
| 56 | + |
| 57 | + bool has_date_style() const { return m_date_style.has_value(); } |
| 58 | + Style date_style() const { return *m_date_style; }; |
| 59 | + StringView date_style_string() const { return style_to_string(*m_date_style); }; |
| 60 | + void set_date_style(StringView style) { m_date_style = style_from_string(style); }; |
| 61 | + |
| 62 | + bool has_time_style() const { return m_time_style.has_value(); } |
| 63 | + Style time_style() const { return *m_time_style; }; |
| 64 | + StringView time_style_string() const { return style_to_string(*m_time_style); }; |
| 65 | + void set_time_style(StringView style) { m_time_style = style_from_string(style); }; |
| 66 | + |
| 67 | + String const& pattern() const { return Patterns::pattern; }; |
| 68 | + void set_pattern(String pattern) { Patterns::pattern = move(pattern); } |
| 69 | + |
| 70 | + bool has_era() const { return Patterns::era.has_value(); } |
| 71 | + Unicode::CalendarPatternStyle era() const { return *Patterns::era; }; |
| 72 | + StringView era_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::era); } |
| 73 | + |
| 74 | + bool has_year() const { return Patterns::year.has_value(); } |
| 75 | + Unicode::CalendarPatternStyle year() const { return *Patterns::year; }; |
| 76 | + StringView year_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::year); } |
| 77 | + |
| 78 | + bool has_month() const { return Patterns::month.has_value(); } |
| 79 | + Unicode::CalendarPatternStyle month() const { return *Patterns::month; }; |
| 80 | + StringView month_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::month); } |
| 81 | + |
| 82 | + bool has_weekday() const { return Patterns::weekday.has_value(); } |
| 83 | + Unicode::CalendarPatternStyle weekday() const { return *Patterns::weekday; }; |
| 84 | + StringView weekday_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::weekday); } |
| 85 | + |
| 86 | + bool has_day() const { return Patterns::day.has_value(); } |
| 87 | + Unicode::CalendarPatternStyle day() const { return *Patterns::day; }; |
| 88 | + StringView day_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::day); } |
| 89 | + |
| 90 | + bool has_day_period() const { return Patterns::day_period.has_value(); } |
| 91 | + Unicode::CalendarPatternStyle day_period() const { return *Patterns::day_period; }; |
| 92 | + StringView day_period_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::day_period); } |
| 93 | + |
| 94 | + bool has_hour() const { return Patterns::hour.has_value(); } |
| 95 | + Unicode::CalendarPatternStyle hour() const { return *Patterns::hour; }; |
| 96 | + StringView hour_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::hour); } |
| 97 | + |
| 98 | + bool has_minute() const { return Patterns::minute.has_value(); } |
| 99 | + Unicode::CalendarPatternStyle minute() const { return *Patterns::minute; }; |
| 100 | + StringView minute_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::minute); } |
| 101 | + |
| 102 | + bool has_second() const { return Patterns::second.has_value(); } |
| 103 | + Unicode::CalendarPatternStyle second() const { return *Patterns::second; }; |
| 104 | + StringView second_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::second); } |
| 105 | + |
| 106 | + bool has_fractional_second_digits() const { return Patterns::fractional_second_digits.has_value(); } |
| 107 | + u8 fractional_second_digits() const { return *Patterns::fractional_second_digits; }; |
| 108 | + |
| 109 | + bool has_time_zone_name() const { return Patterns::time_zone_name.has_value(); } |
| 110 | + Unicode::CalendarPatternStyle time_zone_name() const { return *Patterns::time_zone_name; }; |
| 111 | + StringView time_zone_name_string() const { return Unicode::calendar_pattern_style_to_string(*Patterns::time_zone_name); } |
| 112 | + |
| 113 | +private: |
| 114 | + static Style style_from_string(StringView style); |
| 115 | + static StringView style_to_string(Style style); |
| 116 | + |
| 117 | + String m_locale; // [[Locale]] |
| 118 | + String m_calendar; // [[Calendar]] |
| 119 | + String m_numbering_system; // [[NumberingSystem]] |
| 120 | + Optional<Unicode::HourCycle> m_hour_cycle; // [[HourCycle]] |
| 121 | + String m_time_zone; // [[TimeZone]] |
| 122 | + Optional<Style> m_date_style; // [[DateStyle]] |
| 123 | + Optional<Style> m_time_style; // [[TimeStyle]] |
| 124 | +}; |
| 125 | + |
| 126 | +} |
0 commit comments