-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogfileUtils.cpp
More file actions
150 lines (125 loc) · 3.97 KB
/
Copy pathlogfileUtils.cpp
File metadata and controls
150 lines (125 loc) · 3.97 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "logfileUtils.h"
using std::filesystem::directory_iterator;
const char CDELIMITER = ' ';
std::string adjustTimestampInMinutes(std::string current, std::string format, int offset)
{
std::tm timestamp{};
std::istringstream iss(current);
std::string buf;
std::vector<std::string> dateVec;
std::cout << "current timestamp: " << current << std::endl;
iss >> std::get_time(×tamp, "%Y %b %d %H:%M:%S");
if (iss.fail())
{
throw std::runtime_error{"failed to parse time string"};
}
std::cout << "Number of hours = " << timestamp.tm_hour << '\n';
std::cout << "Number of minutes = " << timestamp.tm_min << '\n';
std::cout << "Number of seconds = " << timestamp.tm_sec << '\n';
timestamp.tm_min = timestamp.tm_min + (offset);
timestamp.tm_sec = 0;
std::stringstream dateStream;
dateStream << std::put_time(×tamp, "%Y %b %d %H:%M:%S");
return dateStream.str();
}
int getMilliseconds(std::string time)
{
int millisec = 0;
std::string delimiter = ".";
size_t pos_start;
pos_start = time.find(delimiter);
if (pos_start != std::string::npos)
{
millisec = stoi(time.substr(pos_start + 1));
}
// std::cout << "millisec: " << millisec << std::endl;
return millisec;
}
int compareTimestamp(std::string refernceTime, std::string currentTime)
{
std::tm time1{}, time2{};
std::istringstream iss1(refernceTime);
std::istringstream iss2(currentTime);
std::vector<std::string> dateVec;
iss1 >> std::get_time(&time1, "%Y %b %d %H:%M:%S");
if (iss1.fail())
{
throw std::runtime_error{"failed to parse time iss1 string"};
}
iss2 >> std::get_time(&time2, "%Y %b %d %H:%M:%S");
if (iss2.fail())
{
throw std::runtime_error{"failed to parse time iss2 string"};
}
// std::cout << "time1 : " << std::put_time(&time1, "%Y %b %d %H:%M:%S") << "time2: " << std::put_time(&time2, "%Y %b %d %H:%M:%S") << std::endl;
double seconds = difftime(mktime(&time1), mktime(&time2));
// std::cout << "time difference is : " << seconds << std::endl;
if (seconds == 0)
{
int time1Millisec = getMilliseconds(refernceTime);
int time2Millisec = getMilliseconds(currentTime);
return (time1Millisec - time2Millisec);
}
return seconds;
}
std::string convertTimestampFormat(std::string timestamp, std::string curfmt, std::string newfmt)
{
std::tm time{};
std::istringstream iss(timestamp);
std::stringstream newTime;
std::string convertedTimestamp = "";
iss >> std::get_time(&time, "%Y/%m/%d-%H:%M:%S");
if (!iss.fail())
{
newTime << std::put_time(&time, "%Y %b %d %H:%M:%S");
convertedTimestamp = newTime.str();
}
return convertedTimestamp;
}
std::vector<std::string> getListFiles(std::string path, std::string identifier)
{
std::vector<std::string> fileslist = {};
for (const auto &file : directory_iterator(path))
{
std::string infile(file.path().filename().string());
if (infile.find(identifier) != std::string::npos)
{
// std::cout << "found!" << '\n';
fileslist.push_back(file.path().string());
}
}
return fileslist;
}
std::string getFilename(std::string filenameWithPath)
{
std::string filename = "";
std::string delimiter = "/";
size_t pos_end;
pos_end = filenameWithPath.find_last_of(delimiter);
if (pos_end != std::string::npos)
{
filename = filenameWithPath.substr(pos_end + 1);
}
std::cout << "\nfilename: " << filename << std::endl;
return filename;
}
std::string getFileExtension(std::string filename)
{
std::string fileExtension = "";
std::string delimiter = ".";
size_t pos_start;
pos_start = filename.find(delimiter);
if (pos_start != std::string::npos)
{
fileExtension = filename.substr(pos_start);
}
std::cout << "fileExtension: " << fileExtension << std::endl;
return fileExtension;
}
// Function to convert timestamp string to time_t
std::time_t convertToTimeT(const std::string& timestamp) {
std::tm tm = {};
std::istringstream ss(timestamp);
ss >> std::get_time(&tm, "%Y %b %d %H:%M:%S");
return std::mktime(&tm);
}