DateTimeFormat.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2021, 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. Morning,
  45. Afternoon,
  46. Evening,
  47. Night,
  48. };
  49. enum class HourCycle : u8 {
  50. H11,
  51. H12,
  52. H23,
  53. H24,
  54. };
  55. enum class CalendarPatternStyle : u8 {
  56. Narrow,
  57. Short,
  58. Long,
  59. Numeric,
  60. TwoDigit,
  61. };
  62. struct CalendarPattern {
  63. enum class Field {
  64. Era,
  65. Year,
  66. Month,
  67. Weekday,
  68. Day,
  69. DayPeriod,
  70. Hour,
  71. Minute,
  72. Second,
  73. FractionalSecondDigits,
  74. TimeZoneName,
  75. };
  76. template<typename Callback>
  77. void for_each_calendar_field_zipped_with(CalendarPattern const& other, Callback&& callback)
  78. {
  79. callback(era, other.era, Field::Era);
  80. callback(year, other.year, Field::Year);
  81. callback(month, other.month, Field::Month);
  82. callback(weekday, other.weekday, Field::Weekday);
  83. callback(day, other.day, Field::Day);
  84. callback(day_period, other.day_period, Field::DayPeriod);
  85. callback(hour, other.hour, Field::Hour);
  86. callback(minute, other.minute, Field::Minute);
  87. callback(second, other.second, Field::Second);
  88. callback(fractional_second_digits, other.fractional_second_digits, Field::FractionalSecondDigits);
  89. callback(time_zone_name, other.time_zone_name, Field::TimeZoneName);
  90. }
  91. String pattern {};
  92. Optional<String> pattern12 {};
  93. // https://unicode.org/reports/tr35/tr35-dates.html#Calendar_Fields
  94. Optional<CalendarPatternStyle> era {};
  95. Optional<CalendarPatternStyle> year {};
  96. Optional<CalendarPatternStyle> month {};
  97. Optional<CalendarPatternStyle> weekday {};
  98. Optional<CalendarPatternStyle> day {};
  99. Optional<CalendarPatternStyle> day_period {};
  100. Optional<CalendarPatternStyle> hour {};
  101. Optional<CalendarPatternStyle> minute {};
  102. Optional<CalendarPatternStyle> second {};
  103. Optional<u8> fractional_second_digits {};
  104. Optional<CalendarPatternStyle> time_zone_name {};
  105. };
  106. enum class CalendarFormatType : u8 {
  107. Date,
  108. Time,
  109. DateTime,
  110. };
  111. struct CalendarFormat {
  112. CalendarPattern full_format {};
  113. CalendarPattern long_format {};
  114. CalendarPattern medium_format {};
  115. CalendarPattern short_format {};
  116. };
  117. HourCycle hour_cycle_from_string(StringView hour_cycle);
  118. StringView hour_cycle_to_string(HourCycle hour_cycle);
  119. CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
  120. StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
  121. Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale);
  122. Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale);
  123. Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
  124. Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
  125. Optional<StringView> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Era value);
  126. Optional<StringView> get_calendar_month_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Month value);
  127. Optional<StringView> get_calendar_weekday_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::Weekday value);
  128. Optional<StringView> get_calendar_day_period_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Unicode::DayPeriod value);
  129. Optional<StringView> get_time_zone_name(StringView locale, StringView time_zone, CalendarPatternStyle style);
  130. }