diff --git a/libc/src/time/time_zone_posix.h b/libc/src/time/time_zone_posix.h --- a/libc/src/time/time_zone_posix.h +++ b/libc/src/time/time_zone_posix.h @@ -11,8 +11,8 @@ #include // int8_t, int16_t and int32_t -#include "src/__support/CPP/Optional.h" #include "src/__support/CPP/StringView.h" +#include "src/__support/CPP/optional.h" #include // int8_t, int16_t and int32_t @@ -91,7 +91,7 @@ // filling in any missing values (DST offset, or start/end transition times) // with the standard-defined defaults. Returns false if the specification // could not be parsed (although some fields of *res may have been altered). - static cpp::Optional + static cpp::optional ParsePosixSpec(const cpp::StringView spec); cpp::StringView m_spec; @@ -114,13 +114,13 @@ bool UpdateDstEnd(); bool SpecHasData(); - cpp::Optional ParseInt(int min, int max); - cpp::Optional ParseAbbr(); - cpp::Optional ParseOffset(int min_hour, int max_hour, int sign); - cpp::Optional ParseMonthWeekWeekday(); - cpp::Optional ParseNonLeapDay(); - cpp::Optional ParseLeapDay(); - cpp::Optional ParseDateTime(); + cpp::optional ParseInt(int min, int max); + cpp::optional ParseAbbr(); + cpp::optional ParseOffset(int min_hour, int max_hour, int sign); + cpp::optional ParseMonthWeekWeekday(); + cpp::optional ParseNonLeapDay(); + cpp::optional ParseLeapDay(); + cpp::optional ParseDateTime(); }; } // namespace time_zone_posix diff --git a/libc/src/time/time_zone_posix.cpp b/libc/src/time/time_zone_posix.cpp --- a/libc/src/time/time_zone_posix.cpp +++ b/libc/src/time/time_zone_posix.cpp @@ -14,14 +14,14 @@ // required unsigned values from 0 through 24. static constexpr int MAX_HOURS_IN_TRANSITION_TIMES = 167; -cpp::Optional PosixTimeZone::ParseInt(int min, int max) { +cpp::optional PosixTimeZone::ParseInt(int min, int max) { if (m_spec.size() == 0) - return cpp::Nullopt; + return cpp::nullopt; char *pEnd; int value = strtol(m_spec.data(), &pEnd, 10); // NOLINT(runtime/deprecated_fn) if (value < min || value > max) - return cpp::Nullopt; + return cpp::nullopt; // Delete the number from the input string. size_t len = pEnd - m_spec.data(); @@ -30,17 +30,17 @@ } // abbr = <.*?> | [^-+,\d]{3,} -cpp::Optional PosixTimeZone::ParseAbbr() { +cpp::optional PosixTimeZone::ParseAbbr() { if (m_spec.size() == 0) - return cpp::Nullopt; + return cpp::nullopt; // Handle special zoneinfo <...> form. - cpp::Optional abbr; + cpp::optional abbr; if (m_spec.starts_with('<')) { m_spec.remove_prefix(1); const auto pos = m_spec.find_first_of('>'); if (pos == cpp::StringView::npos) - return cpp::Nullopt; + return cpp::nullopt; abbr = m_spec.substr(0, pos); // Delete the data up to and including '>' character. m_spec.remove_prefix(pos + 1); @@ -57,17 +57,17 @@ len++; } if (len < 3) - return cpp::Nullopt; + return cpp::nullopt; abbr = m_spec.substr(0, len); m_spec.remove_prefix(len); return abbr; } // offset = [+|-]hh[:mm[:ss]] (aggregated into single seconds value) -cpp::Optional PosixTimeZone::ParseOffset(int min_hour, int max_hour, +cpp::optional PosixTimeZone::ParseOffset(int min_hour, int max_hour, int sign) { if (m_spec.size() == 0) - return cpp::Nullopt; + return cpp::nullopt; // Handle [+|-]. if (m_spec.starts_with('+') || m_spec.starts_with('-')) { @@ -81,9 +81,9 @@ int seconds = 0; // Parse hours - hh - cpp::Optional result = ParseInt(min_hour, max_hour); + cpp::optional result = ParseInt(min_hour, max_hour); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; hours = result.value(); // Check for optional minutes. @@ -94,7 +94,7 @@ // Parse minutes. result = ParseInt(0, 59); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; minutes = result.value(); // Check for optional seconds. @@ -105,7 +105,7 @@ // Parse seconds. result = ParseInt(0, 59); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; seconds = result.value(); } } @@ -115,41 +115,41 @@ // (M) the Nth weekday of a month (e.g., the 2nd Sunday in March). // Mm.w.d -cpp::Optional PosixTimeZone::ParseMonthWeekWeekday() { +cpp::optional PosixTimeZone::ParseMonthWeekWeekday() { // Parse month. - cpp::Optional result = ParseInt(1, 12); + cpp::optional result = ParseInt(1, 12); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; int month = result.value(); // Error if there is no dot. if (!m_spec.starts_with('.')) - return cpp::Nullopt; + return cpp::nullopt; m_spec.remove_prefix(1); // Error if there is no week. if (m_spec.size() == 0) - return cpp::Nullopt; + return cpp::nullopt; // Parse week. result = ParseInt(1, 5); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; int week = result.value(); // Error if there is no dot. if (!m_spec.starts_with('.')) - return cpp::Nullopt; + return cpp::nullopt; m_spec.remove_prefix(1); // Error if there is no weekday. if (m_spec.size() == 0) - return cpp::Nullopt; + return cpp::nullopt; // Parse Weekday. result = ParseInt(0, 6); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; int weekday = result.value(); PosixTransition posixTransition; @@ -162,10 +162,10 @@ // Parse Jn // (J) the Nth day of the year (1 <= N <= 365), excluding leap days. -cpp::Optional PosixTimeZone::ParseNonLeapDay() { - const cpp::Optional result = ParseInt(1, 365); +cpp::optional PosixTimeZone::ParseNonLeapDay() { + const cpp::optional result = ParseInt(1, 365); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; PosixTransition posixTransition; posixTransition.date.fmt = PosixTransition::J; posixTransition.date.j.day = static_cast(result.value()); @@ -174,10 +174,10 @@ // Parse n // (N) the Nth day of the year (0 <= N <= 365), including leap days -cpp::Optional PosixTimeZone::ParseLeapDay() { - const cpp::Optional result = ParseInt(0, 365); +cpp::optional PosixTimeZone::ParseLeapDay() { + const cpp::optional result = ParseInt(0, 365); if (!result.has_value()) - return cpp::Nullopt; + return cpp::nullopt; PosixTransition posixTransition; posixTransition.date.fmt = PosixTransition::N; posixTransition.date.n.day = static_cast(result.value()); @@ -185,12 +185,12 @@ } // datetime = ( Jn | n | Mm.w.d ) [ / offset ] -cpp::Optional PosixTimeZone::ParseDateTime() { +cpp::optional PosixTimeZone::ParseDateTime() { PosixTransition posixTransition; if (m_spec.starts_with(',')) { m_spec.remove_prefix(1); - cpp::Optional optionalPosixTransition; + cpp::optional optionalPosixTransition; if (m_spec.starts_with('M')) { m_spec.remove_prefix(1); optionalPosixTransition = ParseMonthWeekWeekday(); // Mm.w.d @@ -208,10 +208,10 @@ posixTransition.time.offset = 2 * 60 * 60; // default offset is 02:00:00 if (m_spec.starts_with('/')) { m_spec.remove_prefix(1); - const cpp::Optional offset = ParseOffset( + const cpp::optional offset = ParseOffset( -MAX_HOURS_IN_TRANSITION_TIMES, MAX_HOURS_IN_TRANSITION_TIMES, 1); if (!offset.has_value()) - return cpp::Nullopt; + return cpp::nullopt; posixTransition.time.offset = offset.value(); } return posixTransition; @@ -234,7 +234,7 @@ } bool PosixTimeZone::UpdateStdOffset() { - const cpp::Optional offset = ParseOffset(0, 24, -1); + const cpp::optional offset = ParseOffset(0, 24, -1); if (!offset.has_value()) return false; m_std_offset = offset.value(); @@ -244,7 +244,7 @@ bool PosixTimeZone::UpdateDstOffset() { m_dst_offset = m_std_offset + (60 * 60); // default if (!m_spec.starts_with(',')) { - const cpp::Optional offset = ParseOffset(0, 24, -1); + const cpp::optional offset = ParseOffset(0, 24, -1); if (!offset.has_value()) return false; m_dst_offset = offset.value(); @@ -275,28 +275,28 @@ } // spec = std offset [ dst [ offset ] , datetime , datetime ] -cpp::Optional +cpp::optional PosixTimeZone::ParsePosixSpec(const cpp::StringView spec) { if (spec.starts_with(':')) - return cpp::Nullopt; + return cpp::nullopt; PosixTimeZone res(spec); if (!res.UpdateStdAbbr()) - return cpp::Nullopt; + return cpp::nullopt; if (!res.UpdateStdOffset()) - return cpp::Nullopt; + return cpp::nullopt; if (res.m_spec.size() == 0) return res; if (!res.UpdateDstAbbr()) - return cpp::Nullopt; + return cpp::nullopt; if (!res.UpdateDstOffset()) - return cpp::Nullopt; + return cpp::nullopt; if (!res.UpdateDstStart()) - return cpp::Nullopt; + return cpp::nullopt; if (!res.UpdateDstEnd()) - return cpp::Nullopt; + return cpp::nullopt; if (res.m_spec.size() != 0) - return cpp::Nullopt; + return cpp::nullopt; return res; } diff --git a/libc/test/src/time/time_zone_posix_test.cpp b/libc/test/src/time/time_zone_posix_test.cpp --- a/libc/test/src/time/time_zone_posix_test.cpp +++ b/libc/test/src/time/time_zone_posix_test.cpp @@ -247,7 +247,7 @@ TEST(LlvmLibcParsePosixSpec, ValidTestAndVerify) { __llvm_libc::cpp::StringView timezone_spec("PST8PDT,M3.2.0,M11.1.0"); // TODO(rtenneti): Use a special matcher to verify the data. - __llvm_libc::cpp::Optional<__llvm_libc::time_zone_posix::PosixTimeZone> + __llvm_libc::cpp::optional<__llvm_libc::time_zone_posix::PosixTimeZone> posix_result = __llvm_libc::time_zone_posix::PosixTimeZone::ParsePosixSpec( timezone_spec);