RelativeTimeFormat.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullOwnPtr.h>
  8. #include <AK/Optional.h>
  9. #include <AK/String.h>
  10. #include <AK/StringView.h>
  11. #include <AK/Vector.h>
  12. #include <LibUnicode/Forward.h>
  13. namespace Unicode {
  14. // These are just the subset of fields in the CLDR required for ECMA-402.
  15. enum class TimeUnit {
  16. Second,
  17. Minute,
  18. Hour,
  19. Day,
  20. Week,
  21. Month,
  22. Quarter,
  23. Year,
  24. };
  25. Optional<TimeUnit> time_unit_from_string(StringView);
  26. StringView time_unit_to_string(TimeUnit);
  27. enum class NumericDisplay {
  28. Always,
  29. Auto,
  30. };
  31. NumericDisplay numeric_display_from_string(StringView);
  32. StringView numeric_display_to_string(NumericDisplay);
  33. class RelativeTimeFormat {
  34. public:
  35. static NonnullOwnPtr<RelativeTimeFormat> create(StringView locale, Style style);
  36. virtual ~RelativeTimeFormat() = default;
  37. struct Partition {
  38. StringView type;
  39. String value;
  40. StringView unit;
  41. };
  42. virtual String format(double, TimeUnit, NumericDisplay) const = 0;
  43. virtual Vector<Partition> format_to_parts(double, TimeUnit, NumericDisplay) const = 0;
  44. protected:
  45. RelativeTimeFormat() = default;
  46. };
  47. }