TimeZone.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/ByteString.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 IsLink {
  19. No,
  20. Yes,
  21. };
  22. struct TimeZoneIdentifier {
  23. StringView name;
  24. IsLink is_link { IsLink::No };
  25. };
  26. enum class InDST {
  27. No,
  28. Yes,
  29. };
  30. struct Offset {
  31. i64 seconds { 0 };
  32. InDST in_dst { InDST::No };
  33. };
  34. struct NamedOffset : public Offset {
  35. ByteString name;
  36. };
  37. struct Coordinate {
  38. constexpr float decimal_coordinate() const
  39. {
  40. return static_cast<float>(degrees) + (static_cast<float>(minutes) / 60.0f) + (static_cast<float>(seconds) / 3'600.0f);
  41. }
  42. i16 degrees { 0 };
  43. u8 minutes { 0 };
  44. u8 seconds { 0 };
  45. };
  46. struct Location {
  47. Coordinate latitude;
  48. Coordinate longitude;
  49. };
  50. StringView system_time_zone();
  51. StringView current_time_zone();
  52. ErrorOr<void> change_time_zone(StringView time_zone);
  53. ReadonlySpan<TimeZoneIdentifier> all_time_zones();
  54. Optional<TimeZone> time_zone_from_string(StringView time_zone);
  55. StringView time_zone_to_string(TimeZone time_zone);
  56. Optional<StringView> canonicalize_time_zone(StringView time_zone);
  57. Optional<DaylightSavingsRule> daylight_savings_rule_from_string(StringView daylight_savings_rule);
  58. StringView daylight_savings_rule_to_string(DaylightSavingsRule daylight_savings_rule);
  59. Optional<Offset> get_time_zone_offset(TimeZone time_zone, AK::UnixDateTime time);
  60. Optional<Offset> get_time_zone_offset(StringView time_zone, AK::UnixDateTime time);
  61. Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(TimeZone time_zone, AK::UnixDateTime time);
  62. Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(StringView time_zone, AK::UnixDateTime time);
  63. Optional<Location> get_time_zone_location(TimeZone time_zone);
  64. Optional<Location> get_time_zone_location(StringView time_zone);
  65. Optional<Region> region_from_string(StringView region);
  66. StringView region_to_string(Region region);
  67. Vector<StringView> time_zones_in_region(StringView region);
  68. }
  69. template<>
  70. struct AK::Formatter<TimeZone::TimeZone> : Formatter<FormatString> {
  71. ErrorOr<void> format(FormatBuilder& builder, TimeZone::TimeZone const& time_zone)
  72. {
  73. return Formatter<FormatString>::format(builder, TimeZone::time_zone_to_string(time_zone));
  74. }
  75. };