Skip to content

Commit

Permalink
rename string::date_time() to string::current_time() and improved its…
Browse files Browse the repository at this point in the history
… implementation
  • Loading branch information
LiangliangNan committed Oct 24, 2024
1 parent 4016420 commit a0aebb3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
30 changes: 14 additions & 16 deletions easy3d/util/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <iomanip>
#include <cmath>
#include <codecvt>
#include <chrono>


namespace easy3d {
Expand Down Expand Up @@ -197,26 +198,23 @@ namespace easy3d {
}


// format example: "Fri Jan 09 11:39:32 2015"
std::string date_time() {
std::string current_time() {
#if 0
// The following code will result in the format: "Fri Jan 09 11:39:32 2015"
time_t now = ::time(nullptr); /* get current time; same as: time(&now) */
struct tm *timeinfo = localtime(&now);
std::string tstr = asctime(timeinfo);

//return tstr;
return tstr.substr(0, tstr.length() - 1); // discard the terminating null-character

// use strftime() to define your own stamp format
//size_t strftime(char *strDest, size_t maxsize, const char *format, const struct tm *timeptr);

// get a precise timestamp as a string
// const auto now = std::chrono::system_clock::now();
// const auto now_as_time_t = std::chrono::system_clock::to_time_t(now);
// const auto now_in_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
// std::stringstream time_stream;
// time_stream << std::put_time(std::localtime(&now_as_time_t), "%Y%m%d %T")
// << '.' << std::setfill('0') << std::setw(3) << now_in_ms.count();
// time_str_ = time_stream.str();
#endif
// get a precise timestamp as a string in the format: "2024-10-24-17-41-16-753"
const auto now = std::chrono::system_clock::now();
const auto now_as_time_t = std::chrono::system_clock::to_time_t(now);
const auto now_in_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
std::stringstream time_stream;
time_stream << std::put_time(std::localtime(&now_as_time_t), "%Y-%m-%d-%H-%M-%S") // format like "2024-10-24-17-41-16-753"
<< '-' << std::setfill('0') << std::setw(3)
<< now_in_ms.count(); // add milliseconds
return time_stream.str();
}


Expand Down
4 changes: 2 additions & 2 deletions easy3d/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ namespace easy3d {
void appendf(std::string &dst, const char *format, ...);

/**
* @brief Gets the time string, e.g., "Fri Jan 09 11:39:32 2015"
* @brief Gets the current time as a string, e.g., "2024-10-24-17-41-16-753"
*/
std::string date_time();
std::string current_time();

/**
* @brief Converts \p time (in millisecond) into a string with the most suitable/readable unit.
Expand Down
2 changes: 1 addition & 1 deletion easy3d/util/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace easy3d {
#define EASY3D_VERSION_NR 1020504

/// Easy3D release date, in the format YYYYMMDD.
#define EASY3D_RELEASE_DATE 20240927
#define EASY3D_RELEASE_DATE 20241008


#endif // EASY3D_UTIL_VERSION_H

0 comments on commit a0aebb3

Please sign in to comment.