-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtimestamp.cpp
53 lines (44 loc) · 1.31 KB
/
timestamp.cpp
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
//
// Created by nam20485 on 6/12/22.
//
#include <sstream>
#include <iomanip>
#include <ctime>
#include "timestamp.h"
#include <sstream>
#include <time.h>
using namespace std::chrono;
namespace Utils
{
const int MS_PER_S = 1000;
std::string make_timestamp(const system_clock::time_point& timepoint)
{
auto tp_time_t = system_clock::to_time_t(timepoint);
struct tm* p_tm = localtime(&tp_time_t);
if (p_tm == nullptr) return "";
auto ms = duration_cast<milliseconds>(timepoint.time_since_epoch()) % MS_PER_S;
std::stringstream ss;
// date and time
ss << std::put_time(p_tm, "%D %T");
// add ms
ss << '.' << std::setfill('0') << std::setw(3) << ms.count();
return ss.str();
}
std::string make_timestamp()
{
return make_timestamp(system_clock::now());
}
system_clock::time_point parse_timestamp(const std::string& timestamp, const std::string& format)
{
struct tm tm{};
std::istringstream ss(timestamp);
ss >> std::get_time(&tm, format.c_str());
#if defined(_DEBUG)
std::stringstream ss2;
ss2 << std::put_time(&tm, "%D %T");
auto s = ss2.str();
#endif // DEBUG
auto tp = system_clock::from_time_t(std::mktime(&tm));
return tp;
}
}