[libcxx-commits] [libcxx] [libc++][chrono] Loads tzdata.zi in tzdb. (PR #74928)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jan 30 09:29:40 PST 2024
================
@@ -96,6 +121,369 @@ static void __matches(istream& __input, string_view __expected) {
}
}
+[[nodiscard]] static int64_t __parse_integral(istream& __input, bool __leading_zero_allowed) {
+ int64_t __result = __input.get();
+ if (__leading_zero_allowed) {
+ if (__result < '0' || __result > '9')
+ std::__throw_runtime_error("corrupt tzdb: expected a digit");
+ } else {
+ if (__result < '1' || __result > '9')
+ std::__throw_runtime_error("corrupt tzdb: expected a non-zero digit");
+ }
+ __result -= '0';
+ while (true) {
+ if (__input.peek() < '0' || __input.peek() > '9')
+ return __result;
+
+ // In order to avoid possible overflows we limit the accepted range.
+ // Most values parsed are expected to be very small:
+ // - 8784 hours in a year
+ // - 31 days in a month
+ // - year no real maximum, these values are expected to be less than
+ // the range of the year type.
+ //
+ // However the leapseconds use a seconds after epoch value. Using an
+ // int would run into an overflow in 2038. By using a 64-bit value
+ // the range is large enough for the bilions of years. Limiting that
+ // range slightly to make the code easier is not an issue.
+ if (__result > (std::numeric_limits<int64_t>::max() / 16))
+ std::__throw_runtime_error("corrupt tzdb: integral too large");
+
+ __result *= 10;
+ __result += __input.get() - '0';
+ }
+}
+
+//===----------------------------------------------------------------------===//
+// Calendar
+//===----------------------------------------------------------------------===//
+
+[[nodiscard]] static day __parse_day(istream& __input) {
+ unsigned __result = chrono::__parse_integral(__input, false);
+ if (__result > 31)
+ std::__throw_runtime_error("corrupt tzdb day: value too large");
+ return day{__result};
+}
+
+[[nodiscard]] static weekday __parse_weekday(istream& __input) {
+ // TZDB allows the shortest unique name.
+ switch (std::tolower(__input.get())) {
+ case 'f':
+ chrono::__skip(__input, "riday");
+ return Friday;
+
+ case 'm':
+ chrono::__skip(__input, "onday");
+ return Monday;
+
+ case 's':
+ switch (std::tolower(__input.get())) {
+ case 'a':
+ chrono::__skip(__input, "turday");
+ return Saturday;
+
+ case 'u':
+ chrono::__skip(__input, "unday");
+ return Sunday;
+ }
+ break;
+
+ case 't':
+ switch (std::tolower(__input.get())) {
+ case 'h':
+ chrono::__skip(__input, "ursday");
+ return Thursday;
+
+ case 'u':
+ chrono::__skip(__input, "esday");
+ return Tuesday;
+ }
+ break;
+ case 'w':
+ chrono::__skip(__input, "ednesday");
+ return Wednesday;
+ }
+
+ std::__throw_runtime_error("corrupt tzdb weekday: invalid name");
+}
+
+[[nodiscard]] static month __parse_month(istream& __input) {
+ // TZDB allows the shortest unique name.
+ switch (std::tolower(__input.get())) {
+ case 'a':
+ switch (std::tolower(__input.get())) {
+ case 'p':
+ chrono::__skip(__input, "ril");
+ return April;
+
+ case 'u':
+ chrono::__skip(__input, "gust");
+ return August;
+ }
+ break;
+
+ case 'd':
+ chrono::__skip(__input, "ecember");
+ return December;
+
+ case 'f':
+ chrono::__skip(__input, "ebruary");
+ return February;
+
+ case 'j':
+ switch (std::tolower(__input.get())) {
+ case 'a':
+ chrono::__skip(__input, "nuary");
+ return January;
+
+ case 'u':
+ switch (std::tolower(__input.get())) {
+ case 'n':
+ chrono::__skip(__input, 'e');
+ return June;
+
+ case 'l':
+ chrono::__skip(__input, 'y');
+ return July;
+ }
+ }
+ break;
+
+ case 'm':
+ if (std::tolower(__input.get()) == 'a')
+ switch (std::tolower(__input.get())) {
+ case 'y':
+ return May;
+
+ case 'r':
+ chrono::__skip(__input, "ch");
+ return March;
+ }
+ break;
+
+ case 'n':
+ chrono::__skip(__input, "ovember");
+ return November;
+
+ case 'o':
+ chrono::__skip(__input, "ctober");
+ return October;
+
+ case 's':
+ chrono::__skip(__input, "eptember");
+ return September;
+ }
+ std::__throw_runtime_error("corrupt tzdb month: invalid name");
+}
+
+[[nodiscard]] static year __parse_year_value(istream& __input) {
+ bool __negative = __input.peek() == '-';
+ if (__negative) [[unlikely]]
+ __input.get();
+
+ static_assert(year::min() == -year::max());
+ int __result = std::min<int64_t>(__parse_integral(__input, true), static_cast<int>(year::max()));
+
+ return year{__negative ? -__result : __result};
+}
+
+[[nodiscard]] static year __parse_year(istream& __input) {
+ if (std::tolower(__input.peek()) != 'm') [[likely]]
+ return chrono::__parse_year_value(__input);
+
+ __input.get();
+ switch (std::tolower(__input.peek())) {
+ case 'i':
+ __input.get();
+ chrono::__skip(__input, 'n');
+ [[fallthrough]];
+
+ case ' ':
+ // The m is minimum, even when that is ambigious.
+ return year::min();
+
+ case 'a':
+ __input.get();
+ chrono::__skip(__input, 'x');
+ return year::max();
+ }
+
+ std::__throw_runtime_error("corrupt tzdb year: expected 'min' or 'max'");
+}
+
+//===----------------------------------------------------------------------===//
+// TZDB fields
+//===----------------------------------------------------------------------===//
+
+[[nodiscard]] static year __parse_to(istream& __input, year __only) {
+ if (std::tolower(__input.peek()) != 'o')
+ return chrono::__parse_year(__input);
+
+ __input.get();
+ chrono::__skip(__input, "nly");
+ return __only;
+}
+
+[[nodiscard]] static __tz::__constrained_weekday::__comparison_t __parse_comparison(istream& __input) {
+ switch (__input.get()) {
+ case '>':
+ chrono::__matches(__input, '=');
+ return __tz::__constrained_weekday::__ge;
+
+ case '<':
+ chrono::__matches(__input, '=');
+ return __tz::__constrained_weekday::__le;
+ }
+ std::__throw_runtime_error("corrupt tzdb on: expected '>=' or '<='");
+}
+
+[[nodiscard]] static __tz::__on __parse_on(istream& __input) {
+ if (std::isdigit(__input.peek()))
+ return chrono::__parse_day(__input);
+
+ if (std::tolower(__input.peek()) == 'l') {
+ chrono::__matches(__input, "last");
+ return weekday_last(chrono::__parse_weekday(__input));
+ }
+
+ return __tz::__constrained_weekday{
+ chrono::__parse_weekday(__input), chrono::__parse_comparison(__input), chrono::__parse_day(__input)};
+}
+
+[[nodiscard]] static seconds __parse_duration(istream& __input) {
+ seconds __result{0};
+ int __c = __input.peek();
+ bool __negative = __c == '-';
+ if (__negative) {
+ __input.get();
+ // Negative is either a negative value or a single -.
+ // The latter means 0 and the parsing is complete.
+ if (!std::isdigit(__input.peek()))
+ return __result;
+ }
+
+ __result += hours(__parse_integral(__input, true));
+ if (__input.peek() != ':')
+ return __negative ? -__result : __result;
+
+ __input.get();
+ __result += minutes(__parse_integral(__input, true));
+ if (__input.peek() != ':')
+ return __negative ? -__result : __result;
+
+ __input.get();
+ __result += seconds(__parse_integral(__input, true));
+ if (__input.peek() != '.')
+ return __negative ? -__result : __result;
+
+ __input.get();
+ (void)__parse_integral(__input, true); // Truncate the digits.
+
+ return __negative ? -__result : __result;
+}
+
+[[nodiscard]] static __tz::__clock __parse_clock(istream& __input) {
+ switch (__input.get()) { // case sensitive
+ case 'w':
+ return __tz::__clock::__local;
+ case 's':
+ return __tz::__clock::__standard;
+
+ case 'u':
+ case 'g':
+ case 'z':
+ return __tz::__clock::__universal;
+ }
+
+ __input.unget();
+ return __tz::__clock::__local;
+}
+
+[[nodiscard]] static bool __parse_dst(istream& __input, seconds __offset) {
+ switch (__input.get()) { // case sensitive
+ case 's':
+ return false;
+
+ case 'd':
+ return true;
+ }
+
+ __input.unget();
+ return __offset != 0s;
+}
+
+[[nodiscard]] static __tz::__at __parse_at(istream& __input) {
+ return {__parse_duration(__input), __parse_clock(__input)};
+}
+
+[[nodiscard]] static __tz::__save __parse_save(istream& __input) {
+ seconds __time = chrono::__parse_duration(__input);
+ return {__time, chrono::__parse_dst(__input, __time)};
+}
+
+[[nodiscard]] static string __parse_letters(istream& __input) {
+ string __result = __parse_string(__input);
+ return __result != "-" ? __result : "";
----------------
ldionne wrote:
```suggestion
return __result != "-" ? __result : ""; // canonicalize "-" to "" since they are equivalent in the spec
```
https://github.com/llvm/llvm-project/pull/74928
More information about the libcxx-commits
mailing list