absl::ParseTime

Parses an input string according to the provided format string and returns the corresponding absl::Time. Uses strftime()-like formatting options, with the same extensions as FormatTime(), but with the exceptions that %E#S is interpreted as %ES, and %E#f as %Ef. %Ez and %E*z also accept the same inputs, which (along with %z) includes 'z' and 'Z' as synonyms for +00:00. %ET accepts either 'T' or 't'.

Synopsis

Declared in <absl/time/time.h>

bool
ParseTime(
    std::string_view format,
    std::string_view input,
    Time* time,
    std::string* err);

Description

%Y consumes as many numeric characters as it can, so the matching data should always be terminated with a non-numeric. %E4Y always consumes exactly four characters, including any sign.

Unspecified fields are taken from the default date and time of ...

"1970-01-01 00:00:00.0 +0000"

For example, parsing a string of "15:45" (%H:%M) will return an absl::Time that represents "1970-01-01 15:45:00.0 +0000".

Note that since ParseTime() returns time instants, it makes the most sense to parse fully-specified date/time strings that include a UTC offset (%z, %Ez, or %E*z).

Note also that absl::ParseTime() only heeds the fields year, month, day, hour, minute, (fractional) second, and UTC offset. Other fields, like weekday (%a or %A), while parsed for syntactic validity, are ignored in the conversion.

Date and time fields that are out-of-range will be treated as errors rather than normalizing them like absl::CivilSecond does. For example, it is an error to parse the date "Oct 32, 2013" because 32 is out of range.

A leap second of ":60" is normalized to ":00" of the following minute with fractional seconds discarded. The following table shows how the given seconds and subseconds will be parsed:

"59.x" -> 59.x // exact "60.x" -> 00.0 // normalized "00.x" -> 00.x // exact

Errors are indicated by returning false and assigning an error message to the "err" out param if it is non-null.

Note: If the input string is exactly "infinite-future", the returned absl::Time will be absl::InfiniteFuture() and true will be returned. If the input string is "infinite-past", the returned absl::Time will be absl::InfinitePast() and true will be returned.

Return Value

true upon successful parsing.

Parameters

NameDescription
formatThe strftime()-like format string.
inputThe string to parse.
timeOutput parameter receiving the parsed time.
errOutput parameter receiving an error message on failure.