-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_utils.h
More file actions
61 lines (53 loc) · 1.25 KB
/
time_utils.h
File metadata and controls
61 lines (53 loc) · 1.25 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
#pragma once
#include <ctime>
#include <string>
namespace TimeUtils
{
/**
* @brief
* Returns a snapshot of the current time.
*/
time_t now() {
return time_t(0);
}
/**
* @brief
* Calculates the difference between the two specified time points.
*
* @param start
* The first time point.
*
* @param end
* The second time point.
*
* @returns
* The difference between the two time points in seconds.
*/
int delta(const time_t& start, const time_t& end) {
return static_cast<int>(difftime(end, start));
}
/**
* @brief
* Formats the specified time into a string based on the specified format.
*
* @param time
* The time to format.
*
* @param format
* The format which will be used to convert the time into a string.
*/
std::string format(const time_t& time, const std::string& format) {
struct tm localTime;
localtime_s(&localTime, &time);
size_t result;
std::string buffer;
buffer.resize(format.size() + 64);
size_t result = strftime(buffer.data(), buffer.size(), format.c_str(), &localTime);
while (result == 0 && !format.empty()) {
buffer.resize(buffer.size() * 2);
result = strftime(buffer.data(), buffer.size(), format.c_str(), &localTime);
}
buffer.resize(result);
return buffer;
}
};