TimeZone.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #ifdef AK_OS_SERENITY
  86. return system_time_zone();
  87. #else
  88. static constexpr auto zoneinfo = "/zoneinfo"sv;
  89. char* real_path = realpath("/etc/localtime", nullptr);
  90. ScopeGuard free_path = [real_path]() { free(real_path); };
  91. if (real_path) {
  92. auto time_zone = StringView { real_path, strlen(real_path) };
  93. // The zoneinfo file may be located in paths like /usr/share/zoneinfo/ or /usr/share/zoneinfo.default/.
  94. // We want to strip any such prefix from the path to arrive at the time zone name.
  95. if (auto index = time_zone.find(zoneinfo); index.has_value())
  96. time_zone = time_zone.substring_view(*index + zoneinfo.length());
  97. if (auto index = time_zone.find('/'); index.has_value())
  98. time_zone = time_zone.substring_view(*index + 1);
  99. if (auto maybe_time_zone = canonicalize_time_zone(time_zone); maybe_time_zone.has_value())
  100. return *maybe_time_zone;
  101. dbgln_if(TIME_ZONE_DEBUG, "Could not determine time zone from /etc/localtime: {}", time_zone);
  102. } else {
  103. dbgln_if(TIME_ZONE_DEBUG, "Could not read the /etc/localtime link: {}", strerror(errno));
  104. }
  105. return "UTC"sv;
  106. #endif
  107. }
  108. ErrorOr<void> change_time_zone([[maybe_unused]] StringView time_zone)
  109. {
  110. #ifdef AK_OS_SERENITY
  111. TimeZoneFile time_zone_file("w");
  112. if (auto new_time_zone = canonicalize_time_zone(time_zone); new_time_zone.has_value())
  113. return time_zone_file.write_time_zone(*new_time_zone);
  114. return Error::from_string_literal("Provided time zone is not supported");
  115. #else
  116. // Do not even attempt to change the time zone of someone's host machine.
  117. return {};
  118. #endif
  119. }
  120. ReadonlySpan<TimeZoneIdentifier> __attribute__((weak)) all_time_zones()
  121. {
  122. #if !ENABLE_TIME_ZONE_DATA
  123. static constexpr auto utc = Array { TimeZoneIdentifier { "UTC"sv, IsLink::No } };
  124. return utc;
  125. #else
  126. return {};
  127. #endif
  128. }
  129. Optional<TimeZone> __attribute__((weak)) time_zone_from_string([[maybe_unused]] StringView time_zone)
  130. {
  131. #if !ENABLE_TIME_ZONE_DATA
  132. if (time_zone.equals_ignoring_ascii_case("UTC"sv))
  133. return TimeZone::UTC;
  134. #endif
  135. return {};
  136. }
  137. StringView __attribute__((weak)) time_zone_to_string([[maybe_unused]] TimeZone time_zone)
  138. {
  139. #if !ENABLE_TIME_ZONE_DATA
  140. VERIFY(time_zone == TimeZone::UTC);
  141. return "UTC"sv;
  142. #else
  143. return {};
  144. #endif
  145. }
  146. Optional<StringView> canonicalize_time_zone(StringView time_zone)
  147. {
  148. auto maybe_time_zone = time_zone_from_string(time_zone);
  149. if (!maybe_time_zone.has_value())
  150. return {};
  151. auto canonical_time_zone = time_zone_to_string(*maybe_time_zone);
  152. if (canonical_time_zone.is_one_of("Etc/UTC"sv, "Etc/GMT"sv, "GMT"sv))
  153. return "UTC"sv;
  154. return canonical_time_zone;
  155. }
  156. Optional<DaylightSavingsRule> __attribute__((weak)) daylight_savings_rule_from_string(StringView) { return {}; }
  157. StringView __attribute__((weak)) daylight_savings_rule_to_string(DaylightSavingsRule) { return {}; }
  158. Optional<Offset> __attribute__((weak)) get_time_zone_offset([[maybe_unused]] TimeZone time_zone, AK::UnixDateTime)
  159. {
  160. #if !ENABLE_TIME_ZONE_DATA
  161. VERIFY(time_zone == TimeZone::UTC);
  162. return Offset {};
  163. #else
  164. return {};
  165. #endif
  166. }
  167. Optional<Offset> get_time_zone_offset(StringView time_zone, AK::UnixDateTime time)
  168. {
  169. if (auto maybe_time_zone = time_zone_from_string(time_zone); maybe_time_zone.has_value())
  170. return get_time_zone_offset(*maybe_time_zone, time);
  171. return {};
  172. }
  173. Optional<Array<NamedOffset, 2>> __attribute__((weak)) get_named_time_zone_offsets([[maybe_unused]] TimeZone time_zone, AK::UnixDateTime)
  174. {
  175. #if !ENABLE_TIME_ZONE_DATA
  176. VERIFY(time_zone == TimeZone::UTC);
  177. NamedOffset utc_offset {};
  178. utc_offset.name = "UTC"sv;
  179. return Array { utc_offset, utc_offset };
  180. #else
  181. return {};
  182. #endif
  183. }
  184. Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(StringView time_zone, AK::UnixDateTime time)
  185. {
  186. if (auto maybe_time_zone = time_zone_from_string(time_zone); maybe_time_zone.has_value())
  187. return get_named_time_zone_offsets(*maybe_time_zone, time);
  188. return {};
  189. }
  190. Optional<Location> __attribute__((weak)) get_time_zone_location(TimeZone) { return {}; }
  191. Optional<Location> get_time_zone_location(StringView time_zone)
  192. {
  193. if (auto maybe_time_zone = time_zone_from_string(time_zone); maybe_time_zone.has_value())
  194. return get_time_zone_location(*maybe_time_zone);
  195. return {};
  196. }
  197. Optional<Region> __attribute__((weak)) region_from_string(StringView) { return {}; }
  198. StringView __attribute__((weak)) region_to_string(Region) { return {}; }
  199. Vector<StringView> __attribute__((weak)) time_zones_in_region(StringView) { return {}; }
  200. }