Stefan Schuermans commited on 2019-05-04 14:39:12
Showing 4 changed files, with 62 additions and 0 deletions.
... | ... |
@@ -4,8 +4,11 @@ |
4 | 4 |
a blinkenarea.org project */ |
5 | 5 |
|
6 | 6 |
#include <errno.h> |
7 |
+#include <iomanip> |
|
7 | 8 |
#include <math.h> |
9 |
+#include <sstream> |
|
8 | 10 |
#include <stdint.h> |
11 |
+#include <string> |
|
9 | 12 |
#include <sys/time.h> |
10 | 13 |
#include <time.h> |
11 | 14 |
|
... | ... |
@@ -202,6 +205,27 @@ void Time::toTimeval(struct timeval &tv) const |
202 | 205 |
} |
203 | 206 |
} |
204 | 207 |
|
208 |
+/** |
|
209 |
+ * @brief convert to human-readable string |
|
210 |
+ * @return human-readable string |
|
211 |
+ */ |
|
212 |
+std::string Time::toStr() const |
|
213 |
+{ |
|
214 |
+ time_t sec = m_sec; |
|
215 |
+ struct tm t; |
|
216 |
+ localtime_r(&sec, &t); |
|
217 |
+ std::stringstream strm; |
|
218 |
+ strm << std::setfill('0') |
|
219 |
+ << std::setw(4) << (t.tm_year + 1900) |
|
220 |
+ << "-" << std::setw(2) << (t.tm_mon + 1) |
|
221 |
+ << "-" << std::setw(2) << t.tm_mday |
|
222 |
+ << " " << std::setw(2) << t.tm_hour |
|
223 |
+ << ":" << std::setw(2) << t.tm_min |
|
224 |
+ << ":" << std::setw(2) << t.tm_sec |
|
225 |
+ << "." << std::setw(9) << m_ns; |
|
226 |
+ return strm.str(); |
|
227 |
+} |
|
228 |
+ |
|
205 | 229 |
/// fix internal time representation after calculation |
206 | 230 |
void Time::fix() |
207 | 231 |
{ |
... | ... |
@@ -7,6 +7,7 @@ |
7 | 7 |
#define BLINKER_TIME_H |
8 | 8 |
|
9 | 9 |
#include <stdint.h> |
10 |
+#include <string> |
|
10 | 11 |
#include <sys/time.h> |
11 | 12 |
#include <time.h> |
12 | 13 |
|
... | ... |
@@ -85,6 +86,12 @@ public: |
85 | 86 |
*/ |
86 | 87 |
void toTimeval(struct timeval &tv) const; |
87 | 88 |
|
89 |
+ /** |
|
90 |
+ * @brief convert to human-readable string |
|
91 |
+ * @return human-readable string |
|
92 |
+ */ |
|
93 |
+ std::string toStr() const; |
|
94 |
+ |
|
88 | 95 |
public: |
89 | 96 |
/// sleep for duration |
90 | 97 |
void sleepFor() const; |
... | ... |
@@ -5,8 +5,11 @@ |
5 | 5 |
|
6 | 6 |
#include <winsock2.h> // not allowed after windows.h, so include here |
7 | 7 |
#include <windows.h> |
8 |
+#include <iomanip> |
|
8 | 9 |
#include <math.h> |
10 |
+#include <sstream> |
|
9 | 11 |
#include <stdint.h> |
12 |
+#include <string> |
|
10 | 13 |
|
11 | 14 |
#include "Time.h" |
12 | 15 |
|
... | ... |
@@ -240,6 +243,27 @@ void Time::fromFileTime(FILETIME const &ft) |
240 | 243 |
m_ns = (ft_u.u.QuadPart % 10000000ULL) * 100ULL; |
241 | 244 |
} |
242 | 245 |
|
246 |
+/** |
|
247 |
+ * @brief convert to human-readable string |
|
248 |
+ * @return human-readable string |
|
249 |
+ */ |
|
250 |
+std::string Time::toStr() const |
|
251 |
+{ |
|
252 |
+ time_t sec = m_sec; |
|
253 |
+ struct tm t; |
|
254 |
+ localtime_s(&t, &sec); |
|
255 |
+ std::stringstream strm; |
|
256 |
+ strm << std::setfill('0') |
|
257 |
+ << std::setw(4) << (t.tm_year + 1900) |
|
258 |
+ << "-" << std::setw(2) << (t.tm_mon + 1) |
|
259 |
+ << "-" << std::setw(2) << t.tm_mday |
|
260 |
+ << " " << std::setw(2) << t.tm_hour |
|
261 |
+ << ":" << std::setw(2) << t.tm_min |
|
262 |
+ << ":" << std::setw(2) << t.tm_sec |
|
263 |
+ << "." << std::setw(9) << m_ns; |
|
264 |
+ return strm.str(); |
|
265 |
+} |
|
266 |
+ |
|
243 | 267 |
/// fix internal time representation after calculation |
244 | 268 |
void Time::fix() |
245 | 269 |
{ |
... | ... |
@@ -9,6 +9,7 @@ |
9 | 9 |
#include <winsock2.h> // not allowed after windows.h, so include here |
10 | 10 |
#include <windows.h> |
11 | 11 |
#include <stdint.h> |
12 |
+#include <string> |
|
12 | 13 |
|
13 | 14 |
namespace Blinker { |
14 | 15 |
|
... | ... |
@@ -97,6 +98,12 @@ public: |
97 | 98 |
*/ |
98 | 99 |
void fromFileTime(FILETIME const &ft); |
99 | 100 |
|
101 |
+ /** |
|
102 |
+ * @brief convert to human-readable string |
|
103 |
+ * @return human-readable string |
|
104 |
+ */ |
|
105 |
+ std::string toStr() const; |
|
106 |
+ |
|
100 | 107 |
public: |
101 | 108 |
/// sleep for duration |
102 | 109 |
void sleepFor() const; |
103 | 110 |