diff --git a/include/sys.hpp b/include/sys.hpp index c93128a..ee96cf0 100644 --- a/include/sys.hpp +++ b/include/sys.hpp @@ -1,13 +1,8 @@ #ifndef SYS_HPP #define SYS_HPP -#include -#include - -#include -#include +#include #include -#include namespace sys { @@ -27,8 +22,8 @@ struct Memory { }; struct TimeDate { - char time[9]; - char date[13]; + std::string time; + std::string date; }; struct Network { @@ -39,7 +34,7 @@ struct Network { void getTimeDate(TimeDate* td); void getDisk(Memory* disk, std::string mount); void getRam(Memory* mem); -void getNetwork(Network* net, std::string_view device); +void getNetwork(Network* net, std::string device); void detectHwmon(sysSensors* s, std::string platform); void updateSensors(sysSensors* s); diff --git a/src/sys.cpp b/src/sys.cpp index ec889a1..560bc67 100644 --- a/src/sys.cpp +++ b/src/sys.cpp @@ -1,12 +1,17 @@ +#include #include +#include #include #include +#include void sys::getTimeDate(TimeDate* td) { - auto now = std::time(nullptr); - auto tm = *std::localtime(&now); - sprintf(td->time, "%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec); - sprintf(td->date, "%s, %d %s", wdays[tm.tm_wday], tm.tm_mday, months[tm.tm_mon]); + auto now = std::chrono::system_clock::now(); + auto local = std::chrono::current_zone()->to_local(now); + std::chrono::year_month_day ymd{floor(local)}; + std::chrono::weekday wd{floor(local)}; + td->time = std::format("{:%H:%M:%S}", floor(local)); + td->date = std::format("{}, {} {}", wdays[wd.c_encoding()], ymd.day(), months[(unsigned)ymd.month() - 1]); } void sys::getRam(Memory* mem) { @@ -24,7 +29,7 @@ void sys::getDisk(Memory* disk, std::string mount) { disk->total = diskData.f_blocks * diskData.f_frsize; } -void sys::getNetwork(Network* net, std::string_view device) { +void sys::getNetwork(Network* net, std::string device) { struct _net { std::chrono::time_point ts; uint64_t rx = 0; @@ -35,8 +40,8 @@ void sys::getNetwork(Network* net, std::string_view device) { _net current; current.ts = std::chrono::steady_clock::now(); - current.rx = std::stoull(readFile("/sys/class/net/" + std::string(device) + "/statistics/rx_bytes", 32)); - current.tx = std::stoull(readFile("/sys/class/net/" + std::string(device) + "/statistics/tx_bytes", 32)); + current.rx = std::stoull(readFile("/sys/class/net/" + device + "/statistics/rx_bytes", 32)); + current.tx = std::stoull(readFile("/sys/class/net/" + device + "/statistics/tx_bytes", 32)); float ts_diff = std::chrono::duration_cast(current.ts - prev.ts).count() / 1000000.0f; net->download = (current.rx - prev.rx) / ts_diff; net->upload = (current.tx - prev.tx) / ts_diff;