[core] Merge rfc1123, iso8601, and parse_date into chrono.hpp and fix their API
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#define MBGL_UTIL_CHRONO
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -42,6 +43,18 @@ Milliseconds toMilliseconds(std::chrono::time_point<_Clock, _Duration> time_poin
|
||||
return asMilliseconds(toDuration<_Clock, _Duration>(time_point));
|
||||
}
|
||||
|
||||
namespace util {
|
||||
|
||||
// Returns the RFC1123 formatted date. E.g. "Tue, 04 Nov 2014 02:13:24 GMT"
|
||||
std::string rfc1123(SystemTimePoint);
|
||||
|
||||
// YYYY-mm-dd HH:MM:SS e.g. "2015-11-26 16:11:23"
|
||||
std::string iso8601(SystemTimePoint);
|
||||
|
||||
SystemTimePoint parseTimePoint(const char *);
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef MBGL_UTIL_TIME
|
||||
#define MBGL_UTIL_TIME
|
||||
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
namespace util {
|
||||
|
||||
// Returns the RFC1123 formatted date. E.g. "Tue, 04 Nov 2014 02:13:24 GMT"
|
||||
std::string rfc1123(std::time_t time);
|
||||
|
||||
// YYYY-mm-dd HH:MM:SS e.g. "2015-11-26 16:11:23"
|
||||
std::string iso8601(std::time_t time);
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
#endif
|
||||
@@ -5,10 +5,8 @@
|
||||
#include "jni.hpp"
|
||||
|
||||
#include <mbgl/util/async_task.hpp>
|
||||
#include <mbgl/util/time.hpp>
|
||||
#include <mbgl/util/util.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
#include <mbgl/util/parsedate.h>
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
@@ -127,7 +125,7 @@ HTTPAndroidRequest::HTTPAndroidRequest(HTTPAndroidContext* context_, const std::
|
||||
if (existingResponse->etag) {
|
||||
etagStr = *existingResponse->etag;
|
||||
} else if (existingResponse->modified) {
|
||||
modifiedStr = util::rfc1123(SystemClock::to_time_t(*existingResponse->modified));
|
||||
modifiedStr = util::rfc1123(*existingResponse->modified);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,7 +195,7 @@ void HTTPAndroidRequest::onResponse(JNIEnv* env, int code, jstring /* message */
|
||||
}
|
||||
|
||||
if (modified != nullptr) {
|
||||
response->modified = SystemClock::from_time_t(parse_date(mbgl::android::std_string_from_jstring(env, modified).c_str()));
|
||||
response->modified = util::parseTimePoint(mbgl::android::std_string_from_jstring(env, modified).c_str());
|
||||
}
|
||||
|
||||
if (cacheControl != nullptr) {
|
||||
@@ -205,7 +203,7 @@ void HTTPAndroidRequest::onResponse(JNIEnv* env, int code, jstring /* message */
|
||||
}
|
||||
|
||||
if (expires != nullptr) {
|
||||
response->expires = SystemClock::from_time_t(parse_date(mbgl::android::std_string_from_jstring(env, expires).c_str()));
|
||||
response->expires = util::parseTimePoint(mbgl::android::std_string_from_jstring(env, expires).c_str());
|
||||
}
|
||||
|
||||
if (body != nullptr) {
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
#include <mbgl/storage/response.hpp>
|
||||
|
||||
#include <mbgl/util/async_task.hpp>
|
||||
#include <mbgl/util/time.hpp>
|
||||
#include <mbgl/util/parsedate.h>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
@@ -117,7 +115,7 @@ HTTPNSURLRequest::HTTPNSURLRequest(HTTPNSURLContext* context_,
|
||||
[req addValue:@((*existingResponse->etag).c_str())
|
||||
forHTTPHeaderField:@"If-None-Match"];
|
||||
} else if (existingResponse->modified) {
|
||||
[req addValue:@(util::rfc1123(SystemClock::to_time_t(*existingResponse->modified)).c_str())
|
||||
[req addValue:@(util::rfc1123(*existingResponse->modified).c_str())
|
||||
forHTTPHeaderField:@"If-Modified-Since"];
|
||||
}
|
||||
}
|
||||
@@ -217,12 +215,12 @@ void HTTPNSURLRequest::handleResult(NSData *data, NSURLResponse *res, NSError *e
|
||||
|
||||
NSString *expires = [headers objectForKey:@"Expires"];
|
||||
if (expires) {
|
||||
response->expires = SystemClock::from_time_t(parse_date([expires UTF8String]));
|
||||
response->expires = util::parseTimePoint([expires UTF8String]);
|
||||
}
|
||||
|
||||
NSString *last_modified = [headers objectForKey:@"Last-Modified"];
|
||||
if (last_modified) {
|
||||
response->modified = SystemClock::from_time_t(parse_date([last_modified UTF8String]));
|
||||
response->modified = util::parseTimePoint([last_modified UTF8String]);
|
||||
}
|
||||
|
||||
NSString *etag = [headers objectForKey:@"ETag"];
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
|
||||
#include <mbgl/util/time.hpp>
|
||||
#include <mbgl/util/util.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
@@ -368,7 +367,7 @@ HTTPCURLRequest::HTTPCURLRequest(HTTPCURLContext* context_, const std::string& u
|
||||
headers = curl_slist_append(headers, header.c_str());
|
||||
} else if (existingResponse->modified) {
|
||||
const std::string time =
|
||||
std::string("If-Modified-Since: ") + util::rfc1123(SystemClock::to_time_t(*existingResponse->modified));
|
||||
std::string("If-Modified-Since: ") + util::rfc1123(*existingResponse->modified);
|
||||
headers = curl_slist_append(headers, time.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <mbgl/renderer/debug_bucket.hpp>
|
||||
#include <mbgl/renderer/painter.hpp>
|
||||
#include <mbgl/shader/plain_shader.hpp>
|
||||
#include <mbgl/util/time.hpp>
|
||||
|
||||
#include <mbgl/platform/gl.hpp>
|
||||
|
||||
@@ -23,10 +22,10 @@ DebugBucket::DebugBucket(const TileID id, const TileData::State state_, optional
|
||||
}
|
||||
|
||||
if (debugMode & MapDebugOptions::Timestamps && modified && expires) {
|
||||
const std::string modifiedText = "modified: " + util::iso8601(SystemClock::to_time_t(*modified));
|
||||
const std::string modifiedText = "modified: " + util::iso8601(*modified);
|
||||
fontBuffer.addText(modifiedText.c_str(), 50, baseline, 5);
|
||||
|
||||
const std::string expiresText = "expires: " + util::iso8601(SystemClock::to_time_t(*expires));
|
||||
const std::string expiresText = "expires: " + util::iso8601(*expires);
|
||||
fontBuffer.addText(expiresText.c_str(), 50, baseline + 200, 5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
#include <mbgl/util/time.hpp>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
namespace mbgl {
|
||||
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(std::time_t time) {
|
||||
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(std::time_t time) {
|
||||
std::tm info;
|
||||
gmtime_r(&time, &info);
|
||||
char buffer[30];
|
||||
std::strftime(buffer, sizeof(buffer), "%F %T", &info);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
@@ -73,7 +73,7 @@
|
||||
|
||||
*/
|
||||
|
||||
#include <mbgl/util/parsedate.h>
|
||||
#include "parsedate.h"
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include <mbgl/storage/sqlite_cache.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
#include <mbgl/util/time.hpp>
|
||||
#include <mbgl/util/timer.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
Reference in New Issue
Block a user