DateTimeFormat.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibUnicode/DateTimeFormat.h>
  7. #include <LibUnicode/Locale.h>
  8. #if ENABLE_UNICODE_DATA
  9. # include <LibUnicode/UnicodeDateTimeFormat.h>
  10. #endif
  11. namespace Unicode {
  12. HourCycle hour_cycle_from_string(StringView hour_cycle)
  13. {
  14. if (hour_cycle == "h11"sv)
  15. return Unicode::HourCycle::H11;
  16. else if (hour_cycle == "h12"sv)
  17. return Unicode::HourCycle::H12;
  18. else if (hour_cycle == "h23"sv)
  19. return Unicode::HourCycle::H23;
  20. else if (hour_cycle == "h24"sv)
  21. return Unicode::HourCycle::H24;
  22. VERIFY_NOT_REACHED();
  23. }
  24. StringView hour_cycle_to_string(HourCycle hour_cycle)
  25. {
  26. switch (hour_cycle) {
  27. case HourCycle::H11:
  28. return "h11"sv;
  29. case HourCycle::H12:
  30. return "h12"sv;
  31. case HourCycle::H23:
  32. return "h23"sv;
  33. case HourCycle::H24:
  34. return "h24"sv;
  35. default:
  36. VERIFY_NOT_REACHED();
  37. }
  38. }
  39. CalendarPatternStyle calendar_pattern_style_from_string(StringView style)
  40. {
  41. if (style == "narrow"sv)
  42. return CalendarPatternStyle::Narrow;
  43. if (style == "short"sv)
  44. return CalendarPatternStyle::Short;
  45. if (style == "long"sv)
  46. return CalendarPatternStyle::Long;
  47. if (style == "numeric"sv)
  48. return CalendarPatternStyle::Numeric;
  49. if (style == "2-digit"sv)
  50. return CalendarPatternStyle::TwoDigit;
  51. VERIFY_NOT_REACHED();
  52. }
  53. StringView calendar_pattern_style_to_string(CalendarPatternStyle style)
  54. {
  55. switch (style) {
  56. case CalendarPatternStyle::Narrow:
  57. return "narrow"sv;
  58. case CalendarPatternStyle::Short:
  59. return "short"sv;
  60. case CalendarPatternStyle::Long:
  61. return "long"sv;
  62. case CalendarPatternStyle::Numeric:
  63. return "numeric"sv;
  64. case CalendarPatternStyle::TwoDigit:
  65. return "2-digit"sv;
  66. default:
  67. VERIFY_NOT_REACHED();
  68. }
  69. }
  70. // https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  71. Vector<Unicode::HourCycle> get_regional_hour_cycles([[maybe_unused]] StringView locale)
  72. {
  73. #if ENABLE_UNICODE_DATA
  74. if (auto hour_cycles = Detail::get_regional_hour_cycles(locale); !hour_cycles.is_empty())
  75. return hour_cycles;
  76. auto return_default_hour_cycles = []() {
  77. auto hour_cycles = Detail::get_regional_hour_cycles("001"sv);
  78. VERIFY(!hour_cycles.is_empty());
  79. return hour_cycles;
  80. };
  81. auto language = parse_unicode_language_id(locale);
  82. if (!language.has_value())
  83. return return_default_hour_cycles();
  84. if (!language->region.has_value())
  85. language = add_likely_subtags(*language);
  86. if (!language.has_value() || !language->region.has_value())
  87. return return_default_hour_cycles();
  88. if (auto hour_cycles = Detail::get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
  89. return hour_cycles;
  90. return return_default_hour_cycles();
  91. #else
  92. return {};
  93. #endif
  94. }
  95. Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale)
  96. {
  97. if (auto hour_cycles = get_regional_hour_cycles(locale); !hour_cycles.is_empty())
  98. return hour_cycles.first();
  99. return {};
  100. }
  101. Optional<CalendarFormat> get_calendar_format([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarFormatType type)
  102. {
  103. #if ENABLE_UNICODE_DATA
  104. switch (type) {
  105. case CalendarFormatType::Date:
  106. return Detail::get_calendar_date_format(locale, calendar);
  107. case CalendarFormatType::Time:
  108. return Detail::get_calendar_time_format(locale, calendar);
  109. case CalendarFormatType::DateTime:
  110. return Detail::get_calendar_date_time_format(locale, calendar);
  111. default:
  112. VERIFY_NOT_REACHED();
  113. }
  114. #else
  115. return {};
  116. #endif
  117. }
  118. Vector<CalendarPattern> get_calendar_available_formats([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar)
  119. {
  120. #if ENABLE_UNICODE_DATA
  121. return Detail::get_calendar_available_formats(locale, calendar);
  122. #else
  123. return {};
  124. #endif
  125. }
  126. Optional<StringView> get_calendar_era_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Era value)
  127. {
  128. #if ENABLE_UNICODE_DATA
  129. return Detail::get_calendar_era_symbol(locale, calendar, style, value);
  130. #else
  131. return {};
  132. #endif
  133. }
  134. Optional<StringView> get_calendar_month_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Month value)
  135. {
  136. #if ENABLE_UNICODE_DATA
  137. return Detail::get_calendar_month_symbol(locale, calendar, style, value);
  138. #else
  139. return {};
  140. #endif
  141. }
  142. Optional<StringView> get_calendar_weekday_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::Weekday value)
  143. {
  144. #if ENABLE_UNICODE_DATA
  145. return Detail::get_calendar_weekday_symbol(locale, calendar, style, value);
  146. #else
  147. return {};
  148. #endif
  149. }
  150. Optional<StringView> get_calendar_day_period_symbol([[maybe_unused]] StringView locale, [[maybe_unused]] StringView calendar, [[maybe_unused]] CalendarPatternStyle style, [[maybe_unused]] Unicode::DayPeriod value)
  151. {
  152. #if ENABLE_UNICODE_DATA
  153. return Detail::get_calendar_day_period_symbol(locale, calendar, style, value);
  154. #else
  155. return {};
  156. #endif
  157. }
  158. Optional<StringView> get_time_zone_name([[maybe_unused]] StringView locale, [[maybe_unused]] StringView time_zone, [[maybe_unused]] CalendarPatternStyle style)
  159. {
  160. #if ENABLE_UNICODE_DATA
  161. return Detail::get_time_zone_name(locale, time_zone, style);
  162. #else
  163. return {};
  164. #endif
  165. }
  166. }