From c54f28d29d5f1d7a2bb973609beffbe5ad56d422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 11 Oct 2016 21:00:56 +0200 Subject: =?UTF-8?q?Conditionally=20use=20strptime=20if=20we=20don=E2=80=99?= =?UTF-8?q?t=20have=20std::get=5Ftime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- louloulibs/CMakeLists.txt | 13 +++++++++++++ louloulibs/louloulibs.h.cmake | 3 ++- louloulibs/utils/time.cpp | 11 +++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/louloulibs/CMakeLists.txt b/louloulibs/CMakeLists.txt index bf53504..1858bb3 100644 --- a/louloulibs/CMakeLists.txt +++ b/louloulibs/CMakeLists.txt @@ -143,4 +143,17 @@ if(SYSTEMD_FOUND) target_link_libraries(xmpplib ${SYSTEMD_LIBRARIES}) endif() +# +## Check if we have std::get_time +# +include(CheckCXXSourceCompiles) + +check_cxx_source_compiles(" + #include + int main() + { std::get_time(nullptr, \"\"); }" + HAS_GET_TIME) + +mark_as_advanced(HAS_GET_TIME) + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/louloulibs.h.cmake ${CMAKE_BINARY_DIR}/src/louloulibs.h) diff --git a/louloulibs/louloulibs.h.cmake b/louloulibs/louloulibs.h.cmake index 2feaf4e..d5328b8 100644 --- a/louloulibs/louloulibs.h.cmake +++ b/louloulibs/louloulibs.h.cmake @@ -6,4 +6,5 @@ #cmakedefine BOTAN_FOUND #cmakedefine CARES_FOUND #cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}" -#cmakedefine PROJECT_NAME "${PROJECT_NAME}" \ No newline at end of file +#cmakedefine PROJECT_NAME "${PROJECT_NAME}" +#cmakedefine HAS_GET_TIME diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index 7ad6663..afd6117 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -5,6 +5,8 @@ #include #include +#include "louloulibs.h" + namespace utils { std::string to_string(const std::time_t& timestamp) @@ -20,6 +22,7 @@ std::time_t parse_datetime(const std::string& stamp) { static const char* format = "%Y-%m-%dT%H:%M:%S"; std::tm t = {}; +#ifdef HAS_GET_TIME std::istringstream ss(stamp); ss.imbue(std::locale("en_US.utf-8")); @@ -27,6 +30,14 @@ std::time_t parse_datetime(const std::string& stamp) ss >> std::get_time(&t, format) >> timezone; if (ss.fail()) return -1; +#else + /* Y - m - d T H : M : S */ + constexpr std::size_t stamp_size_without_tz = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2; + if (!strptime(stamp.data(), format, &t)) { + return -1; + } + const std::string timezone(stamp.data() + stamp_size_without_tz); +#endif if (timezone.empty()) return -1; -- cgit v1.2.3