[core] Merge rfc1123, iso8601, and parse_date into chrono.hpp and fix their API

This commit is contained in:
John Firebaugh
2016-01-19 18:32:33 -08:00
parent 5cd123422e
commit cef5c331fe
11 changed files with 58 additions and 71 deletions

View File

@@ -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