TimeZone.cpp 7.4 KB

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