DateTimeFormat.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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<HourCycleRegion> __attribute__((weak)) hour_cycle_region_from_string(StringView) { return {}; }
  88. Vector<HourCycle> __attribute__((weak)) get_regional_hour_cycles(StringView) { return {}; }
  89. // https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
  90. Vector<Unicode::HourCycle> get_locale_hour_cycles(StringView locale)
  91. {
  92. if (auto hour_cycles = get_regional_hour_cycles(locale); !hour_cycles.is_empty())
  93. return hour_cycles;
  94. auto return_default_hour_cycles = [&]() { return get_regional_hour_cycles("001"sv); };
  95. auto language = parse_unicode_language_id(locale);
  96. if (!language.has_value())
  97. return return_default_hour_cycles();
  98. if (!language->region.has_value())
  99. language = add_likely_subtags(*language);
  100. if (!language.has_value() || !language->region.has_value())
  101. return return_default_hour_cycles();
  102. if (auto hour_cycles = get_regional_hour_cycles(*language->region); !hour_cycles.is_empty())
  103. return hour_cycles;
  104. return return_default_hour_cycles();
  105. }
  106. Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale)
  107. {
  108. if (auto hour_cycles = get_locale_hour_cycles(locale); !hour_cycles.is_empty())
  109. return hour_cycles.first();
  110. return {};
  111. }
  112. String combine_skeletons(StringView first, StringView second)
  113. {
  114. // https://unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems
  115. constexpr auto field_order = Array {
  116. "G"sv, // Era
  117. "yYuUr"sv, // Year
  118. "ML"sv, // Month
  119. "dDFg"sv, // Day
  120. "Eec"sv, // Weekday
  121. "abB"sv, // Period
  122. "hHKk"sv, // Hour
  123. "m"sv, // Minute
  124. "sSA"sv, // Second
  125. "zZOvVXx"sv, // Zone
  126. };
  127. StringBuilder builder;
  128. auto append_from_skeleton = [&](auto skeleton, auto ch) {
  129. auto first_index = skeleton.find(ch);
  130. if (!first_index.has_value())
  131. return false;
  132. auto last_index = skeleton.find_last(ch);
  133. builder.append(skeleton.substring_view(*first_index, *last_index - *first_index + 1));
  134. return true;
  135. };
  136. for (auto fields : field_order) {
  137. for (auto ch : fields) {
  138. if (append_from_skeleton(first, ch))
  139. break;
  140. if (append_from_skeleton(second, ch))
  141. break;
  142. }
  143. }
  144. return builder.build();
  145. }
  146. Optional<CalendarFormat> __attribute__((weak)) get_calendar_date_format(StringView, StringView) { return {}; }
  147. Optional<CalendarFormat> __attribute__((weak)) get_calendar_time_format(StringView, StringView) { return {}; }
  148. Optional<CalendarFormat> __attribute__((weak)) get_calendar_date_time_format(StringView, StringView) { return {}; }
  149. Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type)
  150. {
  151. switch (type) {
  152. case CalendarFormatType::Date:
  153. return get_calendar_date_format(locale, calendar);
  154. case CalendarFormatType::Time:
  155. return get_calendar_time_format(locale, calendar);
  156. case CalendarFormatType::DateTime:
  157. return get_calendar_date_time_format(locale, calendar);
  158. default:
  159. VERIFY_NOT_REACHED();
  160. }
  161. }
  162. Vector<CalendarPattern> __attribute__((weak)) get_calendar_available_formats(StringView, StringView) { return {}; }
  163. Optional<CalendarRangePattern> __attribute__((weak)) get_calendar_default_range_format(StringView, StringView) { return {}; }
  164. Vector<CalendarRangePattern> __attribute__((weak)) get_calendar_range_formats(StringView, StringView, StringView) { return {}; }
  165. Vector<CalendarRangePattern> __attribute__((weak)) get_calendar_range12_formats(StringView, StringView, StringView) { return {}; }
  166. Optional<StringView> __attribute__((weak)) get_calendar_era_symbol(StringView, StringView, CalendarPatternStyle, Era) { return {}; }
  167. Optional<StringView> __attribute__((weak)) get_calendar_month_symbol(StringView, StringView, CalendarPatternStyle, Month) { return {}; }
  168. Optional<StringView> __attribute__((weak)) get_calendar_weekday_symbol(StringView, StringView, CalendarPatternStyle, Weekday) { return {}; }
  169. Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol(StringView, StringView, CalendarPatternStyle, DayPeriod) { return {}; }
  170. Optional<StringView> __attribute__((weak)) get_calendar_day_period_symbol_for_hour(StringView, StringView, CalendarPatternStyle, u8) { return {}; }
  171. Optional<StringView> __attribute__((weak)) get_time_zone_name(StringView, StringView, CalendarPatternStyle, TimeZone::InDST) { return {}; }
  172. Optional<TimeZoneFormat> __attribute__((weak)) get_time_zone_format(StringView) { return {}; }
  173. static Optional<String> format_time_zone_offset(StringView locale, CalendarPatternStyle style, i64 offset_seconds)
  174. {
  175. auto formats = get_time_zone_format(locale);
  176. if (!formats.has_value())
  177. return {};
  178. auto number_system = get_default_number_system(locale);
  179. if (!number_system.has_value())
  180. return {};
  181. if (offset_seconds == 0)
  182. return formats->gmt_zero_format;
  183. auto sign = offset_seconds > 0 ? formats->symbol_ahead_sign : formats->symbol_behind_sign;
  184. auto separator = offset_seconds > 0 ? formats->symbol_ahead_separator : formats->symbol_behind_separator;
  185. offset_seconds = llabs(offset_seconds);
  186. auto offset_hours = offset_seconds / 3'600;
  187. offset_seconds %= 3'600;
  188. auto offset_minutes = offset_seconds / 60;
  189. offset_seconds %= 60;
  190. StringBuilder builder;
  191. builder.append(sign);
  192. switch (style) {
  193. // The long format always uses 2-digit hours field and minutes field, with optional 2-digit seconds field.
  194. case CalendarPatternStyle::LongOffset:
  195. builder.appendff("{:02}{}{:02}", offset_hours, separator, offset_minutes);
  196. if (offset_seconds > 0)
  197. builder.appendff("{}{:02}", separator, offset_seconds);
  198. break;
  199. // The short format is intended for the shortest representation and uses hour fields without leading zero, with optional 2-digit minutes and seconds fields.
  200. case CalendarPatternStyle::ShortOffset:
  201. builder.appendff("{}", offset_hours);
  202. if (offset_minutes > 0) {
  203. builder.appendff("{}{:02}", separator, offset_minutes);
  204. if (offset_seconds > 0)
  205. builder.appendff("{}{:02}", separator, offset_seconds);
  206. }
  207. break;
  208. default:
  209. VERIFY_NOT_REACHED();
  210. }
  211. // The digits used for hours, minutes and seconds fields in this format are the locale's default decimal digits.
  212. auto result = replace_digits_for_number_system(*number_system, builder.build());
  213. return formats->gmt_format.replace("{0}"sv, result, ReplaceMode::FirstOnly);
  214. }
  215. // https://unicode.org/reports/tr35/tr35-dates.html#Time_Zone_Format_Terminology
  216. String format_time_zone(StringView locale, StringView time_zone, CalendarPatternStyle style, AK::Time time)
  217. {
  218. auto offset = TimeZone::get_time_zone_offset(time_zone, time);
  219. if (!offset.has_value())
  220. return time_zone;
  221. switch (style) {
  222. case CalendarPatternStyle::Short:
  223. case CalendarPatternStyle::Long:
  224. case CalendarPatternStyle::ShortGeneric:
  225. case CalendarPatternStyle::LongGeneric:
  226. if (auto name = get_time_zone_name(locale, time_zone, style, offset->in_dst); name.has_value())
  227. return *name;
  228. break;
  229. case CalendarPatternStyle::ShortOffset:
  230. case CalendarPatternStyle::LongOffset:
  231. return format_time_zone_offset(locale, style, offset->seconds).value_or(time_zone);
  232. default:
  233. VERIFY_NOT_REACHED();
  234. }
  235. // If more styles are added, consult the following table to ensure always falling back to GMT offset is still correct:
  236. // https://unicode.org/reports/tr35/tr35-dates.html#dfst-zone
  237. switch (style) {
  238. case CalendarPatternStyle::Short:
  239. case CalendarPatternStyle::ShortGeneric:
  240. return format_time_zone(locale, time_zone, CalendarPatternStyle::ShortOffset, time);
  241. case CalendarPatternStyle::Long:
  242. case CalendarPatternStyle::LongGeneric:
  243. return format_time_zone(locale, time_zone, CalendarPatternStyle::LongOffset, time);
  244. default:
  245. VERIFY_NOT_REACHED();
  246. }
  247. }
  248. }