DateTimeFormat.h 6.2 KB

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