diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-19 18:32:33 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-22 18:41:54 -0800 |
commit | cef5c331fe6ab827e71aed1e4e0387983083c88e (patch) | |
tree | 47c71da0e81f25678640032d5708212ef022072e /src/mbgl/util/chrono.cpp | |
parent | 5cd123422ed026912c53c44a393f141f990a09df (diff) | |
download | qtlocation-mapboxgl-cef5c331fe6ab827e71aed1e4e0387983083c88e.tar.gz |
[core] Merge rfc1123, iso8601, and parse_date into chrono.hpp and fix their API
Diffstat (limited to 'src/mbgl/util/chrono.cpp')
-rw-r--r-- | src/mbgl/util/chrono.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/mbgl/util/chrono.cpp b/src/mbgl/util/chrono.cpp index ca8ebca953..f0b3f88192 100644 --- a/src/mbgl/util/chrono.cpp +++ b/src/mbgl/util/chrono.cpp @@ -1,5 +1,9 @@ #include <mbgl/util/chrono.hpp> +#include <parsedate/parsedate.h> + +#include <cstdio> + namespace mbgl { template Duration toDuration<Clock, Duration>(TimePoint); @@ -17,4 +21,35 @@ template Milliseconds asMilliseconds<Seconds>(Seconds); template Milliseconds toMilliseconds<Clock, Duration>(TimePoint); template Milliseconds toMilliseconds<SystemClock, SystemDuration>(SystemTimePoint); +namespace util { + +static const char *week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; +static const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + +std::string rfc1123(SystemTimePoint timePoint) { + std::time_t time = SystemClock::to_time_t(timePoint); + std::tm info; + gmtime_r(&time, &info); + char buffer[30]; + snprintf(buffer, 30, "%s, %02d %s %4d %02d:%02d:%02d GMT", week[info.tm_wday], info.tm_mday, + months[info.tm_mon], 1900 + info.tm_year, info.tm_hour, info.tm_min, info.tm_sec); + return buffer; +} + +std::string iso8601(SystemTimePoint timePoint) { + std::time_t time = SystemClock::to_time_t(timePoint); + std::tm info; + gmtime_r(&time, &info); + char buffer[30]; + std::strftime(buffer, sizeof(buffer), "%F %T", &info); + return buffer; +} + +SystemTimePoint parseTimePoint(const char * timePoint) { + return SystemClock::from_time_t(parse_date(timePoint)); +} + +} // namespace util + } // namespace mbgl |