From d90c7bc94204a334ebbac691edb9c50c6377cdec Mon Sep 17 00:00:00 2001 From: toge Date: Fri, 24 May 2024 20:53:01 +0900 Subject: [PATCH] Please use std::chrono::parse instead of date::parse (#67) * use std::chrono::parse instead of date::parse * add REDUCT_CPP_USE_STD_CHRONO option --- CMakeLists.txt | 1 + cmake/InstallDependencies.cmake | 18 +++++++++++------- src/CMakeLists.txt | 3 +++ src/reduct/client.cc | 13 +++++++++++++ src/reduct/internal/serialisation.cc | 8 ++++++++ 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cda9d1d..9f99897 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ project(reductcpp VERSION ${REDUCT_CPP_FULL_VERSION}) message(STATUS "Version ${REDUCT_CPP_FULL_VERSION}") set(REDUCT_CPP_ENABLE_TESTS OFF CACHE BOOL "Compile tests") +set(REDUCT_CPP_USE_STD_CHRONO OFF CACHE BOOL "use std::chrono instead of HowardHinnant date library") set(CMAKE_CXX_STANDARD 20) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/cmake/InstallDependencies.cmake b/cmake/InstallDependencies.cmake index af91360..47e29db 100644 --- a/cmake/InstallDependencies.cmake +++ b/cmake/InstallDependencies.cmake @@ -49,12 +49,16 @@ else () URL_HASH MD5=814c5e121b29e37ee836312f0eb0328f ) - FetchContent_Declare( - date - URL https://github.com/HowardHinnant/date/archive/refs/tags/v3.0.1.zip - URL_HASH MD5=cf556cc376d15055b8235b05b2fc6253) - - FetchContent_MakeAvailable(fmt nlohmann_json httplib concurrentqueue date) add_library(dependencies INTERFACE) - target_link_libraries(dependencies INTERFACE fmt nlohmann_json httplib concurrentqueue date) + FetchContent_MakeAvailable(fmt nlohmann_json httplib concurrentqueue) + target_link_libraries(dependencies INTERFACE fmt nlohmann_json httplib concurrentqueue) + + if(NOT REDUCT_CPP_USE_STD_CHRONO) + FetchContent_Declare( + date + URL https://github.com/HowardHinnant/date/archive/refs/tags/v3.0.1.zip + URL_HASH MD5=cf556cc376d15055b8235b05b2fc6253) + FetchContent_MakeAvailable(date) + target_link_libraries(dependencies INTERFACE date) + endif() endif () diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 10a0925..a868531 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,6 +24,9 @@ target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_ target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE dependencies) set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${PUBLIC_HEADERS}") target_compile_definitions(${CMAKE_PROJECT_NAME} PUBLIC CPPHTTPLIB_OPENSSL_SUPPORT) +if(REDUCT_CPP_USE_STD_CHRONO) + target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE REDUCT_CPP_USE_STD_CHRONO) +endif() include(GNUInstallDirs) install(TARGETS ${CMAKE_PROJECT_NAME} diff --git a/src/reduct/client.cc b/src/reduct/client.cc index d1bff1e..81e3c2b 100644 --- a/src/reduct/client.cc +++ b/src/reduct/client.cc @@ -2,7 +2,11 @@ #include "reduct/client.h" +#ifdef REDUCT_CPP_USE_STD_CHRONO +#include +#else #include +#endif #include #include @@ -56,8 +60,13 @@ class Client : public IClient { .disk_quota = license.at("disk_quota"), .fingerprint = license.at("fingerprint"), }; +#ifdef REDUCT_CPP_USE_STD_CHRONO + std::istringstream(license.at("expiry_date").get()) >> + std::chrono::parse("%FT%TZ", server_info.license->expiry_date); +#else std::istringstream(license.at("expiry_date").get()) >> date::parse("%FT%TZ", server_info.license->expiry_date); +#endif } return { @@ -142,7 +151,11 @@ class Client : public IClient { token_list.reserve(json_tokens.size()); for (const auto& token : json_tokens) { Time created_at; +#ifdef REDUCT_CPP_USE_STD_CHRONO + std::istringstream(token.at("created_at").get()) >> std::chrono::parse("%FT%TZ", created_at); +#else std::istringstream(token.at("created_at").get()) >> date::parse("%FT%TZ", created_at); +#endif token_list.push_back(Token{ .name = token.at("name"), diff --git a/src/reduct/internal/serialisation.cc b/src/reduct/internal/serialisation.cc index 01f19e1..184314c 100644 --- a/src/reduct/internal/serialisation.cc +++ b/src/reduct/internal/serialisation.cc @@ -2,7 +2,11 @@ #include "reduct/internal/serialisation.h" +#ifdef REDUCT_CPP_USE_STD_CHRONO +#include +#else #include +#endif namespace reduct::internal { @@ -65,7 +69,11 @@ Result ParseBucketSettings(const nlohmann::json& json) { Result ParseTokenInfo(const nlohmann::json& json) { IClient::Time created_at; +#ifdef REDUCT_CPP_USE_STD_CHRONO + std::istringstream(json.at("created_at").get()) >> std::chrono::parse("%FT%TZ", created_at); +#else std::istringstream(json.at("created_at").get()) >> date::parse("%FT%TZ", created_at); +#endif return { IClient::FullTokenInfo{