TimeZone.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/String.h>
  8. #include <LibTimeZone/TimeZone.h>
  9. #include <limits.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <unistd.h>
  15. namespace TimeZone {
  16. // NOTE: Without ENABLE_TIME_ZONE_DATA LibTimeZone operates in a UTC-only mode and only recognizes
  17. // the 'UTC' time zone, which is slightly more useful than a bunch of dummy functions that
  18. // can't do anything. When we build with time zone data, these weakly linked functions are
  19. // replaced with their proper counterparts.
  20. #if !ENABLE_TIME_ZONE_DATA
  21. enum class TimeZone : u16 {
  22. UTC,
  23. };
  24. #endif
  25. class TimeZoneFile {
  26. public:
  27. TimeZoneFile(char const* mode)
  28. : m_file(fopen("/etc/timezone", mode))
  29. {
  30. if (m_file)
  31. flockfile(m_file);
  32. }
  33. ~TimeZoneFile()
  34. {
  35. if (m_file) {
  36. funlockfile(m_file);
  37. fclose(m_file);
  38. }
  39. }
  40. ErrorOr<String> read_time_zone()
  41. {
  42. if (!m_file)
  43. return Error::from_string_literal("Could not open /etc/timezone");
  44. Array<u8, 128> buffer;
  45. size_t bytes = fread(buffer.data(), 1, buffer.size(), m_file);
  46. if (bytes == 0)
  47. return Error::from_string_literal("Could not read time zone from /etc/timezone");
  48. return String(buffer.span().slice(0, bytes)).trim_whitespace();
  49. }
  50. ErrorOr<void> write_time_zone(StringView time_zone)
  51. {
  52. if (!m_file)
  53. return Error::from_string_literal("Could not open /etc/timezone");
  54. auto bytes = fwrite(time_zone.characters_without_null_termination(), 1, time_zone.length(), m_file);
  55. if (bytes != time_zone.length())
  56. return Error::from_string_literal("Could not write new time zone to /etc/timezone");
  57. return {};
  58. }
  59. private:
  60. FILE* m_file { nullptr };
  61. };
  62. StringView system_time_zone()
  63. {
  64. TimeZoneFile time_zone_file("r");
  65. auto time_zone = time_zone_file.read_time_zone();
  66. // FIXME: Propagate the error to existing callers.
  67. if (time_zone.is_error()) {
  68. dbgln_if(TIME_ZONE_DEBUG, "{}", time_zone.error());
  69. return "UTC"sv;
  70. }
  71. return canonicalize_time_zone(time_zone.value()).value_or("UTC"sv);
  72. }
  73. StringView current_time_zone()
  74. {
  75. if (char* tz = getenv("TZ"); tz != nullptr) {
  76. // FIXME: Actually parse the TZ environment variable, described here:
  77. // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08
  78. StringView time_zone { tz, strlen(tz) };
  79. if (auto maybe_time_zone = canonicalize_time_zone(time_zone); maybe_time_zone.has_value())
  80. return *maybe_time_zone;
  81. dbgln_if(TIME_ZONE_DEBUG, "Could not determine time zone from TZ environment: {}", time_zone);
  82. }
  83. #ifdef __serenity__
  84. return system_time_zone();
  85. #else
  86. static constexpr auto zoneinfo = "/zoneinfo/"sv;
  87. char buffer[PATH_MAX];
  88. if (auto size = readlink("/etc/localtime", buffer, sizeof(buffer)); size > 0) {
  89. StringView time_zone { buffer, static_cast<size_t>(size) };
  90. if (auto index = time_zone.find(zoneinfo); index.has_value())
  91. time_zone = time_zone.substring_view(*index + zoneinfo.length());
  92. if (auto maybe_time_zone = canonicalize_time_zone(time_zone); maybe_time_zone.has_value())
  93. return *maybe_time_zone;
  94. dbgln_if(TIME_ZONE_DEBUG, "Could not determine time zone from /etc/localtime: {}", time_zone);
  95. } else {
  96. dbgln_if(TIME_ZONE_DEBUG, "Could not read the /etc/localtime link: {}", strerror(errno));
  97. }
  98. return "UTC"sv;
  99. #endif
  100. }
  101. ErrorOr<void> change_time_zone([[maybe_unused]] StringView time_zone)
  102. {
  103. #ifdef __serenity__
  104. TimeZoneFile time_zone_file("w");
  105. if (auto new_time_zone = canonicalize_time_zone(time_zone); new_time_zone.has_value())
  106. return time_zone_file.write_time_zone(*new_time_zone);
  107. return Error::from_string_literal("Provided time zone is not supported");
  108. #else
  109. // Do not even attempt to change the time zone of someone's host machine.
  110. return {};
  111. #endif
  112. }
  113. Span<StringView const> __attribute__((weak)) all_time_zones()
  114. {
  115. #if !ENABLE_TIME_ZONE_DATA
  116. static constexpr auto utc = Array { "UTC"sv };
  117. return utc;
  118. #else
  119. return {};
  120. #endif
  121. }
  122. Optional<TimeZone> __attribute__((weak)) time_zone_from_string([[maybe_unused]] StringView time_zone)
  123. {
  124. #if !ENABLE_TIME_ZONE_DATA
  125. if (time_zone.equals_ignoring_case("UTC"sv))
  126. return TimeZone::UTC;
  127. #endif
  128. return {};
  129. }
  130. StringView __attribute__((weak)) time_zone_to_string([[maybe_unused]] TimeZone time_zone)
  131. {
  132. #if !ENABLE_TIME_ZONE_DATA
  133. VERIFY(time_zone == TimeZone::UTC);
  134. return "UTC"sv;
  135. #else
  136. return {};
  137. #endif
  138. }
  139. Optional<StringView> canonicalize_time_zone(StringView time_zone)
  140. {
  141. auto maybe_time_zone = time_zone_from_string(time_zone);
  142. if (!maybe_time_zone.has_value())
  143. return {};
  144. auto canonical_time_zone = time_zone_to_string(*maybe_time_zone);
  145. if (canonical_time_zone.is_one_of("Etc/UTC"sv, "Etc/GMT"sv))
  146. return "UTC"sv;
  147. return canonical_time_zone;
  148. }
  149. Optional<DaylightSavingsRule> __attribute__((weak)) daylight_savings_rule_from_string(StringView) { return {}; }
  150. StringView __attribute__((weak)) daylight_savings_rule_to_string(DaylightSavingsRule) { return {}; }
  151. Optional<Offset> __attribute__((weak)) get_time_zone_offset([[maybe_unused]] TimeZone time_zone, AK::Time)
  152. {
  153. #if !ENABLE_TIME_ZONE_DATA
  154. VERIFY(time_zone == TimeZone::UTC);
  155. return Offset {};
  156. #else
  157. return {};
  158. #endif
  159. }
  160. Optional<Offset> get_time_zone_offset(StringView time_zone, AK::Time time)
  161. {
  162. if (auto maybe_time_zone = time_zone_from_string(time_zone); maybe_time_zone.has_value())
  163. return get_time_zone_offset(*maybe_time_zone, time);
  164. return {};
  165. }
  166. Optional<Array<NamedOffset, 2>> __attribute__((weak)) get_named_time_zone_offsets([[maybe_unused]] TimeZone time_zone, AK::Time)
  167. {
  168. #if !ENABLE_TIME_ZONE_DATA
  169. VERIFY(time_zone == TimeZone::UTC);
  170. NamedOffset utc_offset {};
  171. utc_offset.name = "UTC"sv;
  172. return Array { utc_offset, utc_offset };
  173. #else
  174. return {};
  175. #endif
  176. }
  177. Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(StringView time_zone, AK::Time time)
  178. {
  179. if (auto maybe_time_zone = time_zone_from_string(time_zone); maybe_time_zone.has_value())
  180. return get_named_time_zone_offsets(*maybe_time_zone, time);
  181. return {};
  182. }
  183. Optional<Location> __attribute__((weak)) get_time_zone_location(TimeZone) { return {}; }
  184. Optional<Location> get_time_zone_location(StringView time_zone)
  185. {
  186. if (auto maybe_time_zone = time_zone_from_string(time_zone); maybe_time_zone.has_value())
  187. return get_time_zone_location(*maybe_time_zone);
  188. return {};
  189. }
  190. Optional<Region> __attribute__((weak)) region_from_string(StringView) { return {}; }
  191. StringView __attribute__((weak)) region_to_string(Region) { return {}; }
  192. Vector<StringView> __attribute__((weak)) time_zones_in_region(StringView) { return {}; }
  193. }