TimeZone.cpp 7.0 KB

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