replace strftime_l with our custom formatter

This commit is contained in:
Konstantin Käfer
2014-11-04 06:44:50 -08:00
parent b6c10e44c0
commit 75889d8124
3 changed files with 23 additions and 10 deletions

View File

@@ -1,7 +1,9 @@
#ifndef MBGL_UTIL_TIME
#define MBGL_UTIL_TIME
#include <string>
#include <cstdint>
#include <ctime>
namespace mbgl {
@@ -15,6 +17,10 @@ namespace util {
// comparisons between different timestamps produce meaningful values.
timestamp now();
// Returns the RFC1123 formatted date. E.g. "Tue, 04 Nov 2014 02:13:24 GMT"
std::string rfc1123(std::time_t time);
}
// used for time conversions

View File

@@ -2,6 +2,7 @@
#include <mbgl/util/uv-messenger.h>
#include <mbgl/util/std.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/time.hpp>
#include <uv.h>
#include <curl/curl.h>
@@ -9,8 +10,6 @@
#include <queue>
#include <cassert>
#include <cstring>
#include <ctime>
#include <xlocale.h>
// This file contains code from http://curl.haxx.se/libcurl/c/multi-uv.html:
@@ -369,9 +368,6 @@ void start_request(void *const ptr) {
util::ptr<HTTPRequestBaton> &baton = *baton_guard.get();
assert(baton);
// Create a C locale
static locale_t locale = newlocale(LC_ALL_MASK, nullptr, nullptr);
CURL *handle = nullptr;
if (!handles.empty()) {
handle = handles.front();
@@ -390,11 +386,9 @@ void start_request(void *const ptr) {
const std::string header = std::string("If-None-Match: ") + baton->response->etag;
context->headers = curl_slist_append(context->headers, header.c_str());
} else if (baton->response->modified) {
const time_t modified = baton->response->modified;
struct tm *timeinfo = std::gmtime(&modified);
char buffer[64];
strftime_l(buffer, 64, "If-Modified-Since: %a, %d %b %Y %H:%M:%S GMT", timeinfo, locale);
context->headers = curl_slist_append(context->headers, buffer);
const std::string time = std::string("If-Modified-Since: ") +
util::rfc1123(baton->response->modified);
context->headers = curl_slist_append(context->headers, time.c_str());
}
}

View File

@@ -8,5 +8,18 @@ timestamp now() {
return uv_hrtime();
}
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;
}
}
}