Skip to content

Commit a0bfdb7

Browse files
authored
Merge pull request #3 from WeiJiLab/add_date_lib
Add C++20 compatible date library
2 parents 143a9b4 + 070db54 commit a0bfdb7

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ include(${CMAKE_BINARY_DIR}/conan.cmake)
1717
conan_cmake_configure(REQUIRES boost/1.76.0
1818
REQUIRES gtest/1.11.0
1919
REQUIRES nlohmann_json/3.9.1
20+
REQUIRES date/3.0.1
2021
OPTIONS boost:header_only=True
22+
OPTIONS date:header_only=True
2123
GENERATORS cmake_find_package)
2224

2325
conan_cmake_autodetect(settings)

rest/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ find_package(Threads REQUIRED)
1515
target_link_libraries(rest PRIVATE Threads::Threads)
1616

1717
target_link_libraries(rest PUBLIC controller)
18+
19+
find_package(date REQUIRED)
20+
target_link_libraries(rest PUBLIC date::date)

rest/controller/MeterReadingController.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef DEVELOPER_JOYOFENERGY_CPP_BEAST_METERREADINGCONTROLLER_H
22
#define DEVELOPER_JOYOFENERGY_CPP_BEAST_METERREADINGCONTROLLER_H
33

4+
#include <date/date.h>
45
#include <service/ElectricityReadingService.h>
56
#include <service/MeterReadingService.h>
67

@@ -16,17 +17,13 @@ namespace http = boost::beast::http;
1617

1718
namespace detail {
1819
auto toRfc3339(std::chrono::time_point<std::chrono::system_clock> time) {
19-
std::stringstream ss;
20-
const auto ctime = std::chrono::system_clock::to_time_t(time);
21-
ss << std::put_time(std::gmtime(&ctime), "%FT%T");
22-
return ss.str();
20+
return date::format("%FT%TZ", time);
2321
}
2422

2523
auto fromRfc3339(const std::string &time) {
26-
std::tm tm = {};
27-
std::stringstream ss(time);
28-
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
29-
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));
24+
std::chrono::time_point<std::chrono::system_clock> tp;
25+
std::istringstream ss(time);
26+
ss >> date::parse("%FT%TZ", tp);
3027
return tp;
3128
}
3229

0 commit comments

Comments
 (0)