Time.h 1019 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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(int value, Type type);
  18. Time(float value, Type type);
  19. static Time make_seconds(float);
  20. Time percentage_of(Percentage const&) const;
  21. ErrorOr<String> to_string() const;
  22. float to_seconds() const;
  23. bool operator==(Time const& other) const
  24. {
  25. return m_type == other.m_type && m_value == other.m_value;
  26. }
  27. private:
  28. StringView unit_name() const;
  29. Type m_type;
  30. float m_value { 0 };
  31. };
  32. }
  33. template<>
  34. struct AK::Formatter<Web::CSS::Time> : Formatter<StringView> {
  35. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Time const& time)
  36. {
  37. return Formatter<StringView>::format(builder, TRY(time.to_string()));
  38. }
  39. };