Skip to content

Commit 08b7484

Browse files
committed
[CLI] Return date-time in stricter format for info command
1 parent aa67f92 commit 08b7484

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

cpp/main.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ string Version::getAppFullInfo(bool csv) {
288288
if (!csv) {
289289
out << "Compile Time: ";
290290
}
291-
out << __DATE__ << " " << __TIME__;
291+
out << getCompilationDateTime(csv);
292292
out << commaOrLineBreak();
293293

294294
if (!csv) {
@@ -349,3 +349,39 @@ string Version::getBackend() {
349349
#endif
350350
;
351351
}
352+
353+
string Version::getCompilationDateTime(const bool csv) {
354+
if (csv) {
355+
try {
356+
// Try to return date-time in the following format (completely numeric): YYYY-MM-DD-HH-MM-SS
357+
const vector<std::string> dateStrs = Global::split(__DATE__);
358+
const vector<std::string> timeStrs = Global::split(__TIME__, ':');
359+
360+
const int year = Global::stringToInt(dateStrs[2]);
361+
362+
static const vector<string> months = {
363+
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
364+
};
365+
const size_t monthIndex = indexOf(months, dateStrs[0]);
366+
if (monthIndex == std::string::npos) throw std::runtime_error("Invalid month");
367+
368+
const int day = Global::stringToInt(dateStrs[1]);
369+
370+
const int hour = Global::stringToInt(timeStrs[0]);
371+
372+
const int minute = Global::stringToInt(timeStrs[1]);
373+
374+
const int second = Global::stringToInt(timeStrs[2]);
375+
376+
std::string out;
377+
out.resize(19); // "YYYY-MM-DD-hh-mm-ss" = 19 chars
378+
std::snprintf(out.data(), out.size() + 1,
379+
"%04d-%02d-%02d-%02d-%02d-%02d",
380+
year, static_cast<int>(monthIndex) + 1, day, hour, minute, second);
381+
return out;
382+
} catch (const std::exception& _) {
383+
}
384+
}
385+
386+
return string(__DATE__) + " " + string(__TIME__);
387+
}

cpp/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ namespace Version {
6767
std::string getGitRevision();
6868
std::string getBackend();
6969
std::string getBuildType();
70+
std::string getCompilationDateTime(bool csv = false);
7071
}

0 commit comments

Comments
 (0)