TimeZone.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Array.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/Error.h>
  10. #include <AK/Format.h>
  11. #include <AK/Optional.h>
  12. #include <AK/StringView.h>
  13. #include <AK/Time.h>
  14. #include <AK/Types.h>
  15. #include <AK/Vector.h>
  16. #include <LibTimeZone/Forward.h>
  17. namespace TimeZone {
  18. enum class InDST {
  19. No,
  20. Yes,
  21. };
  22. struct Offset {
  23. i64 seconds { 0 };
  24. InDST in_dst { InDST::No };
  25. };
  26. struct NamedOffset : public Offset {
  27. DeprecatedString name;
  28. };
  29. struct Coordinate {
  30. constexpr float decimal_coordinate() const
  31. {
  32. return static_cast<float>(degrees) + (static_cast<float>(minutes) / 60.0f) + (static_cast<float>(seconds) / 3'600.0f);
  33. }
  34. i16 degrees { 0 };
  35. u8 minutes { 0 };
  36. u8 seconds { 0 };
  37. };
  38. struct Location {
  39. Coordinate latitude;
  40. Coordinate longitude;
  41. };
  42. StringView system_time_zone();
  43. StringView current_time_zone();
  44. ErrorOr<void> change_time_zone(StringView time_zone);
  45. ReadonlySpan<StringView> all_time_zones();
  46. Optional<TimeZone> time_zone_from_string(StringView time_zone);
  47. StringView time_zone_to_string(TimeZone time_zone);
  48. Optional<StringView> canonicalize_time_zone(StringView time_zone);
  49. Optional<DaylightSavingsRule> daylight_savings_rule_from_string(StringView daylight_savings_rule);
  50. StringView daylight_savings_rule_to_string(DaylightSavingsRule daylight_savings_rule);
  51. Optional<Offset> get_time_zone_offset(TimeZone time_zone, AK::Time time);
  52. Optional<Offset> get_time_zone_offset(StringView time_zone, AK::Time time);
  53. Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(TimeZone time_zone, AK::Time time);
  54. Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(StringView time_zone, AK::Time time);
  55. Optional<Location> get_time_zone_location(TimeZone time_zone);
  56. Optional<Location> get_time_zone_location(StringView time_zone);
  57. Optional<Region> region_from_string(StringView region);
  58. StringView region_to_string(Region region);
  59. Vector<StringView> time_zones_in_region(StringView region);
  60. }
  61. template<>
  62. struct AK::Formatter<TimeZone::TimeZone> : Formatter<FormatString> {
  63. ErrorOr<void> format(FormatBuilder& builder, TimeZone::TimeZone const& time_zone)
  64. {
  65. return Formatter<FormatString>::format(builder, TimeZone::time_zone_to_string(time_zone));
  66. }
  67. };