Commit 1a08bdf1 authored by Max Kellermann's avatar Max Kellermann

time/ISO8601: ParseISO8601() returns precision

parent 48b122f2
...@@ -113,7 +113,7 @@ ParseTimeStamp(const char *s) ...@@ -113,7 +113,7 @@ ParseTimeStamp(const char *s)
return std::chrono::system_clock::from_time_t((time_t)value); return std::chrono::system_clock::from_time_t((time_t)value);
/* try ISO 8601 */ /* try ISO 8601 */
return ParseISO8601(s); return ParseISO8601(s).first;
} }
static constexpr bool static constexpr bool
......
...@@ -58,7 +58,8 @@ FormatISO8601(std::chrono::system_clock::time_point tp) ...@@ -58,7 +58,8 @@ FormatISO8601(std::chrono::system_clock::time_point tp)
return FormatISO8601(GmTime(tp)); return FormatISO8601(GmTime(tp));
} }
std::chrono::system_clock::time_point std::pair<std::chrono::system_clock::time_point,
std::chrono::system_clock::duration>
ParseISO8601(const char *s) ParseISO8601(const char *s)
{ {
assert(s != nullptr); assert(s != nullptr);
...@@ -73,6 +74,10 @@ ParseISO8601(const char *s) ...@@ -73,6 +74,10 @@ ParseISO8601(const char *s)
if (end == nullptr || *end != 0) if (end == nullptr || *end != 0)
throw std::runtime_error("Failed to parse time stamp"); throw std::runtime_error("Failed to parse time stamp");
return TimeGm(tm); std::chrono::system_clock::duration precision = std::chrono::seconds(1);
auto tp = TimeGm(tm);
return std::make_pair(tp, precision);
#endif /* !_WIN32 */ #endif /* !_WIN32 */
} }
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "util/Compiler.h" #include "util/Compiler.h"
#include <chrono> #include <chrono>
#include <utility>
#include <stddef.h> #include <stddef.h>
...@@ -50,7 +51,16 @@ gcc_pure ...@@ -50,7 +51,16 @@ gcc_pure
StringBuffer<64> StringBuffer<64>
FormatISO8601(std::chrono::system_clock::time_point tp); FormatISO8601(std::chrono::system_clock::time_point tp);
std::chrono::system_clock::time_point /**
* Parse a time stamp in ISO8601 format.
*
* Throws on error.
*
* @return a pair consisting of the time point and the specified
* precision; e.g. for a date, the second value is "one day"
*/
std::pair<std::chrono::system_clock::time_point,
std::chrono::system_clock::duration>
ParseISO8601(const char *s); ParseISO8601(const char *s);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment