Skip to content

Commit eb2a88e

Browse files
committed
Use C++20 standard for time management
1 parent fe516db commit eb2a88e

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/dsf/headers/Dynamics.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <algorithm>
1515
#include <cassert>
16+
#include <chrono>
1617
#include <concepts>
1718
#include <vector>
1819
#include <random>
@@ -28,7 +29,6 @@
2829
#include <filesystem>
2930
#include <functional>
3031
#include <iomanip>
31-
#include <sstream>
3232

3333
#include <tbb/tbb.h>
3434

@@ -97,10 +97,9 @@ namespace dsf {
9797
/// @brief Get the current simulation time as formatted string (YYYY-MM-DD HH:MM:SS)
9898
/// @return std::string, The current simulation time as formatted string
9999
inline auto strDateTime() const {
100-
std::time_t const t = time();
101-
std::ostringstream oss;
102-
oss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
103-
return oss.str();
100+
return std::format(
101+
"{:%Y-%m-%d %H:%M:%S}", std::chrono::floor<std::chrono::seconds>(std::chrono::current_zone()->to_local(
102+
std::chrono::system_clock::from_time_t(time()))));
104103
}
105104
};
106105

test/Test_dynamics.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,11 @@ TEST_CASE("FirstOrderDynamics") {
225225
graph.importMatrix("./data/matrix.dat", false);
226226
// graph.adjustNodeCapacities();
227227
FirstOrderDynamics dynamics{graph, false, 69, 0., dsf::PathWeight::LENGTH};
228-
std::ostringstream default_oss;
229-
std::time_t defaultTime{0};
230-
default_oss << std::put_time(std::localtime(&defaultTime), "%Y-%m-%d %H:%M:%S");
231-
CHECK_EQ(dynamics.strDateTime(), default_oss.str());
228+
CHECK_EQ(dynamics.strDateTime(),
229+
std::format("{:%Y-%m-%d %H:%M:%S}",
230+
std::chrono::floor<std::chrono::seconds>(
231+
std::chrono::current_zone()->to_local(
232+
std::chrono::system_clock::from_time_t(0)))));
232233
auto const epochStart{
233234
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now())};
234235
dynamics.setInitTime(epochStart);
@@ -243,10 +244,13 @@ TEST_CASE("FirstOrderDynamics") {
243244
CHECK(dynamics.nAgents() < n);
244245
CHECK_EQ(dynamics.time_step(), 40);
245246
CHECK_EQ(dynamics.time() - epochStart, 40);
246-
std::ostringstream oss;
247-
auto currentTime = epochStart + 40;
248-
oss << std::put_time(std::localtime(&currentTime), "%Y-%m-%d %H:%M:%S");
249-
CHECK_EQ(dynamics.strDateTime(), oss.str());
247+
CHECK_EQ(
248+
dynamics.strDateTime(),
249+
std::format(
250+
"{:%Y-%m-%d %H:%M:%S}",
251+
std::chrono::floor<std::chrono::seconds>(
252+
std::chrono::current_zone()->to_local(
253+
std::chrono::system_clock::from_time_t(dynamics.time())))));
250254
}
251255
}
252256
}

0 commit comments

Comments
 (0)