nifi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sza...@apache.org
Subject [nifi-minifi-cpp] 01/01: add :format() fallback
Date Tue, 27 Oct 2020 14:58:59 GMT
This is an automated email from the ASF dual-hosted git repository.

szaszm pushed a commit to branch date_format_fallback
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git

commit 61b8aeaf08ff23b7f003ac7c0a912829ea20d8b2
Author: Marton Szasz <szaszm01@gmail.com>
AuthorDate: Tue Oct 27 15:58:08 2020 +0100

    add :format() fallback
---
 extensions/expression-language/Expression.cpp      | 46 +++++++++++++++++++++-
 .../impl/expression/Expression.h                   |  4 +-
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/extensions/expression-language/Expression.cpp b/extensions/expression-language/Expression.cpp
index 6120c20..17f8e9a 100644
--- a/extensions/expression-language/Expression.cpp
+++ b/extensions/expression-language/Expression.cpp
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 
+#include <chrono>
 #include <utility>
 #include <iostream>
 #include <iomanip>
@@ -53,6 +54,8 @@
 
 #ifdef EXPRESSION_LANGUAGE_USE_DATE
 #include "date/tz.h"
+#else
+#include <ctime>
 #endif  // EXPRESSION_LANGUAGE_USE_DATE
 
 namespace org {
@@ -627,6 +630,47 @@ Value expr_toDate(const std::vector<Value> &args) {
   return Value(std::chrono::duration_cast<std::chrono::milliseconds>(zt.get_sys_time().time_since_epoch()).count());
 }
 
+#else
+
+Value expr_format(const std::vector<Value>& args)
+{
+  const std::chrono::milliseconds dur(args.at(0).asUnsignedLong());
+  const std::chrono::time_point<std::chrono::system_clock> dt(dur);
+  const auto unix_time = std::chrono::system_clock::to_time_t(dt);
+  const auto zoned_time = [&args, unix_time] {
+    std::tm buf;
+    if (args.size() > 2 && args[2].asString() == "UTC") {
+#ifdef WIN32
+      const auto err = gmtime_s(&buf, &unix_time);
+#else
+      const std::tm* const not_thread_safe_internal_ptr = gmtime(&unix_time);
+      if (not_thread_safe_internal_ptr) { buf = *not_thread_safe_internal_ptr; }
+      const auto err = errno;
+#endif /* WIN32 */
+      if (err) { throw std::system_error{err, std::generic_category()}; }
+    } else if (args.size() > 2) {
+      throw std::domain_error{"format() with Non-UTC custom timezone is only supported when
compiled with the date.h library"};
+    } else {
+#ifdef WIN32
+      const auto err = localtime_s(&buf, &unix_time);
+#else
+      const std::tm* const not_thread_safe_internal_ptr = localtime(&unix_time);
+      if (not_thread_safe_internal_ptr) { buf = *not_thread_safe_internal_ptr; }
+      const auto err = errno;
+#endif /* WIN32 */
+      if (err) { throw std::system_error{err, std::generic_category()}; }
+    }
+    return buf;
+  }();
+  std::stringstream result_s;
+  result_s << std::put_time(&zoned_time, args.at(1).asString().c_str());
+  return Value(result_s.str());
+}
+
+Value expr_toDate(const std::vector<Value>&) {
+  throw std::domain_error{"toDate() is only supported when compiled with the date.h library"};
+}
+
 #endif  // EXPRESSION_LANGUAGE_USE_DATE
 
 Value expr_now(const std::vector<Value> &args) {
@@ -1490,12 +1534,10 @@ Expression make_dynamic_function(const std::string &function_name,
const std::ve
     return make_count(function_name, args);
   } else if (function_name == "join") {
     return make_join(function_name, args);
-#ifdef EXPRESSION_LANGUAGE_USE_DATE
   } else if (function_name == "format") {
     return make_dynamic_function_incomplete<expr_format>(function_name, args, 1);
   } else if (function_name == "toDate") {
     return make_dynamic_function_incomplete<expr_toDate>(function_name, args, 1);
-#endif  // EXPRESSION_LANGUAGE_USE_DATE
   } else if (function_name == "now") {
     return make_dynamic_function_incomplete<expr_now>(function_name, args, 0);
   } else {
diff --git a/extensions/expression-language/impl/expression/Expression.h b/extensions/expression-language/impl/expression/Expression.h
index c8cb03e..fee299a 100644
--- a/extensions/expression-language/impl/expression/Expression.h
+++ b/extensions/expression-language/impl/expression/Expression.h
@@ -21,14 +21,14 @@
 #define EXPRESSION_LANGUAGE_USE_REGEX
 
 // Disable regex in EL for incompatible compilers
-#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 9)
+#if !defined(WIN32) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__
< 9))
 #undef EXPRESSION_LANGUAGE_USE_REGEX
 #endif
 
 #define EXPRESSION_LANGUAGE_USE_DATE
 
 // Disable date in EL for incompatible compilers
-#if __GNUC__ < 5
+#if defined(WIN32) || __GNUC__ < 5
 #undef EXPRESSION_LANGUAGE_USE_DATE
 #endif
 


Mime
View raw message