DateTimeFormat.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <AK/StringBuilder.h>
  8. #include <LibUnicode/DateTimeFormat.h>
  9. #include <LibUnicode/Locale.h>
  10. #include <LibUnicode/NumberFormat.h>
  11. #include <stdlib.h>
  12. namespace Unicode {
  13. HourCycle hour_cycle_from_string(StringView hour_cycle)
  14. {
  15. if (hour_cycle == "h11"sv)
  16. return Unicode::HourCycle::H11;
  17. else if (hour_cycle == "h12"sv)
  18. return Unicode::HourCycle::H12;
  19. else if (hour_cycle == "h23"sv)
  20. return Unicode::HourCycle::H23;
  21. else if (hour_cycle == "h24"sv)
  22. return Unicode::HourCycle::H24;
  23. VERIFY_NOT_REACHED();
  24. }
  25. StringView hour_cycle_to_string(HourCycle hour_cycle)
  26. {
  27. switch (hour_cycle) {
  28. case HourCycle::H11:
  29. return "h11"sv;
  30. case HourCycle::H12:
  31. return "h12"sv;
  32. case HourCycle::H23:
  33. return "h23"sv;
  34. case HourCycle::H24:
  35. return "h24"sv;
  36. default:
  37. VERIFY_NOT_REACHED();
  38. }
  39. }
  40. CalendarPatternStyle calendar_pattern_style_from_string(StringView style)
  41. {
  42. if (style == "narrow"sv)
  43. return CalendarPatternStyle::Narrow;
  44. if (style == "short"sv)
  45. return CalendarPatternStyle::Short;
  46. if (style == "long"sv)
  47. return CalendarPatternStyle::Long;
  48. if (style == "numeric"sv)
  49. return CalendarPatternStyle::Numeric;
  50. if (style == "2-digit"sv)
  51. return CalendarPatternStyle::TwoDigit;
  52. if (style == "shortOffset"sv)
  53. return CalendarPatternStyle::ShortOffset;
  54. if (style == "longOffset"sv)
  55. return CalendarPatternStyle::LongOffset;
  56. if (style == "shortGeneric"sv)
  57. return CalendarPatternStyle::ShortGeneric;
  58. if (style == "longGeneric"sv)
  59. return CalendarPatternStyle::LongGeneric;
  60. VERIFY_NOT_REACHED();
  61. }
  62. StringView calendar_pattern_style_to_string(CalendarPatternStyle style)
  63. {
  64. switch (style) {
  65. case CalendarPatternStyle::Narrow:
  66. return "narrow"sv;
  67. case CalendarPatternStyle::Short:
  68. return "short"sv;
  69. case CalendarPatternStyle::Long:
  70. return "long"sv;
  71. case CalendarPatternStyle::Numeric:
  72. return "numeric"sv;
  73. case CalendarPatternStyle::TwoDigit:
  74. return "2-digit"sv;
  75. case CalendarPatternStyle::ShortOffset:
  76. return "shortOffset"sv;
  77. case CalendarPatternStyle::LongOffset:
  78. return "longOffset"sv;
  79. case CalendarPatternStyle::ShortGeneric:
  80. return "shortGeneric"sv;
  81. case CalendarPatternStyle::LongGeneric:
  82. return "longGeneric"sv;
  83. default:
  84. VERIFY_NOT_REACHED();
  85. }
  86. }
  87. Optional<Calendar> __attribute__((weak)) calendar_from_string(StringView) { return {}; }
  88. Optional<HourCycleRegion> __attribute__((weak)) hour_cycle_region_from_string(StringView) { return {}; }
  89. Span<StringView const> __attribute__((weak)) get_available_calendars() { return {}; }
  90. Vector<HourCycle> __attribute__((weak)) get_regional_hour_cycles(StringView) { return {}; }
  91. // https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  92. Vector<Unicode::HourCycle> get_locale_hour_cycles(StringView locale)
  93. {
  94. if (auto hour_cycles = get_regional_hour_cycles(locale); !hour_cycles.is_empty())
  95. return hour_cycles;
  96. auto return_default_hour_cycles = [&]() { return get_regional_hour_cycles("001"sv); };
  97. auto language = parse_unicode_language_id(locale);
  98. if (!language.has_value())
  99. return return_default_hour_cycles();
  100. if (!language->region.has_value())
  101. language = add_likely_subtags(*language);
  102. if (!language.has_value() || !language->region.has_value())
  103. return return_default_hour_cycles();
  104. if (auto hour_cycles = get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
  105. return hour_cycles;
  106. return return_default_hour_cycles();
  107. }
  108. Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale)
  109. {
  110. if (auto hour_cycles = get_locale_hour_cycles(locale); !hour_cycles.is_empty())
  111. return hour_cycles.first();
  112. return {};
  113. }
  114. String combine_skeletons(StringView first, StringView second)
  115. {
  116. // https://unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems
  117. constexpr auto field_order = Array {
  118. "G"sv, // Era
  119. "yYuUr"sv, // Year
  120. "ML"sv, // Month
  121. "dDFg"sv, // Day
  122. "Eec"sv, // Weekday
  123. "abB"sv, // Period
  124. "hHKk"sv, // Hour
  125. "m"sv, // Minute
  126. "sSA"sv, // Second
  127. "zZOvVXx"sv, // Zone
  128. };
  129. StringBuilder builder;
  130. auto append_from_skeleton = [&](auto skeleton, auto ch) {
  131. auto first_index = skeleton.find(ch);
  132. if (!first_index.has_value())
  133. return false;
  134. auto last_index = skeleton.find_last(ch);
  135. builder.append(skeleton.substring_view(*first_index, *last_index - *first_index + 1));
  136. return true;
  137. };
  138. for (auto fields : field_order) {
  139. for (auto ch : fields) {
  140. if (append_from_skeleton(first, ch))
  141. break;
  142. if (append_from_skeleton(second, ch))
  143. break;
  144. }
  145. }
  146. return builder.build();
  147. }
  148. Optional<CalendarFormat> __attribute__((weak)) get_calendar_date_format(StringView, StringView) { return {}; }
  149. Optional<CalendarFormat> __attribute__((weak)) get_calendar_time_format(StringView, StringView) { return {}; }
  150. Optional<CalendarFormat> __attribute__((weak)) get_calendar_date_time_format(StringView, StringView) { return {}; }
  151. Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type)
  152. {
  153. switch (type) {
  154. case CalendarFormatType::Date:
  155. return get_calendar_date_format(locale, calendar);
  156. case CalendarFormatType::Time:
  157. return get_calendar_time_format(locale, calendar);
  158. case CalendarFormatType::DateTime:
  159. return get_calendar_date_time_format(locale, calendar);
  160. default:
  161. VERIFY_NOT_REACHED();
  162. }
  163. }
  164. Vector<CalendarPattern> __attribute__((weak)) get_calendar_available_formats(StringView, StringView) { return {}; }
  165. Optional<CalendarRangePattern> __attribute__((weak)) get_calendar_default_range_format(StringView, StringView) { return {}; }
  166. Vector<CalendarRangePattern> __attribute__((weak)) get_calendar_range_formats(StringView, StringView, StringView) { return {}; }
  167. Vector<CalendarRangePattern> __attribute__((weak)) get_calendar_range12_formats(StringView, StringView, StringView) { return {}; }
  168. Optional<StringView> __attribute__((weak)) get_calendar_era_symbol(StringView, StringView, CalendarPatternStyle, Era) { return {}; }
  169. Optional<StringView> __attribute__((weak)) get_calendar_month_symbol(StringView, StringView, CalendarPatternStyle, Month) { return {}; }
  170. Optional<StringView> __attribute__((weak)) get_calendar_weekday_symbol(StringView, StringView, CalendarPatternStyle, Weekday) { return {}; }
  171. Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol(StringView, StringView, CalendarPatternStyle, DayPeriod) { return {}; }
  172. Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol_for_hour(StringView, StringView, CalendarPatternStyle, u8) { return {}; }
  173. Optional<StringView> __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle, TimeZone::InDST) { return {}; }
  174. Optional<TimeZoneFormat> __attribute__((weak)) get_time_zone_format(StringView) { return {}; }
  175. static Optional<String> format_time_zone_offset(StringView locale, CalendarPatternStyle style, i64 offset_seconds)
  176. {
  177. auto formats = get_time_zone_format(locale);
  178. if (!formats.has_value())
  179. return {};
  180. auto number_system = get_default_number_system(locale);
  181. if (!number_system.has_value())
  182. return {};
  183. if (offset_seconds == 0)
  184. return formats->gmt_zero_format;
  185. auto sign = offset_seconds > 0 ? formats->symbol_ahead_sign : formats->symbol_behind_sign;
  186. auto separator = offset_seconds > 0 ? formats->symbol_ahead_separator : formats->symbol_behind_separator;
  187. offset_seconds = llabs(offset_seconds);
  188. auto offset_hours = offset_seconds / 3'600;
  189. offset_seconds %= 3'600;
  190. auto offset_minutes = offset_seconds / 60;
  191. offset_seconds %= 60;
  192. StringBuilder builder;
  193. builder.append(sign);
  194. switch (style) {
  195. // The long format always uses 2-digit hours field and minutes field, with optional 2-digit seconds field.
  196. case CalendarPatternStyle::LongOffset:
  197. builder.appendff("{:02}{}{:02}", offset_hours, separator, offset_minutes);
  198. if (offset_seconds > 0)
  199. builder.appendff("{}{:02}", separator, offset_seconds);
  200. break;
  201. // The short format is intended for the shortest representation and uses hour fields without leading zero, with optional 2-digit minutes and seconds fields.
  202. case CalendarPatternStyle::ShortOffset:
  203. builder.appendff("{}", offset_hours);
  204. if (offset_minutes > 0) {
  205. builder.appendff("{}{:02}", separator, offset_minutes);
  206. if (offset_seconds > 0)
  207. builder.appendff("{}{:02}", separator, offset_seconds);
  208. }
  209. break;
  210. default:
  211. VERIFY_NOT_REACHED();
  212. }
  213. // The digits used for hours, minutes and seconds fields in this format are the locale's default decimal digits.
  214. auto result = replace_digits_for_number_system(*number_system, builder.build());
  215. return formats->gmt_format.replace("{0}"sv, result);
  216. }
  217. // https://unicode.org/reports/tr35/tr35-dates.html#Time_Zone_Format_Terminology
  218. String format_time_zone(StringView locale, StringView time_zone, CalendarPatternStyle style, AK::Time time)
  219. {
  220. auto offset = TimeZone::get_time_zone_offset(time_zone, time);
  221. if (!offset.has_value())
  222. return time_zone;
  223. switch (style) {
  224. case CalendarPatternStyle::Short:
  225. case CalendarPatternStyle::Long:
  226. case CalendarPatternStyle::ShortGeneric:
  227. case CalendarPatternStyle::LongGeneric:
  228. if (auto name = get_time_zone_name(locale, time_zone, style, offset->in_dst); name.has_value())
  229. return *name;
  230. break;
  231. case CalendarPatternStyle::ShortOffset:
  232. case CalendarPatternStyle::LongOffset:
  233. return format_time_zone_offset(locale, style, offset->seconds).value_or(time_zone);
  234. default:
  235. VERIFY_NOT_REACHED();
  236. }
  237. // If more styles are added, consult the following table to ensure always falling back to GMT offset is still correct:
  238. // https://unicode.org/reports/tr35/tr35-dates.html#dfst-zone
  239. switch (style) {
  240. case CalendarPatternStyle::Short:
  241. case CalendarPatternStyle::ShortGeneric:
  242. return format_time_zone(locale, time_zone, CalendarPatternStyle::ShortOffset, time);
  243. case CalendarPatternStyle::Long:
  244. case CalendarPatternStyle::LongGeneric:
  245. return format_time_zone(locale, time_zone, CalendarPatternStyle::LongOffset, time);
  246. default:
  247. VERIFY_NOT_REACHED();
  248. }
  249. }
  250. }