Conditionally use strptime if we don’t have std::get_time
This commit is contained in:
@@ -143,4 +143,17 @@ if(SYSTEMD_FOUND)
|
|||||||
target_link_libraries(xmpplib ${SYSTEMD_LIBRARIES})
|
target_link_libraries(xmpplib ${SYSTEMD_LIBRARIES})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
#
|
||||||
|
## Check if we have std::get_time
|
||||||
|
#
|
||||||
|
include(CheckCXXSourceCompiles)
|
||||||
|
|
||||||
|
check_cxx_source_compiles("
|
||||||
|
#include <iomanip>
|
||||||
|
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)
|
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/louloulibs.h.cmake ${CMAKE_BINARY_DIR}/src/louloulibs.h)
|
||||||
|
|||||||
@@ -7,3 +7,4 @@
|
|||||||
#cmakedefine CARES_FOUND
|
#cmakedefine CARES_FOUND
|
||||||
#cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}"
|
#cmakedefine SOFTWARE_VERSION "${SOFTWARE_VERSION}"
|
||||||
#cmakedefine PROJECT_NAME "${PROJECT_NAME}"
|
#cmakedefine PROJECT_NAME "${PROJECT_NAME}"
|
||||||
|
#cmakedefine HAS_GET_TIME
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
|
|
||||||
|
#include "louloulibs.h"
|
||||||
|
|
||||||
namespace utils
|
namespace utils
|
||||||
{
|
{
|
||||||
std::string to_string(const std::time_t& timestamp)
|
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";
|
static const char* format = "%Y-%m-%dT%H:%M:%S";
|
||||||
std::tm t = {};
|
std::tm t = {};
|
||||||
|
#ifdef HAS_GET_TIME
|
||||||
std::istringstream ss(stamp);
|
std::istringstream ss(stamp);
|
||||||
ss.imbue(std::locale("en_US.utf-8"));
|
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;
|
ss >> std::get_time(&t, format) >> timezone;
|
||||||
if (ss.fail())
|
if (ss.fail())
|
||||||
return -1;
|
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())
|
if (timezone.empty())
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user