Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamp on backup files #52

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/cloysterhpc/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void backupFile(std::string_view filename);
void changeValueInConfigurationFile(
const std::string&, const std::string&, std::string_view);
void addStringToFile(std::string_view filename, std::string_view string);
std::string getCurrentTimestamp();

} /* namespace cloyster */

Expand Down
20 changes: 19 additions & 1 deletion src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,28 @@ void removeFile(std::string_view filename)
}
}

/**
* \brief Get the current timestamp as a string.
*
* This function generates the current timestamp in the format
* "YYYYMMDD_HHMMSS".
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs needs to be also updated.

*
* \return A string with the current timestamp.
*/
std::string getCurrentTimestamp()
{
time_t now;
time(&now);
char buf[sizeof "2011-10-08T07:07:09Z"];
strftime(buf, sizeof buf, "%FT%TZ", gmtime(&now));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use chrono instead?

strftime is a C directive that's available on ctime and we should definitely avoid using legacy C functions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this can be done with chrono
https://en.cppreference.com/w/cpp/chrono

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use stringstream if using chrono. Already discussed with @viniciusferrao

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The beautiful thing of C++ is the legacy that we have. There's probably a way to completely ditch stringstream because it sucks and we have std::format now, however we just need to find a way to use it with chrono.

return buf;
}

/* Backup file */
void backupFile(std::string_view filename)
{
const auto& backupFile = fmt::format("{}/backup{}", installPath, filename);
const auto& backupFile = fmt::format(
"{}/backup{}_{}", installPath, filename, getCurrentTimestamp());

if (cloyster::dryRun) {
LOG_INFO(
Expand Down
Loading