[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,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);
}
}

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

View File

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

View File

@@ -73,7 +73,7 @@
*/
#include <mbgl/util/parsedate.h>
#include "parsedate.h"

38
src/parsedate/parsedate.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef HEADER_PARSEDATE_H
#define HEADER_PARSEDATE_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
#include <time.h>
time_t parse_date(const char *p);
#ifdef __cplusplus
}
#endif
#endif /* HEADER_PARSEDATE_H */