You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/05/26 12:56:34 UTC

[GitHub] [nifi-minifi-cpp] martinzink commented on a diff in pull request #1335: MINIFICPP-1809: custom Cron(quartz syntax) implementation and cron tests

martinzink commented on code in PR #1335:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1335#discussion_r882640735


##########
libminifi/src/utils/Cron.cpp:
##########
@@ -0,0 +1,483 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/Cron.h"
+#include "utils/TimeUtil.h"
+#include "utils/StringUtils.h"
+#include "date/date.h"
+
+using namespace std::literals::chrono_literals;
+
+using std::chrono::seconds;
+using std::chrono::minutes;
+using std::chrono::hours;
+using std::chrono::days;
+
+// TODO(C++20): move to std::chrono when calendar is fully supported
+using date::local_seconds;
+using date::day;
+using date::weekday;
+using date::month;
+using date::year;
+using date::year_month_day;
+using date::last;
+using date::local_days;
+using date::from_stream;
+using date::make_time;
+
+using date::Friday; using date::Saturday; using date::Sunday;
+
+namespace org::apache::nifi::minifi::utils {
+namespace {
+
+bool operator<=(const weekday& lhs, const weekday& rhs) {
+  return lhs.c_encoding() <= rhs.c_encoding();
+}
+
+template <typename FieldType>
+FieldType parse(const std::string&);
+
+template <>
+seconds parse<seconds>(const std::string& second_str) {
+  auto sec_int = std::stoul(second_str);
+  if (sec_int <= 59)
+    return seconds(sec_int);
+  throw BadCronExpression("Invalid second " + second_str);
+}
+
+template <>
+minutes parse<minutes>(const std::string& minute_str) {
+  auto min_int = std::stoul(minute_str);
+  if (min_int <= 59)
+    return minutes(min_int);
+  throw BadCronExpression("Invalid minute " + minute_str);
+}
+
+template <>
+hours parse<hours>(const std::string& hour_str) {
+  auto hour_int = std::stoul(hour_str);
+  if (hour_int <= 23)
+    return hours(hour_int);
+  throw BadCronExpression("Invalid hour " + hour_str);
+}
+
+template <>
+days parse<days>(const std::string& days_str) {
+  return days(std::stoul(days_str));
+}
+
+template <>
+day parse<day>(const std::string& day_str) {
+  auto day_int = std::stoul(day_str);
+  if (day_int >= 1 && day_int <= 31)
+    return day(day_int);
+  throw BadCronExpression("Invalid day " + day_str);
+}
+
+template <>
+month parse<month>(const std::string& month_str) {
+// https://github.com/HowardHinnant/date/issues/550
+// TODO(gcc11): Due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78714
+// the month parsing with '%b' is case sensitive in gcc11
+// This has been fixed in gcc12
+#if defined(__GNUC__) && __GNUC__ < 12
+  auto patched_month_str = StringUtils::toLower(month_str);
+  if (!patched_month_str.empty())
+    patched_month_str[0] = std::toupper(patched_month_str[0]);
+  std::stringstream stream(patched_month_str);
+#else
+  std::stringstream stream(month_str);
+#endif
+
+  stream.imbue(std::locale("en_US.UTF-8"));
+  month parsed_month{};
+  if (month_str.size() > 2) {
+    from_stream(stream, "%b", parsed_month);
+    if (parsed_month.ok())
+      return parsed_month;
+  } else {
+    from_stream(stream, "%m", parsed_month);
+    if (parsed_month.ok())
+      return parsed_month;
+  }
+
+  throw BadCronExpression("Invalid month " + month_str);
+}
+
+template <>
+weekday parse<weekday>(const std::string& weekday_str) {
+  std::stringstream stream(weekday_str);
+  stream.imbue(std::locale("en_US.UTF-8"));
+
+  if (weekday_str.size() > 2) {
+    weekday parsed_weekday{};
+    from_stream(stream, "%a", parsed_weekday);
+    if (parsed_weekday.ok())
+      return parsed_weekday;
+  } else {
+    unsigned weekday_num;
+    stream >> weekday_num;
+    if (!stream.bad() && weekday_num < 7)
+      return weekday(weekday_num-1);

Review Comment:
   The quartz syntax(used in NiFi) is a different than the standard cron.
   In quartz 1 means SUN and 7 means SAT.
   See the examples at http://www.quartz-scheduler.org/documentation/quartz-2.3.0/tutorials/crontrigger.html



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org