forked from JustVugg/Mqttcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_utils.hpp
More file actions
115 lines (94 loc) · 3.33 KB
/
Copy pathtime_utils.hpp
File metadata and controls
115 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// time_utils.hpp - CORRETTO per Windows
#ifndef OUR_TIME_UTILS_HPP
#define OUR_TIME_UTILS_HPP
#include <chrono>
#include <cstdint>
#include <thread>
#include <string>
#include <ctime>
#include <iomanip>
#include <sstream>
namespace ourmqtt {
class Time {
public:
static uint64_t now_ms() {
using namespace std::chrono;
return duration_cast<milliseconds>(
steady_clock::now().time_since_epoch()
).count();
}
static uint64_t now_us() {
using namespace std::chrono;
return duration_cast<microseconds>(
steady_clock::now().time_since_epoch()
).count();
}
static void sleep_ms(uint32_t ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
static std::string timestamp() {
auto now = std::chrono::system_clock::now();
auto time_t = std::chrono::system_clock::to_time_t(now);
char buffer[100];
#ifdef _WIN32
// Usa localtime_s per Windows (versione sicura)
struct tm tm_info;
localtime_s(&tm_info, &time_t);
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tm_info);
#else
// Linux/Unix usa localtime standard
std::tm* tm = std::localtime(&time_t);
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm);
#endif
return std::string(buffer);
}
};
class Timer {
private:
std::chrono::steady_clock::time_point start_time_;
public:
Timer() : start_time_(std::chrono::steady_clock::now()) {}
void reset() {
start_time_ = std::chrono::steady_clock::now();
}
uint64_t elapsed_ms() const {
auto now = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(
now - start_time_
).count();
}
uint64_t elapsed_us() const {
auto now = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(
now - start_time_
).count();
}
bool has_expired(uint64_t timeout_ms) const {
return elapsed_ms() >= timeout_ms;
}
};
class Stopwatch {
private:
std::chrono::high_resolution_clock::time_point start_;
std::chrono::high_resolution_clock::time_point lap_;
public:
Stopwatch() {
start_ = lap_ = std::chrono::high_resolution_clock::now();
}
void start() {
start_ = lap_ = std::chrono::high_resolution_clock::now();
}
double lap() {
auto now = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - lap_);
lap_ = now;
return duration.count() / 1000.0;
}
double elapsed() const {
auto now = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(now - start_);
return duration.count() / 1000.0;
}
};
}
#endif