diff --git a/BaseLib/DateTools.cpp b/BaseLib/DateTools.cpp index 4ff77230a55c26e25d15459635cc032d25d011d3..4f2c665fdf8f5a4a7fb91925958cd141f8a95956 100644 --- a/BaseLib/DateTools.cpp +++ b/BaseLib/DateTools.cpp @@ -16,6 +16,7 @@ #include <cmath> #include <cstdlib> +#include <ctime> #include <sstream> #include <logog/include/logog.hpp> @@ -114,4 +115,17 @@ int xmlDate2int(const std::string &s) } return 0; } + +std::string formatDate( + std::chrono::time_point<std::chrono::system_clock> const& time) +{ + auto const time_t = std::chrono::system_clock::to_time_t(time); + char time_str[100]; + if (std::strftime(time_str, sizeof(time_str), "%F %T%z", + std::localtime(&time_t))) { + return time_str; + } else { + return "FAILED FORMATTING THE GIVEN TIME POINT."; + } +} } // end namespace BaseLib diff --git a/BaseLib/DateTools.h b/BaseLib/DateTools.h index 9c1e15dc3358e52bb2c913ebd27500f37b2faeb6..9ef6b0e5cf4cd67f661b00330071ac4ddffd822b 100644 --- a/BaseLib/DateTools.h +++ b/BaseLib/DateTools.h @@ -15,9 +15,11 @@ #ifndef DATETOOLS_H #define DATETOOLS_H +#include <chrono> #include <string> -namespace BaseLib { +namespace BaseLib +{ /** * Converts three integers representing a date into a double. @@ -55,6 +57,15 @@ int strDate2int(const std::string &s); */ int xmlDate2int(const std::string &s); +/** + * Formats the given time point according to RFC 3339 (cf. man-page of the unix + * date utility). + * + * Example: 2006-08-14 02:34:56-06:00 + */ +std::string formatDate( + std::chrono::time_point<std::chrono::system_clock> const& time); + } // namespace BaseLib #endif //DATETOOLS_H