Skip to content

Commit

Permalink
Please use std::chrono::parse instead of date::parse (#67)
Browse files Browse the repository at this point in the history
* use std::chrono::parse instead of date::parse

* add REDUCT_CPP_USE_STD_CHRONO option
  • Loading branch information
toge authored May 24, 2024
1 parent d04a523 commit d90c7bc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions cmake/InstallDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
13 changes: 13 additions & 0 deletions src/reduct/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

#include "reduct/client.h"

#ifdef REDUCT_CPP_USE_STD_CHRONO
#include <chrono>
#else
#include <date/date.h>
#endif
#include <fmt/core.h>
#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -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::string>()) >>
std::chrono::parse("%FT%TZ", server_info.license->expiry_date);
#else
std::istringstream(license.at("expiry_date").get<std::string>()) >>
date::parse("%FT%TZ", server_info.license->expiry_date);
#endif
}

return {
Expand Down Expand Up @@ -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::string>()) >> std::chrono::parse("%FT%TZ", created_at);
#else
std::istringstream(token.at("created_at").get<std::string>()) >> date::parse("%FT%TZ", created_at);
#endif

token_list.push_back(Token{
.name = token.at("name"),
Expand Down
8 changes: 8 additions & 0 deletions src/reduct/internal/serialisation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

#include "reduct/internal/serialisation.h"

#ifdef REDUCT_CPP_USE_STD_CHRONO
#include <chrono>
#else
#include <date/date.h>
#endif

namespace reduct::internal {

Expand Down Expand Up @@ -65,7 +69,11 @@ Result<IBucket::Settings> ParseBucketSettings(const nlohmann::json& json) {

Result<IClient::FullTokenInfo> ParseTokenInfo(const nlohmann::json& json) {
IClient::Time created_at;
#ifdef REDUCT_CPP_USE_STD_CHRONO
std::istringstream(json.at("created_at").get<std::string>()) >> std::chrono::parse("%FT%TZ", created_at);
#else
std::istringstream(json.at("created_at").get<std::string>()) >> date::parse("%FT%TZ", created_at);
#endif

return {
IClient::FullTokenInfo{
Expand Down

0 comments on commit d90c7bc

Please sign in to comment.