Time.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <LibWeb/Forward.h>
  9. namespace Web::CSS {
  10. class Time {
  11. public:
  12. enum class Type {
  13. S,
  14. Ms,
  15. };
  16. static Optional<Type> unit_from_name(StringView);
  17. Time(double value, Type type);
  18. static Time make_seconds(double);
  19. Time percentage_of(Percentage const&) const;
  20. String to_string() const;
  21. double to_milliseconds() const;
  22. double to_seconds() const;
  23. Type type() const { return m_type; }
  24. double raw_value() const { return m_value; }
  25. StringView unit_name() const;
  26. bool operator==(Time const& other) const
  27. {
  28. return m_type == other.m_type && m_value == other.m_value;
  29. }
  30. int operator<=>(Time const& other) const
  31. {
  32. auto this_seconds = to_seconds();
  33. auto other_seconds = other.to_seconds();
  34. if (this_seconds < other_seconds)
  35. return -1;
  36. if (this_seconds > other_seconds)
  37. return 1;
  38. return 0;
  39. }
  40. static Time resolve_calculated(NonnullRefPtr<CSSMathValue> const&, Layout::Node const&, Time const& reference_value);
  41. private:
  42. Type m_type;
  43. double m_value { 0 };
  44. };
  45. }
  46. template<>
  47. struct AK::Formatter<Web::CSS::Time> : Formatter<StringView> {
  48. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Time const& time)
  49. {
  50. return Formatter<StringView>::format(builder, time.to_string());
  51. }
  52. };