mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 20:14:23 +08:00
Various cleanups in //absl/time/format.cc
* Remove old workaround for lack of constexpr * Simplify whitespace stripping * Remove old ParseFlag/UnparseFlag (use AbslParseFlag/AbslUnparseFlag) PiperOrigin-RevId: 921784357 Change-Id: I87037954a4d261f76e95b45c81b2ee47f5a14ca9
This commit is contained in:
committed by
Copybara-Service
parent
147d631c21
commit
01c2bd4a02
@@ -17,6 +17,7 @@
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
#include "absl/strings/ascii.h"
|
||||
#include "absl/strings/match.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -36,8 +37,8 @@ ABSL_DLL extern const char RFC1123_no_wday[] = "%d %b %E4Y %H:%M:%S %z";
|
||||
|
||||
namespace {
|
||||
|
||||
const char kInfiniteFutureStr[] = "infinite-future";
|
||||
const char kInfinitePastStr[] = "infinite-past";
|
||||
constexpr absl::string_view kInfiniteFutureStr = "infinite-future";
|
||||
constexpr absl::string_view kInfinitePastStr = "infinite-past";
|
||||
|
||||
struct cctz_parts {
|
||||
cctz::time_point<cctz::seconds> sec;
|
||||
@@ -99,23 +100,19 @@ bool ParseTime(absl::string_view format, absl::string_view input,
|
||||
// the fields with respect to the given TimeZone.
|
||||
bool ParseTime(absl::string_view format, absl::string_view input,
|
||||
absl::TimeZone tz, absl::Time* time, std::string* err) {
|
||||
// Portable toolchains means we don't get nice constexpr here.
|
||||
struct Literal {
|
||||
const char* name;
|
||||
size_t size;
|
||||
static constexpr struct Literal {
|
||||
absl::string_view name;
|
||||
absl::Time value;
|
||||
};
|
||||
static Literal literals[] = {
|
||||
{kInfiniteFutureStr, strlen(kInfiniteFutureStr), InfiniteFuture()},
|
||||
{kInfinitePastStr, strlen(kInfinitePastStr), InfinitePast()},
|
||||
} kLiterals[] = {
|
||||
{kInfiniteFutureStr, InfiniteFuture()},
|
||||
{kInfinitePastStr, InfinitePast()},
|
||||
};
|
||||
input = StripLeadingAsciiWhitespace(input);
|
||||
for (const auto& lit : literals) {
|
||||
if (absl::StartsWith(input, absl::string_view(lit.name, lit.size))) {
|
||||
absl::string_view tail = input;
|
||||
tail.remove_prefix(lit.size);
|
||||
tail = StripLeadingAsciiWhitespace(tail);
|
||||
if (tail.empty()) {
|
||||
for (const auto& lit : kLiterals) {
|
||||
if (absl::StartsWith(input, lit.name)) {
|
||||
absl::string_view tail = input.substr(lit.name.size());
|
||||
// The trailing portion must be empty or whitespace.
|
||||
if (StripLeadingAsciiWhitespace(tail).empty()) {
|
||||
*time = lit.value;
|
||||
return true;
|
||||
}
|
||||
@@ -143,13 +140,6 @@ bool AbslParseFlag(absl::string_view text, absl::Time* t, std::string* error) {
|
||||
std::string AbslUnparseFlag(absl::Time t) {
|
||||
return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
|
||||
}
|
||||
bool ParseFlag(const std::string& text, absl::Time* t, std::string* error) {
|
||||
return absl::ParseTime(RFC3339_full, text, absl::UTCTimeZone(), t, error);
|
||||
}
|
||||
|
||||
std::string UnparseFlag(absl::Time t) {
|
||||
return absl::FormatTime(RFC3339_full, t, absl::UTCTimeZone());
|
||||
}
|
||||
|
||||
ABSL_NAMESPACE_END
|
||||
} // namespace absl
|
||||
|
||||
Reference in New Issue
Block a user