GenerateTimeZoneData.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "../LibUnicode/GeneratorUtil.h" // FIXME: Move this somewhere common.
  7. #include <AK/DateConstants.h>
  8. #include <AK/DeprecatedString.h>
  9. #include <AK/Error.h>
  10. #include <AK/Format.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/SourceGenerator.h>
  13. #include <AK/StringBuilder.h>
  14. #include <AK/Vector.h>
  15. #include <LibCore/ArgsParser.h>
  16. #include <LibTimeZone/TimeZone.h>
  17. namespace {
  18. struct DateTime {
  19. u16 year { 0 };
  20. Optional<u8> month;
  21. Optional<u8> day;
  22. Optional<u8> last_weekday;
  23. Optional<u8> after_weekday;
  24. Optional<u8> before_weekday;
  25. Optional<u8> hour;
  26. Optional<u8> minute;
  27. Optional<u8> second;
  28. };
  29. struct TimeZoneOffset {
  30. i64 offset { 0 };
  31. Optional<DateTime> until;
  32. Optional<DeprecatedString> dst_rule;
  33. Optional<i32> dst_rule_index;
  34. i64 dst_offset { 0 };
  35. size_t standard_format { 0 };
  36. size_t daylight_format { 0 };
  37. };
  38. struct DaylightSavingsOffset {
  39. i64 offset { 0 };
  40. u16 year_from { 0 };
  41. Optional<u16> year_to;
  42. DateTime in_effect;
  43. size_t format { 0 };
  44. };
  45. struct TimeZoneData {
  46. UniqueStringStorage unique_strings;
  47. HashMap<DeprecatedString, Vector<TimeZoneOffset>> time_zones;
  48. Vector<DeprecatedString> time_zone_names;
  49. Vector<Alias> time_zone_aliases;
  50. HashMap<DeprecatedString, Vector<DaylightSavingsOffset>> dst_offsets;
  51. Vector<DeprecatedString> dst_offset_names;
  52. HashMap<DeprecatedString, TimeZone::Location> time_zone_coordinates;
  53. HashMap<DeprecatedString, Vector<size_t>> time_zone_regions;
  54. Vector<DeprecatedString> time_zone_region_names;
  55. };
  56. }
  57. template<>
  58. struct AK::Formatter<DateTime> : Formatter<FormatString> {
  59. ErrorOr<void> format(FormatBuilder& builder, DateTime const& date_time)
  60. {
  61. return Formatter<FormatString>::format(builder,
  62. "{{ {}, {}, {}, {}, {}, {}, {}, {}, {} }}"sv,
  63. date_time.year,
  64. date_time.month.value_or(1),
  65. date_time.day.value_or(1),
  66. date_time.last_weekday.value_or(0),
  67. date_time.after_weekday.value_or(0),
  68. date_time.before_weekday.value_or(0),
  69. date_time.hour.value_or(0),
  70. date_time.minute.value_or(0),
  71. date_time.second.value_or(0));
  72. }
  73. };
  74. template<>
  75. struct AK::Formatter<TimeZoneOffset> : Formatter<FormatString> {
  76. ErrorOr<void> format(FormatBuilder& builder, TimeZoneOffset const& time_zone_offset)
  77. {
  78. return Formatter<FormatString>::format(builder,
  79. "{{ {}, {}, {}, {}, {}, {}, {} }}"sv,
  80. time_zone_offset.offset,
  81. time_zone_offset.until.value_or({}),
  82. time_zone_offset.until.has_value(),
  83. time_zone_offset.dst_rule_index.value_or(-1),
  84. time_zone_offset.dst_offset,
  85. time_zone_offset.standard_format,
  86. time_zone_offset.daylight_format);
  87. }
  88. };
  89. template<>
  90. struct AK::Formatter<DaylightSavingsOffset> : Formatter<FormatString> {
  91. ErrorOr<void> format(FormatBuilder& builder, DaylightSavingsOffset const& dst_offset)
  92. {
  93. auto format_time = [&](auto year) {
  94. return DeprecatedString::formatted("AK::Time::from_timestamp({}, 1, 1, 0, 0, 0, 0)", year);
  95. };
  96. static DeprecatedString max_year_as_time("max_year_as_time"sv);
  97. return Formatter<FormatString>::format(builder,
  98. "{{ {}, {}, {}, {}, {} }}"sv,
  99. dst_offset.offset,
  100. format_time(dst_offset.year_from),
  101. dst_offset.year_to.has_value()
  102. ? format_time(*dst_offset.year_to + 1)
  103. : max_year_as_time,
  104. dst_offset.in_effect,
  105. dst_offset.format);
  106. }
  107. };
  108. template<>
  109. struct AK::Formatter<TimeZone::Coordinate> : Formatter<FormatString> {
  110. ErrorOr<void> format(FormatBuilder& builder, TimeZone::Coordinate const& coordinate)
  111. {
  112. return Formatter<FormatString>::format(builder,
  113. "{{ {}, {}, {} }}"sv,
  114. coordinate.degrees,
  115. coordinate.minutes,
  116. coordinate.seconds);
  117. }
  118. };
  119. template<>
  120. struct AK::Formatter<TimeZone::Location> : Formatter<FormatString> {
  121. ErrorOr<void> format(FormatBuilder& builder, TimeZone::Location const& location)
  122. {
  123. return Formatter<FormatString>::format(builder,
  124. "{{ {}, {} }}"sv,
  125. location.latitude,
  126. location.longitude);
  127. }
  128. };
  129. static Optional<DateTime> parse_date_time(ReadonlySpan<StringView> segments)
  130. {
  131. auto comment_index = find_index(segments.begin(), segments.end(), "#"sv);
  132. if (comment_index != segments.size())
  133. segments = segments.slice(0, comment_index);
  134. if (segments.is_empty())
  135. return {};
  136. DateTime date_time {};
  137. date_time.year = segments[0].to_uint().value();
  138. if (segments.size() > 1)
  139. date_time.month = find_index(short_month_names.begin(), short_month_names.end(), segments[1]) + 1;
  140. if (segments.size() > 2) {
  141. if (segments[2].starts_with("last"sv)) {
  142. auto weekday = segments[2].substring_view("last"sv.length());
  143. date_time.last_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
  144. } else if (auto index = segments[2].find(">="sv); index.has_value()) {
  145. auto weekday = segments[2].substring_view(0, *index);
  146. date_time.after_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
  147. auto day = segments[2].substring_view(*index + ">="sv.length());
  148. date_time.day = day.to_uint().value();
  149. } else if (auto index = segments[2].find("<="sv); index.has_value()) {
  150. auto weekday = segments[2].substring_view(0, *index);
  151. date_time.before_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
  152. auto day = segments[2].substring_view(*index + "<="sv.length());
  153. date_time.day = day.to_uint().value();
  154. } else {
  155. date_time.day = segments[2].to_uint().value();
  156. }
  157. }
  158. if (segments.size() > 3) {
  159. // FIXME: Some times end with a letter, e.g. "2:00u" and "2:00s". Figure out what this means and handle it.
  160. auto time_segments = segments[3].split_view(':');
  161. date_time.hour = time_segments[0].to_int().value();
  162. date_time.minute = time_segments.size() > 1 ? time_segments[1].substring_view(0, 2).to_uint().value() : 0;
  163. date_time.second = time_segments.size() > 2 ? time_segments[2].substring_view(0, 2).to_uint().value() : 0;
  164. }
  165. return date_time;
  166. }
  167. static i64 parse_time_offset(StringView segment)
  168. {
  169. auto segments = segment.split_view(':');
  170. i64 hours = segments[0].to_int().value();
  171. i64 minutes = segments.size() > 1 ? segments[1].to_uint().value() : 0;
  172. i64 seconds = segments.size() > 2 ? segments[2].to_uint().value() : 0;
  173. i64 sign = ((hours < 0) || (segments[0] == "-0"sv)) ? -1 : 1;
  174. return (hours * 3600) + sign * ((minutes * 60) + seconds);
  175. }
  176. static void parse_dst_rule(StringView segment, TimeZoneOffset& time_zone)
  177. {
  178. if (segment.contains(':'))
  179. time_zone.dst_offset = parse_time_offset(segment);
  180. else if (segment != "-"sv)
  181. time_zone.dst_rule = segment;
  182. }
  183. static void parse_format(StringView format, TimeZoneData& time_zone_data, TimeZoneOffset& time_zone)
  184. {
  185. auto formats = format.replace("%s"sv, "{}"sv, ReplaceMode::FirstOnly).split('/');
  186. VERIFY(formats.size() <= 2);
  187. time_zone.standard_format = time_zone_data.unique_strings.ensure(formats[0]);
  188. if (formats.size() == 2)
  189. time_zone.daylight_format = time_zone_data.unique_strings.ensure(formats[1]);
  190. else
  191. time_zone.daylight_format = time_zone.standard_format;
  192. }
  193. static Vector<TimeZoneOffset>& parse_zone(StringView zone_line, TimeZoneData& time_zone_data)
  194. {
  195. auto segments = zone_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
  196. // "Zone" NAME STDOFF RULES FORMAT [UNTIL]
  197. VERIFY(segments[0] == "Zone"sv);
  198. auto name = segments[1];
  199. TimeZoneOffset time_zone {};
  200. time_zone.offset = parse_time_offset(segments[2]);
  201. parse_dst_rule(segments[3], time_zone);
  202. parse_format(segments[4], time_zone_data, time_zone);
  203. if (segments.size() > 5)
  204. time_zone.until = parse_date_time(segments.span().slice(5));
  205. auto& time_zones = time_zone_data.time_zones.ensure(name);
  206. time_zones.append(move(time_zone));
  207. if (!time_zone_data.time_zone_names.contains_slow(name))
  208. time_zone_data.time_zone_names.append(name);
  209. return time_zones;
  210. }
  211. static void parse_zone_continuation(StringView zone_line, TimeZoneData& time_zone_data, Vector<TimeZoneOffset>& time_zones)
  212. {
  213. auto segments = zone_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
  214. // STDOFF RULES FORMAT [UNTIL]
  215. TimeZoneOffset time_zone {};
  216. time_zone.offset = parse_time_offset(segments[0]);
  217. parse_dst_rule(segments[1], time_zone);
  218. parse_format(segments[2], time_zone_data, time_zone);
  219. if (segments.size() > 3)
  220. time_zone.until = parse_date_time(segments.span().slice(3));
  221. time_zones.append(move(time_zone));
  222. }
  223. static void parse_link(StringView link_line, TimeZoneData& time_zone_data)
  224. {
  225. auto segments = link_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
  226. // Link TARGET LINK-NAME
  227. VERIFY(segments[0] == "Link"sv);
  228. auto target = segments[1];
  229. auto alias = segments[2];
  230. time_zone_data.time_zone_aliases.append({ target, alias });
  231. }
  232. static void parse_rule(StringView rule_line, TimeZoneData& time_zone_data)
  233. {
  234. auto segments = rule_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
  235. // Rule NAME FROM TO TYPE IN ON AT SAVE LETTER/S
  236. VERIFY(segments[0] == "Rule"sv);
  237. auto name = segments[1];
  238. DaylightSavingsOffset dst_offset {};
  239. dst_offset.offset = parse_time_offset(segments[8]);
  240. dst_offset.year_from = segments[2].to_uint().value();
  241. if (segments[3] == "only")
  242. dst_offset.year_to = dst_offset.year_from;
  243. else if (segments[3] != "max"sv)
  244. dst_offset.year_to = segments[3].to_uint().value();
  245. auto in_effect = Array { "0"sv, segments[5], segments[6], segments[7] };
  246. dst_offset.in_effect = parse_date_time(in_effect).release_value();
  247. if (segments[9] != "-"sv)
  248. dst_offset.format = time_zone_data.unique_strings.ensure(segments[9]);
  249. auto& dst_offsets = time_zone_data.dst_offsets.ensure(name);
  250. dst_offsets.append(move(dst_offset));
  251. if (!time_zone_data.dst_offset_names.contains_slow(name))
  252. time_zone_data.dst_offset_names.append(name);
  253. }
  254. static ErrorOr<void> parse_time_zones(StringView time_zone_path, TimeZoneData& time_zone_data)
  255. {
  256. // For reference, the man page for `zic` has the best documentation of the TZDB file format.
  257. auto file = TRY(open_file(time_zone_path, Core::File::OpenMode::Read));
  258. Array<u8, 1024> buffer {};
  259. Vector<TimeZoneOffset>* last_parsed_zone = nullptr;
  260. while (TRY(file->can_read_line())) {
  261. auto line = TRY(file->read_line(buffer));
  262. if (line.is_empty() || line.trim_whitespace(TrimMode::Left).starts_with('#'))
  263. continue;
  264. if (line.starts_with("Zone"sv)) {
  265. last_parsed_zone = &parse_zone(line, time_zone_data);
  266. } else if (line.starts_with('\t')) {
  267. VERIFY(last_parsed_zone != nullptr);
  268. parse_zone_continuation(line, time_zone_data, *last_parsed_zone);
  269. } else {
  270. last_parsed_zone = nullptr;
  271. if (line.starts_with("Link"sv))
  272. parse_link(line, time_zone_data);
  273. else if (line.starts_with("Rule"sv))
  274. parse_rule(line, time_zone_data);
  275. }
  276. }
  277. return {};
  278. }
  279. static ErrorOr<void> parse_time_zone_coordinates(Core::BufferedFile& file, TimeZoneData& time_zone_data)
  280. {
  281. auto parse_coordinate = [](auto coordinate) {
  282. VERIFY(coordinate.substring_view(0, 1).is_one_of("+"sv, "-"sv));
  283. TimeZone::Coordinate parsed {};
  284. if (coordinate.length() == 5) {
  285. // ±DDMM
  286. parsed.degrees = coordinate.substring_view(0, 3).to_int().value();
  287. parsed.minutes = coordinate.substring_view(3).to_int().value();
  288. } else if (coordinate.length() == 6) {
  289. // ±DDDMM
  290. parsed.degrees = coordinate.substring_view(0, 4).to_int().value();
  291. parsed.minutes = coordinate.substring_view(4).to_int().value();
  292. } else if (coordinate.length() == 7) {
  293. // ±DDMMSS
  294. parsed.degrees = coordinate.substring_view(0, 3).to_int().value();
  295. parsed.minutes = coordinate.substring_view(3, 2).to_int().value();
  296. parsed.seconds = coordinate.substring_view(5).to_int().value();
  297. } else if (coordinate.length() == 8) {
  298. // ±DDDDMMSS
  299. parsed.degrees = coordinate.substring_view(0, 4).to_int().value();
  300. parsed.minutes = coordinate.substring_view(4, 2).to_int().value();
  301. parsed.seconds = coordinate.substring_view(6).to_int().value();
  302. } else {
  303. VERIFY_NOT_REACHED();
  304. }
  305. return parsed;
  306. };
  307. Array<u8, 1024> buffer {};
  308. while (TRY(file.can_read_line())) {
  309. auto line = TRY(file.read_line(buffer));
  310. if (line.is_empty() || line.trim_whitespace(TrimMode::Left).starts_with('#'))
  311. continue;
  312. auto segments = line.split_view('\t');
  313. auto regions = segments[0];
  314. auto coordinates = segments[1];
  315. auto zone = segments[2];
  316. VERIFY(time_zone_data.time_zones.contains(zone));
  317. auto index = coordinates.find_any_of("+-"sv, StringView::SearchDirection::Backward).value();
  318. auto latitude = parse_coordinate(coordinates.substring_view(0, index));
  319. auto longitude = parse_coordinate(coordinates.substring_view(index));
  320. time_zone_data.time_zone_coordinates.set(zone, { latitude, longitude });
  321. TRY(regions.for_each_split_view(',', SplitBehavior::Nothing, [&](auto region) -> ErrorOr<void> {
  322. auto index = time_zone_data.unique_strings.ensure(zone);
  323. TRY(time_zone_data.time_zone_regions.ensure(region).try_append(index));
  324. if (!time_zone_data.time_zone_region_names.contains_slow(region))
  325. TRY(time_zone_data.time_zone_region_names.try_append(region));
  326. return {};
  327. }));
  328. }
  329. return {};
  330. }
  331. static void set_dst_rule_indices(TimeZoneData& time_zone_data)
  332. {
  333. for (auto& time_zone : time_zone_data.time_zones) {
  334. for (auto& time_zone_offset : time_zone.value) {
  335. if (!time_zone_offset.dst_rule.has_value())
  336. continue;
  337. auto dst_rule_index = time_zone_data.dst_offset_names.find_first_index(*time_zone_offset.dst_rule);
  338. time_zone_offset.dst_rule_index = static_cast<i32>(dst_rule_index.value());
  339. }
  340. }
  341. }
  342. static DeprecatedString format_identifier(StringView owner, DeprecatedString identifier)
  343. {
  344. constexpr auto gmt_time_zones = Array { "Etc/GMT"sv, "GMT"sv };
  345. for (auto gmt_time_zone : gmt_time_zones) {
  346. if (identifier.starts_with(gmt_time_zone)) {
  347. auto offset = identifier.substring_view(gmt_time_zone.length());
  348. if (offset.starts_with('+'))
  349. identifier = DeprecatedString::formatted("{}_Ahead_{}", gmt_time_zone, offset.substring_view(1));
  350. else if (offset.starts_with('-'))
  351. identifier = DeprecatedString::formatted("{}_Behind_{}", gmt_time_zone, offset.substring_view(1));
  352. }
  353. }
  354. identifier = identifier.replace("-"sv, "_"sv, ReplaceMode::All);
  355. identifier = identifier.replace("/"sv, "_"sv, ReplaceMode::All);
  356. if (all_of(identifier, is_ascii_digit))
  357. return DeprecatedString::formatted("{}_{}", owner[0], identifier);
  358. if (is_ascii_lower_alpha(identifier[0]))
  359. return DeprecatedString::formatted("{:c}{}", to_ascii_uppercase(identifier[0]), identifier.substring_view(1));
  360. return identifier;
  361. }
  362. static ErrorOr<void> generate_time_zone_data_header(Core::BufferedFile& file, TimeZoneData& time_zone_data)
  363. {
  364. StringBuilder builder;
  365. SourceGenerator generator { builder };
  366. generator.append(R"~~~(
  367. #pragma once
  368. #include <AK/Types.h>
  369. namespace TimeZone {
  370. )~~~");
  371. generate_enum(generator, format_identifier, "TimeZone"sv, {}, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases);
  372. generate_enum(generator, format_identifier, "DaylightSavingsRule"sv, {}, time_zone_data.dst_offset_names);
  373. generate_enum(generator, format_identifier, "Region"sv, {}, time_zone_data.time_zone_region_names);
  374. generator.append(R"~~~(
  375. }
  376. )~~~");
  377. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  378. return {};
  379. }
  380. static ErrorOr<void> generate_time_zone_data_implementation(Core::BufferedFile& file, TimeZoneData& time_zone_data)
  381. {
  382. StringBuilder builder;
  383. SourceGenerator generator { builder };
  384. generator.set("string_index_type"sv, time_zone_data.unique_strings.type_that_fits());
  385. set_dst_rule_indices(time_zone_data);
  386. generator.append(R"~~~(
  387. #include <AK/Array.h>
  388. #include <AK/BinarySearch.h>
  389. #include <AK/Optional.h>
  390. #include <AK/Span.h>
  391. #include <AK/NumericLimits.h>
  392. #include <AK/StringView.h>
  393. #include <AK/Time.h>
  394. #include <LibTimeZone/TimeZone.h>
  395. #include <LibTimeZone/TimeZoneData.h>
  396. namespace TimeZone {
  397. static constexpr auto max_year_as_time = AK::Time::from_timestamp(NumericLimits<u16>::max(), 1, 1, 0, 0, 0, 0);
  398. struct DateTime {
  399. AK::Time time_since_epoch() const
  400. {
  401. // FIXME: This implementation does not take last_weekday, after_weekday, or before_weekday into account.
  402. return AK::Time::from_timestamp(year, month, day, hour, minute, second, 0);
  403. }
  404. u16 year { 0 };
  405. u8 month { 1 };
  406. u8 day { 1 };
  407. u8 last_weekday { 0 };
  408. u8 after_weekday { 0 };
  409. u8 before_weekday { 0 };
  410. u8 hour { 0 };
  411. u8 minute { 0 };
  412. u8 second { 0 };
  413. };
  414. struct TimeZoneOffset {
  415. i64 offset { 0 };
  416. DateTime until {};
  417. bool has_until { false };
  418. i32 dst_rule { -1 };
  419. i64 dst_offset { 0 };
  420. @string_index_type@ standard_format { 0 };
  421. @string_index_type@ daylight_format { 0 };
  422. };
  423. struct DaylightSavingsOffset {
  424. AK::Time time_in_effect(AK::Time time) const
  425. {
  426. auto in_effect = this->in_effect;
  427. in_effect.year = seconds_since_epoch_to_year(time.to_seconds());
  428. return in_effect.time_since_epoch();
  429. }
  430. i64 offset { 0 };
  431. AK::Time year_from {};
  432. AK::Time year_to {};
  433. DateTime in_effect {};
  434. @string_index_type@ format { 0 };
  435. };
  436. )~~~");
  437. time_zone_data.unique_strings.generate(generator);
  438. auto append_offsets = [&](auto const& name, auto type, auto const& offsets) {
  439. generator.set("name", name);
  440. generator.set("type", type);
  441. generator.set("size", DeprecatedString::number(offsets.size()));
  442. generator.append(R"~~~(
  443. static constexpr Array<@type@, @size@> @name@ { {
  444. )~~~");
  445. for (auto const& offset : offsets)
  446. generator.append(DeprecatedString::formatted(" {},\n", offset));
  447. generator.append("} };\n");
  448. };
  449. generate_mapping(generator, time_zone_data.time_zone_names, "TimeZoneOffset"sv, "s_time_zone_offsets"sv, "s_time_zone_offsets_{}"sv, format_identifier,
  450. [&](auto const& name, auto const& value) {
  451. auto const& time_zone_offsets = time_zone_data.time_zones.find(value)->value;
  452. append_offsets(name, "TimeZoneOffset"sv, time_zone_offsets);
  453. });
  454. generate_mapping(generator, time_zone_data.dst_offset_names, "DaylightSavingsOffset"sv, "s_dst_offsets"sv, "s_dst_offsets_{}"sv, format_identifier,
  455. [&](auto const& name, auto const& value) {
  456. auto const& dst_offsets = time_zone_data.dst_offsets.find(value)->value;
  457. append_offsets(name, "DaylightSavingsOffset"sv, dst_offsets);
  458. });
  459. generate_mapping(generator, time_zone_data.time_zone_region_names, time_zone_data.unique_strings.type_that_fits(), "s_regional_time_zones"sv, "s_regional_time_zones_{}"sv, format_identifier,
  460. [&](auto const& name, auto const& value) {
  461. auto const& time_zones = time_zone_data.time_zone_regions.find(value)->value;
  462. generator.set("name", name);
  463. generator.set("size", DeprecatedString::number(time_zones.size()));
  464. generator.append(R"~~~(
  465. static constexpr Array<@string_index_type@, @size@> @name@ { {)~~~");
  466. bool first = true;
  467. for (auto const& time_zone : time_zones) {
  468. generator.append(first ? " "sv : ", "sv);
  469. generator.append(DeprecatedString::number(time_zone));
  470. first = false;
  471. }
  472. generator.append(" } };");
  473. });
  474. generator.set("size", DeprecatedString::number(time_zone_data.time_zone_names.size()));
  475. generator.append(R"~~~(
  476. static constexpr Array<Location, @size@> s_time_zone_locations { {
  477. )~~~");
  478. for (auto const& time_zone : time_zone_data.time_zone_names) {
  479. auto location = time_zone_data.time_zone_coordinates.get(time_zone).value_or({});
  480. generator.append(DeprecatedString::formatted(" {},\n", location));
  481. }
  482. generator.append("} };\n");
  483. auto append_string_conversions = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) -> ErrorOr<void> {
  484. HashValueMap<DeprecatedString> hashes;
  485. TRY(hashes.try_ensure_capacity(values.size()));
  486. auto hash = [](auto const& value) {
  487. return CaseInsensitiveASCIIStringViewTraits::hash(value);
  488. };
  489. for (auto const& value : values)
  490. hashes.set(hash(value), format_identifier(enum_title, value));
  491. for (auto const& alias : aliases)
  492. hashes.set(hash(alias.alias), format_identifier(enum_title, alias.alias));
  493. ValueFromStringOptions options {};
  494. options.sensitivity = CaseSensitivity::CaseInsensitive;
  495. generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes), options);
  496. generate_value_to_string(generator, "{}_to_string"sv, enum_title, enum_snake, format_identifier, values);
  497. return {};
  498. };
  499. TRY(append_string_conversions("TimeZone"sv, "time_zone"sv, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases));
  500. TRY(append_string_conversions("DaylightSavingsRule"sv, "daylight_savings_rule"sv, time_zone_data.dst_offset_names));
  501. TRY(append_string_conversions("Region"sv, "region"sv, time_zone_data.time_zone_region_names));
  502. generator.append(R"~~~(
  503. static Array<DaylightSavingsOffset const*, 2> find_dst_offsets(TimeZoneOffset const& time_zone_offset, AK::Time time)
  504. {
  505. auto const& dst_rules = s_dst_offsets[time_zone_offset.dst_rule];
  506. DaylightSavingsOffset const* standard_offset = nullptr;
  507. DaylightSavingsOffset const* daylight_offset = nullptr;
  508. DaylightSavingsOffset const* last_offset = nullptr;
  509. auto preferred_rule = [&](auto* current_offset, auto& new_offset) {
  510. if (!current_offset)
  511. return &new_offset;
  512. auto new_time_in_effect = new_offset.time_in_effect(time);
  513. return (time >= new_time_in_effect) ? &new_offset : current_offset;
  514. };
  515. for (size_t index = 0; (index < dst_rules.size()) && (!standard_offset || !daylight_offset); ++index) {
  516. auto const& dst_rule = dst_rules[index];
  517. if (last_offset == nullptr)
  518. last_offset = &dst_rule;
  519. else if (dst_rule.time_in_effect(dst_rule.year_to) > last_offset->time_in_effect(last_offset->year_to))
  520. last_offset = &dst_rule;
  521. if ((time < dst_rule.year_from) || (time >= dst_rule.year_to))
  522. continue;
  523. if (dst_rule.offset == 0)
  524. standard_offset = preferred_rule(standard_offset, dst_rule);
  525. else
  526. daylight_offset = preferred_rule(daylight_offset, dst_rule);
  527. }
  528. // If there isn't a standard or daylight rule in effect, fall back to the last rule given in the TZDB.
  529. if (!standard_offset) {
  530. VERIFY(last_offset != nullptr);
  531. standard_offset = last_offset;
  532. }
  533. return { standard_offset, daylight_offset ? daylight_offset : standard_offset };
  534. }
  535. static Offset get_active_dst_offset(TimeZoneOffset const& time_zone_offset, AK::Time time)
  536. {
  537. auto offsets = find_dst_offsets(time_zone_offset, time);
  538. if (offsets[0] == offsets[1])
  539. return { offsets[0]->offset, InDST::No };
  540. auto standard_time_in_effect = offsets[0]->time_in_effect(time);
  541. auto daylight_time_in_effect = offsets[1]->time_in_effect(time);
  542. if (daylight_time_in_effect < standard_time_in_effect) {
  543. if ((time < daylight_time_in_effect) || (time >= standard_time_in_effect))
  544. return { offsets[0]->offset, InDST::No };
  545. } else {
  546. if ((time >= standard_time_in_effect) && (time < daylight_time_in_effect))
  547. return { offsets[0]->offset, InDST::No };
  548. }
  549. return { offsets[1]->offset, InDST::Yes };
  550. }
  551. static TimeZoneOffset const& find_time_zone_offset(TimeZone time_zone, AK::Time time)
  552. {
  553. auto const& time_zone_offsets = s_time_zone_offsets[to_underlying(time_zone)];
  554. size_t index = 0;
  555. for (; index < time_zone_offsets.size(); ++index) {
  556. auto const& time_zone_offset = time_zone_offsets[index];
  557. if (!time_zone_offset.has_until || (time_zone_offset.until.time_since_epoch() > time))
  558. break;
  559. }
  560. VERIFY(index < time_zone_offsets.size());
  561. return time_zone_offsets[index];
  562. }
  563. Optional<Offset> get_time_zone_offset(TimeZone time_zone, AK::Time time)
  564. {
  565. auto const& time_zone_offset = find_time_zone_offset(time_zone, time);
  566. Offset dst_offset {};
  567. if (time_zone_offset.dst_rule != -1) {
  568. dst_offset = get_active_dst_offset(time_zone_offset, time);
  569. } else {
  570. auto in_dst = time_zone_offset.dst_offset == 0 ? InDST::No : InDST::Yes;
  571. dst_offset = { time_zone_offset.dst_offset, in_dst };
  572. }
  573. dst_offset.seconds += time_zone_offset.offset;
  574. return dst_offset;
  575. }
  576. Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(TimeZone time_zone, AK::Time time)
  577. {
  578. auto const& time_zone_offset = find_time_zone_offset(time_zone, time);
  579. Array<NamedOffset, 2> named_offsets;
  580. auto format_name = [](auto format, auto offset) -> DeprecatedString {
  581. if (offset == 0)
  582. return decode_string(format).replace("{}"sv, ""sv, ReplaceMode::FirstOnly);
  583. return DeprecatedString::formatted(decode_string(format), decode_string(offset));
  584. };
  585. auto set_named_offset = [&](auto& named_offset, auto dst_offset, auto in_dst, auto format, auto offset) {
  586. named_offset.seconds = time_zone_offset.offset + dst_offset;
  587. named_offset.in_dst = in_dst;
  588. named_offset.name = format_name(format, offset);
  589. };
  590. if (time_zone_offset.dst_rule != -1) {
  591. auto offsets = find_dst_offsets(time_zone_offset, time);
  592. auto in_dst = offsets[1]->offset == 0 ? InDST::No : InDST::Yes;
  593. set_named_offset(named_offsets[0], offsets[0]->offset, InDST::No, time_zone_offset.standard_format, offsets[0]->format);
  594. set_named_offset(named_offsets[1], offsets[1]->offset, in_dst, time_zone_offset.daylight_format, offsets[1]->format);
  595. } else {
  596. auto in_dst = time_zone_offset.dst_offset == 0 ? InDST::No : InDST::Yes;
  597. set_named_offset(named_offsets[0], time_zone_offset.dst_offset, in_dst, time_zone_offset.standard_format, 0);
  598. set_named_offset(named_offsets[1], time_zone_offset.dst_offset, in_dst, time_zone_offset.daylight_format, 0);
  599. }
  600. return named_offsets;
  601. }
  602. Optional<Location> get_time_zone_location(TimeZone time_zone)
  603. {
  604. auto is_valid_coordinate = [](auto const& coordinate) {
  605. return (coordinate.degrees != 0) || (coordinate.minutes != 0) || (coordinate.seconds != 0);
  606. };
  607. auto const& location = s_time_zone_locations[to_underlying(time_zone)];
  608. if (is_valid_coordinate(location.latitude) && is_valid_coordinate(location.longitude))
  609. return location;
  610. return {};
  611. }
  612. Vector<StringView> time_zones_in_region(StringView region)
  613. {
  614. auto region_value = region_from_string(region);
  615. if (!region_value.has_value())
  616. return {};
  617. auto region_index = to_underlying(*region_value);
  618. auto const& regional_time_zones = s_regional_time_zones[region_index];
  619. Vector<StringView> time_zones;
  620. time_zones.ensure_capacity(regional_time_zones.size());
  621. for (auto time_zone : regional_time_zones)
  622. time_zones.unchecked_append(decode_string(time_zone));
  623. return time_zones;
  624. }
  625. )~~~");
  626. generate_available_values(generator, "all_time_zones"sv, time_zone_data.time_zone_names);
  627. generator.append(R"~~~(
  628. }
  629. )~~~");
  630. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  631. return {};
  632. }
  633. ErrorOr<int> serenity_main(Main::Arguments arguments)
  634. {
  635. StringView generated_header_path;
  636. StringView generated_implementation_path;
  637. StringView time_zone_coordinates_path;
  638. Vector<StringView> time_zone_paths;
  639. Core::ArgsParser args_parser;
  640. args_parser.add_option(generated_header_path, "Path to the time zone data header file to generate", "generated-header-path", 'h', "generated-header-path");
  641. args_parser.add_option(generated_implementation_path, "Path to the time zone data implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  642. args_parser.add_option(time_zone_coordinates_path, "Path to the time zone data coordinates file", "time-zone-coordinates-path", 'z', "time-zone-coordinates-path");
  643. args_parser.add_positional_argument(time_zone_paths, "Paths to the time zone database files", "time-zone-paths");
  644. args_parser.parse(arguments);
  645. auto generated_header_file = TRY(open_file(generated_header_path, Core::File::OpenMode::Write));
  646. auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::File::OpenMode::Write));
  647. auto time_zone_coordinates_file = TRY(open_file(time_zone_coordinates_path, Core::File::OpenMode::Read));
  648. TimeZoneData time_zone_data {};
  649. for (auto time_zone_path : time_zone_paths)
  650. TRY(parse_time_zones(time_zone_path, time_zone_data));
  651. TRY(parse_time_zone_coordinates(*time_zone_coordinates_file, time_zone_data));
  652. TRY(generate_time_zone_data_header(*generated_header_file, time_zone_data));
  653. TRY(generate_time_zone_data_implementation(*generated_implementation_file, time_zone_data));
  654. return 0;
  655. }