From commits-return-43851-archive-asf-public=cust-asf.ponee.io@nifi.apache.org Tue Oct 27 14:59:00 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mxout1-ec2-va.apache.org (mxout1-ec2-va.apache.org [3.227.148.255]) by mx-eu-01.ponee.io (Postfix) with ESMTPS id 33C4F180686 for ; Tue, 27 Oct 2020 15:59:00 +0100 (CET) Received: from mail.apache.org (mailroute1-lw-us.apache.org [207.244.88.153]) by mxout1-ec2-va.apache.org (ASF Mail Server at mxout1-ec2-va.apache.org) with SMTP id 740E14404C for ; Tue, 27 Oct 2020 14:58:59 +0000 (UTC) Received: (qmail 59539 invoked by uid 500); 27 Oct 2020 14:58:59 -0000 Mailing-List: contact commits-help@nifi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@nifi.apache.org Delivered-To: mailing list commits@nifi.apache.org Received: (qmail 59530 invoked by uid 99); 27 Oct 2020 14:58:59 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Oct 2020 14:58:59 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id AD8BB820B2; Tue, 27 Oct 2020 14:58:58 +0000 (UTC) Date: Tue, 27 Oct 2020 14:58:59 +0000 To: "commits@nifi.apache.org" Subject: [nifi-minifi-cpp] 01/01: add :format() fallback MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: szaszm@apache.org In-Reply-To: <160381073854.30763.8360889656513388064@gitbox.apache.org> References: <160381073854.30763.8360889656513388064@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: nifi-minifi-cpp X-Git-Refname: refs/heads/date_format_fallback X-Git-Reftype: branch X-Git-Rev: 61b8aeaf08ff23b7f003ac7c0a912829ea20d8b2 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20201027145858.AD8BB820B2@gitbox.apache.org> 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 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 #include #include #include @@ -53,6 +54,8 @@ #ifdef EXPRESSION_LANGUAGE_USE_DATE #include "date/tz.h" +#else +#include #endif // EXPRESSION_LANGUAGE_USE_DATE namespace org { @@ -627,6 +630,47 @@ Value expr_toDate(const std::vector &args) { return Value(std::chrono::duration_cast(zt.get_sys_time().time_since_epoch()).count()); } +#else + +Value expr_format(const std::vector& args) +{ + const std::chrono::milliseconds dur(args.at(0).asUnsignedLong()); + const std::chrono::time_point 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&) { + 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 &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(function_name, args, 1); } else if (function_name == "toDate") { return make_dynamic_function_incomplete(function_name, args, 1); -#endif // EXPRESSION_LANGUAGE_USE_DATE } else if (function_name == "now") { return make_dynamic_function_incomplete(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