src: Add code in case struct tm.tm_gmtoff is not available

This commit is contained in:
Tatsuhiro Tsujikawa 2014-11-24 18:39:35 +09:00
parent 8f22ff3032
commit e11834d1c9
2 changed files with 40 additions and 0 deletions

View File

@ -468,6 +468,14 @@ AC_CHECK_TYPES([ptrdiff_t])
AC_C_BIGENDIAN
AC_SYS_LARGEFILE
AC_CHECK_MEMBER([struct tm.tm_gmtoff], [have_struct_tm_tm_gmtoff=yes],
[have_struct_tm_tm_gmtoff=no], [[#include <time.h>]])
if test "x$have_struct_tm_tm_gmtoff" = "xyes"; then
AC_DEFINE([HAVE_STRUCT_TM_TM_GMTOFF], [1],
[Define to 1 if you have `struct tm.tm_gmtoff` member.])
fi
# Checks for library functions.
if test "x$cross_compiling" != "xyes"; then
AC_FUNC_MALLOC

View File

@ -231,6 +231,7 @@ std::string common_log_date(time_t t)
return "";
}
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
// Format data like this:
// 03/Jul/2014:00:19:38 +0900
std::string res;
@ -264,6 +265,13 @@ std::string common_log_date(time_t t)
p = cpydig(p, (gmtoff % 3600) / 60, 2);
return res;
#else // !HAVE_STRUCT_TM_TM_GMTOFF
char buf[32];
strftime(buf, sizeof(buf), "%d/%b/%Y:%T %z", &tms);
return buf;
#endif // !HAVE_STRUCT_TM_TM_GMTOFF
}
std::string iso8601_date(int64_t ms)
@ -275,6 +283,7 @@ std::string iso8601_date(int64_t ms)
return "";
}
#ifdef HAVE_STRUCT_TM_TM_GMTOFF
// Format data like this:
// 2014-11-15T12:58:24.741Z
// 2014-11-15T12:58:24.741+09:00
@ -315,6 +324,29 @@ std::string iso8601_date(int64_t ms)
res.resize(p - std::begin(res));
return res;
#else // !HAVE_STRUCT_TM_TM_GMTOFF
char buf[128];
auto nwrite = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", &tms);
nwrite += snprintf(&buf[nwrite], sizeof(buf) - nwrite, ".%03d",
static_cast<int>(ms % 1000));
auto nzone = strftime(&buf[nwrite], sizeof(buf) - nwrite, "%z", &tms);
// %z of strftime writes +hhmm or -hhmm not Z, +hh:mm or -hh:mm. Do
// %nothing if nzone is not 5. we don't know how to cope with this.
if(nzone == 5) {
if(memcmp(&buf[nwrite], "+0000", 5) == 0) {
// 0000 should be Z
memcpy(&buf[nwrite], "Z", 2);
} else {
// Move mm part to right by 1 including terminal \0
memmove(&buf[nwrite + 4], &buf[nwrite + 3], 3);
// Insert ':' between hh and mm
buf[nwrite + 3] = ':';
}
}
return buf;
#endif // !HAVE_STRUCT_TM_TM_GMTOFF
}
time_t parse_http_date(const std::string& s)