Skip to content

Commit db38439

Browse files
committed
improve run time calc code
1 parent 97074c8 commit db38439

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

core/WebServer/WebServer.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ WebServer::WebServer(std::vector<monitor::Monitor*> &mons,
1818
m_running(true)
1919

2020
{
21+
m_start_time = std::chrono::system_clock::now();
22+
2123
// Disable all Mongoose logging to console
2224
mg_log_level = 0;
2325

@@ -153,12 +155,11 @@ void WebServer::handleStatusPage(struct mg_connection *c, struct mg_http_message
153155
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
154156

155157
// run time
156-
static auto start_time = std::chrono::system_clock::now();
157-
std::chrono::duration<double> elapsed_seconds = now - start_time;
158+
std::chrono::duration<double> elapsed_seconds = now - m_start_time;
158159
double hours = elapsed_seconds.count() / 3600.0;
159160

160161
html << "<p><b>Current Time:</b> " << std::ctime(&now_time) << "</p>";
161-
html << "<p><b>Run Time:</b> " << fmt1(hours,2) << " hours</p>";
162+
html << "<p><b>Run Time:</b> " << fmt1(hours,3) << " hours</p>";
162163

163164
// ---- Main table ----
164165
html << "<table>";

core/WebServer/WebServer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class WebServer {
4747

4848
std::vector<monitor::Monitor*> &m_monitor;
4949
contactor::Contactor* m_main_contactor;
50+
51+
std::chrono::system_clock::time_point m_start_time;
5052
};
5153

5254
} // namespace core

monitor/monitor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Monitor.hpp"
22
#include "bitset"
3+
#include <iomanip>
34
#include <chrono>
45
namespace monitor
56
{
@@ -15,7 +16,8 @@ namespace monitor
1516
std::chrono::duration<double> elapsed_seconds = nowtime - start;
1617

1718
os << "Current Time: " << std::ctime(&now_time);
18-
os << "Run Time: " << 0.001 * int(1000.0 * (int(elapsed_seconds.count() + 0.5) / 3600.0)) << " hours" << std::endl;
19+
os << "Run Time: " << std::fixed << std::setprecision(3) << (elapsed_seconds.count() / 3600.0) << " hours" << std::endl;
20+
os << std::defaultfloat << std::setprecision(6);
1921

2022
os << "Battery Number: ";
2123
for (i = 0; i < vm.size(); i++)

0 commit comments

Comments
 (0)