DateTimeFormat.h 5.7 KB

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