c++ify time/date, clean up includes while we're at it

This commit is contained in:
Drath Vader
2025-10-24 15:10:14 +02:00
parent 5e42c45e28
commit 3ccaa513b3
2 changed files with 16 additions and 16 deletions

View File

@@ -1,13 +1,8 @@
#ifndef SYS_HPP
#define SYS_HPP
#include <string_view>
#include <sys/statvfs.h>
#include <array>
#include <ctime>
#include <cstdint>
#include <string>
#include <utils.hpp>
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);

View File

@@ -1,12 +1,17 @@
#include <sys/statvfs.h>
#include <chrono>
#include <format>
#include <string>
#include <sys.hpp>
#include <utils.hpp>
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<std::chrono::days>(local)};
std::chrono::weekday wd{floor<std::chrono::days>(local)};
td->time = std::format("{:%H:%M:%S}", floor<std::chrono::seconds>(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<std::chrono::steady_clock> 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<std::chrono::microseconds>(current.ts - prev.ts).count() / 1000000.0f;
net->download = (current.rx - prev.rx) / ts_diff;
net->upload = (current.tx - prev.tx) / ts_diff;