Skip to content

Commit

Permalink
Some works to add a getDateTime function.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-trinh committed Oct 22, 2023
1 parent f1b8f88 commit 2ead3e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/core/include/visp3/core/vpIoTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class VISP_EXPORT vpIoTools
static std::string getUserName();
static std::string getenv(const std::string &env);
static std::string getViSPImagesDataPath();
static std::string getDateTime(const std::string &format="%Y-%m-%d_%H-%M-%S");
static void getVersion(const std::string &version, unsigned int &major, unsigned int &minor, unsigned int &patch);
static bool checkDirectory(const std::string &dirname);
static bool checkFifo(const std::string &filename);
Expand Down
23 changes: 23 additions & 0 deletions modules/core/src/tools/file/vpIoTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,29 @@ std::string vpIoTools::getTempPath()
#endif
}

std::string vpIoTools::getDateTime(const std::string &format)
{
std::time_t now;
std::time(&now);

// first try with an on-stack buffer (fast path)
char buf[64];
size_t written = std::strftime(buf, sizeof(buf), format.c_str(), std::localtime(&now));
if (written > 0) {
return buf;
}

// now, iterate with an allocated buffer
size_t len = sizeof(buf);
std::vector<char> v;
do {
v.resize(len *= 2);
written = std::strftime(v.data(), v.size(), format.c_str(), std::localtime(&now));
} while (written == 0);

return std::string(v.data(), written);
}

/*!
Sets the base name (prefix) of the experiment files.
Expand Down
1 change: 1 addition & 0 deletions modules/core/test/tools/io/testBuildInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ int main()
{
std::string info = vpIoTools::getBuildInformation();
std::cout << info << std::endl;
std::cout << vpIoTools::getDateTime() << std::endl;

return EXIT_SUCCESS;
}

0 comments on commit 2ead3e1

Please sign in to comment.