DateTimeFormat.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2021-2022, Tim Flynn <trflynn89@pm.me>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <AK/String.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Time.h>
  11. #include <AK/Types.h>
  12. #include <AK/Vector.h>
  13. #include <LibUnicode/Forward.h>
  14. namespace Unicode {
  15. enum class Era : u8 {
  16. BC,
  17. AD,
  18. };
  19. enum class Month : u8 {
  20. January,
  21. February,
  22. March,
  23. April,
  24. May,
  25. June,
  26. July,
  27. August,
  28. September,
  29. October,
  30. November,
  31. December,
  32. };
  33. enum class Weekday : u8 {
  34. Sunday,
  35. Monday,
  36. Tuesday,
  37. Wednesday,
  38. Thursday,
  39. Friday,
  40. Saturday,
  41. };
  42. enum class DayPeriod : u8 {
  43. AM,
  44. PM,
  45. Morning1,
  46. Morning2,
  47. Afternoon1,
  48. Afternoon2,
  49. Evening1,
  50. Evening2,
  51. Night1,
  52. Night2,
  53. };
  54. enum class HourCycle : u8 {
  55. H11,
  56. H12,
  57. H23,
  58. H24,
  59. };
  60. enum class CalendarPatternStyle : u8 {
  61. Narrow,
  62. Short,
  63. Long,
  64. Numeric,
  65. TwoDigit,
  66. ShortOffset,
  67. LongOffset,
  68. ShortGeneric,
  69. LongGeneric,
  70. };
  71. struct CalendarPattern {
  72. enum class Field {
  73. Era,
  74. Year,
  75. Month,
  76. Weekday,
  77. Day,
  78. DayPeriod,
  79. Hour,
  80. Minute,
  81. Second,
  82. FractionalSecondDigits,
  83. TimeZoneName,
  84. };
  85. template<typename Callback>
  86. void for_each_calendar_field_zipped_with(CalendarPattern const& other, Callback&& callback)
  87. {
  88. callback(era, other.era, Field::Era);
  89. callback(year, other.year, Field::Year);
  90. callback(month, other.month, Field::Month);
  91. callback(weekday, other.weekday, Field::Weekday);
  92. callback(day, other.day, Field::Day);
  93. callback(day_period, other.day_period, Field::DayPeriod);
  94. callback(hour, other.hour, Field::Hour);
  95. callback(minute, other.minute, Field::Minute);
  96. callback(second, other.second, Field::Second);
  97. callback(fractional_second_digits, other.fractional_second_digits, Field::FractionalSecondDigits);
  98. callback(time_zone_name, other.time_zone_name, Field::TimeZoneName);
  99. }
  100. String skeleton {};
  101. String pattern {};
  102. Optional<String> pattern12 {};
  103. Optional<HourCycle> hour_cycle {};
  104. // https://unicode.org/reports/tr35/tr35-dates.html#Calendar_Fields
  105. Optional<CalendarPatternStyle> era {};
  106. Optional<CalendarPatternStyle> year {};
  107. Optional<CalendarPatternStyle> month {};
  108. Optional<CalendarPatternStyle> weekday {};
  109. Optional<CalendarPatternStyle> day {};
  110. Optional<CalendarPatternStyle> day_period {};
  111. Optional<CalendarPatternStyle> hour {};
  112. Optional<CalendarPatternStyle> minute {};
  113. Optional<CalendarPatternStyle> second {};
  114. Optional<u8> fractional_second_digits {};
  115. Optional<CalendarPatternStyle> time_zone_name {};
  116. };
  117. struct CalendarRangePattern : public CalendarPattern {
  118. enum class Field {
  119. Era,
  120. Year,
  121. Month,
  122. Day,
  123. AmPm,
  124. DayPeriod,
  125. Hour,
  126. Minute,
  127. Second,
  128. FractionalSecondDigits,
  129. };
  130. Optional<Field> field {};
  131. String start_range {};
  132. StringView separator {};
  133. String end_range {};
  134. };
  135. enum class CalendarFormatType : u8 {
  136. Date,
  137. Time,
  138. DateTime,
  139. };
  140. struct CalendarFormat {
  141. CalendarPattern full_format {};
  142. CalendarPattern long_format {};
  143. CalendarPattern medium_format {};
  144. CalendarPattern short_format {};
  145. };
  146. enum class CalendarSymbol : u8 {
  147. DayPeriod,
  148. Era,
  149. Month,
  150. Weekday,
  151. };
  152. struct TimeZoneFormat {
  153. StringView symbol_ahead_sign {};
  154. StringView symbol_ahead_separator {};
  155. StringView symbol_behind_sign {};
  156. StringView symbol_behind_separator {};
  157. StringView gmt_format {};
  158. StringView gmt_zero_format {};
  159. };
  160. HourCycle hour_cycle_from_string(StringView hour_cycle);
  161. StringView hour_cycle_to_string(HourCycle hour_cycle);
  162. CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
  163. StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
  164. Optional<Calendar> calendar_from_string(StringView calendar);
  165. Optional<HourCycleRegion> hour_cycle_region_from_string(StringView hour_cycle_region);
  166. Vector<HourCycle> get_regional_hour_cycles(StringView region);
  167. Vector<Unicode::HourCycle> get_locale_hour_cycles(StringView locale);
  168. Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale);
  169. String combine_skeletons(StringView first, StringView second);
  170. Optional<CalendarFormat> get_calendar_date_format(StringView locale, StringView calendar);
  171. Optional<CalendarFormat> get_calendar_time_format(StringView locale, StringView calendar);
  172. Optional<CalendarFormat> get_calendar_date_time_format(StringView locale, StringView calendar);
  173. Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
  174. Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
  175. Optional<Unicode::CalendarRangePattern> get_calendar_default_range_format(StringView locale, StringView calendar);
  176. Vector<Unicode::CalendarRangePattern> get_calendar_range_formats(StringView locale, StringView calendar, StringView skeleton);
  177. Vector<Unicode::CalendarRangePattern> get_calendar_range12_formats(StringView locale, StringView calendar, StringView skeleton);
  178. Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Era value);
  179. Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Month value);
  180. Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Weekday value);
  181. Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::DayPeriod value);
  182. Optional<StringView> get_calendar_day_period_symbol_for_hour(StringView locale, StringView calendar, CalendarPatternStyle style, u8 hour);
  183. String format_time_zone(StringView locale, StringView time_zone, CalendarPatternStyle style, AK::Time time);
  184. Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style);
  185. Optional<TimeZoneFormat> get_time_zone_format(StringView locale);
  186. }