From 7743a35fe0ba1ed63c4b30c52507587e1de69aee Mon Sep 17 00:00:00 2001
From: Christoph Lehmann <christoph.lehmann@ufz.de>
Date: Thu, 28 Jul 2016 09:45:26 +0200
Subject: [PATCH] [BL] added datetime formatting function

---
 BaseLib/DateTools.cpp | 14 ++++++++++++++
 BaseLib/DateTools.h   | 13 ++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/BaseLib/DateTools.cpp b/BaseLib/DateTools.cpp
index 4ff77230a55..4f2c665fdf8 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 9c1e15dc335..9ef6b0e5cf4 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
-- 
GitLab