DateTimeFormat.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 HourCycle : u8 {
  15. H11,
  16. H12,
  17. H23,
  18. H24,
  19. };
  20. enum class CalendarPatternStyle : u8 {
  21. Narrow,
  22. Short,
  23. Long,
  24. Numeric,
  25. TwoDigit,
  26. };
  27. struct CalendarPattern {
  28. template<typename Callback>
  29. void for_each_calendar_field_zipped_with(CalendarPattern const& other, Callback&& callback)
  30. {
  31. callback(era, other.era);
  32. callback(year, other.year);
  33. callback(month, other.month);
  34. callback(weekday, other.weekday);
  35. callback(day, other.day);
  36. callback(day_period, other.day_period);
  37. callback(hour, other.hour);
  38. callback(minute, other.minute);
  39. callback(second, other.second);
  40. callback(fractional_second_digits, other.fractional_second_digits);
  41. callback(time_zone_name, other.time_zone_name);
  42. }
  43. String pattern {};
  44. Optional<String> pattern12 {};
  45. // https://unicode.org/reports/tr35/tr35-dates.html#Calendar_Fields
  46. Optional<CalendarPatternStyle> era {};
  47. Optional<CalendarPatternStyle> year {};
  48. Optional<CalendarPatternStyle> month {};
  49. Optional<CalendarPatternStyle> weekday {};
  50. Optional<CalendarPatternStyle> day {};
  51. Optional<CalendarPatternStyle> day_period {};
  52. Optional<CalendarPatternStyle> hour {};
  53. Optional<CalendarPatternStyle> minute {};
  54. Optional<CalendarPatternStyle> second {};
  55. Optional<u8> fractional_second_digits {};
  56. Optional<CalendarPatternStyle> time_zone_name {};
  57. };
  58. enum class CalendarFormatType : u8 {
  59. Date,
  60. Time,
  61. DateTime,
  62. };
  63. struct CalendarFormat {
  64. CalendarPattern full_format {};
  65. CalendarPattern long_format {};
  66. CalendarPattern medium_format {};
  67. CalendarPattern short_format {};
  68. };
  69. HourCycle hour_cycle_from_string(StringView hour_cycle);
  70. StringView hour_cycle_to_string(HourCycle hour_cycle);
  71. CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
  72. StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
  73. Vector<Unicode::HourCycle> get_regional_hour_cycles(StringView locale);
  74. Optional<Unicode::HourCycle> get_default_regional_hour_cycle(StringView locale);
  75. Optional<CalendarFormat> get_calendar_format(StringView locale, StringView calendar, CalendarFormatType type);
  76. Vector<CalendarPattern> get_calendar_available_formats(StringView locale, StringView calendar);
  77. }