time/ISO8601: move code to ParseTimeOfDay()
This commit is contained in:
parent
7770298a65
commit
2bc127bb43
@ -108,6 +108,30 @@ ParseTimeZoneOffset(const char *&s)
|
|||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
ParseTimeOfDay(const char *s, struct tm &tm,
|
||||||
|
std::chrono::system_clock::duration &precision) noexcept
|
||||||
|
{
|
||||||
|
const char *end;
|
||||||
|
|
||||||
|
if ((end = strptime(s, "%T", &tm)) != nullptr)
|
||||||
|
precision = std::chrono::seconds(1);
|
||||||
|
else if ((end = strptime(s, "%H%M%S", &tm)) != nullptr)
|
||||||
|
/* no field separators */
|
||||||
|
precision = std::chrono::seconds(1);
|
||||||
|
else if ((end = strptime(s, "%H%M", &tm)) != nullptr)
|
||||||
|
/* no field separators */
|
||||||
|
precision = std::chrono::minutes(1);
|
||||||
|
else if ((end = strptime(s, "%H:%M", &tm)) != nullptr)
|
||||||
|
precision = std::chrono::minutes(1);
|
||||||
|
else if ((end = strptime(s, "%H", &tm)) != nullptr)
|
||||||
|
precision = std::chrono::hours(1);
|
||||||
|
else
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
|
||||||
std::pair<std::chrono::system_clock::time_point,
|
std::pair<std::chrono::system_clock::time_point,
|
||||||
std::chrono::system_clock::duration>
|
std::chrono::system_clock::duration>
|
||||||
ParseISO8601(const char *s)
|
ParseISO8601(const char *s)
|
||||||
@ -138,22 +162,9 @@ ParseISO8601(const char *s)
|
|||||||
if (*s == 'T') {
|
if (*s == 'T') {
|
||||||
++s;
|
++s;
|
||||||
|
|
||||||
if ((end = strptime(s, "%T", &tm)) != nullptr)
|
s = ParseTimeOfDay(s, tm, precision);
|
||||||
precision = std::chrono::seconds(1);
|
if (s == nullptr)
|
||||||
else if ((end = strptime(s, "%H%M%S", &tm)) != nullptr)
|
|
||||||
/* no field separators */
|
|
||||||
precision = std::chrono::seconds(1);
|
|
||||||
else if ((end = strptime(s, "%H%M", &tm)) != nullptr)
|
|
||||||
/* no field separators */
|
|
||||||
precision = std::chrono::minutes(1);
|
|
||||||
else if ((end = strptime(s, "%H:%M", &tm)) != nullptr)
|
|
||||||
precision = std::chrono::minutes(1);
|
|
||||||
else if ((end = strptime(s, "%H", &tm)) != nullptr)
|
|
||||||
precision = std::chrono::hours(1);
|
|
||||||
else
|
|
||||||
throw std::runtime_error("Failed to parse time of day");
|
throw std::runtime_error("Failed to parse time of day");
|
||||||
|
|
||||||
s = end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto tp = TimeGm(tm);
|
auto tp = TimeGm(tm);
|
||||||
|
Loading…
Reference in New Issue
Block a user