This is an automated email from the ASF dual-hosted git repository.

exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/main by this push:
     new 56dd63c580 NIFI-14356 Fixed missing YearMonth and Year Handling for 
Expression Language (#9796)
56dd63c580 is described below

commit 56dd63c58066b8be644061f60b7cdd59ec35315a
Author: Gorbasch <[email protected]>
AuthorDate: Wed Mar 12 03:30:28 2025 +0100

    NIFI-14356 Fixed missing YearMonth and Year Handling for Expression 
Language (#9796)
    
    Signed-off-by: David Handermann <[email protected]>
---
 .../nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java  | 6 +++++-
 .../src/test/java/org/apache/nifi/util/TestFormatUtils.java         | 6 ++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git 
a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java 
b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java
index 27022c731b..7808558c16 100644
--- 
a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java
+++ 
b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/FormatUtils.java
@@ -24,6 +24,8 @@ import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
+import java.time.Year;
+import java.time.YearMonth;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
@@ -242,11 +244,13 @@ public class FormatUtils {
             throw new IllegalArgumentException("Text cannot be null");
         }
 
-        TemporalAccessor parsed = formatter.parseBest(text, Instant::from, 
LocalDateTime::from, LocalDate::from, LocalTime::from);
+        TemporalAccessor parsed = formatter.parseBest(text, Instant::from, 
LocalDateTime::from, LocalDate::from, YearMonth::from, Year::from, 
LocalTime::from);
         return switch (parsed) {
             case Instant instant -> instant;
             case LocalDateTime localDateTime -> 
toInstantInSystemDefaultTimeZone(localDateTime);
             case LocalDate localDate -> 
toInstantInSystemDefaultTimeZone(localDate.atTime(0, 0));
+            case YearMonth yearMonth -> 
toInstantInSystemDefaultTimeZone(yearMonth.atDay(1).atTime(0, 0));
+            case Year year -> 
toInstantInSystemDefaultTimeZone(year.atDay(1).atTime(0, 0));
             case null, default -> 
toInstantInSystemDefaultTimeZone(((LocalTime) 
parsed).atDate(EPOCH_INITIAL_DATE));
         };
     }
diff --git 
a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestFormatUtils.java
 
b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestFormatUtils.java
index 4fbba4fa95..2c5081377e 100644
--- 
a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestFormatUtils.java
+++ 
b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/TestFormatUtils.java
@@ -48,16 +48,22 @@ public class TestFormatUtils {
         return Stream.of(/*GMT-*/
             Arguments.of("yyyy-MM-dd HH:mm:ss", "2020-01-01 02:00:00", 
NEW_YORK_TIME_ZONE_ID, "2020-01-01T07:00:00"),
             Arguments.of("yyyy-MM-dd", "2020-01-01", NEW_YORK_TIME_ZONE_ID, 
"2020-01-01T05:00:00"),
+            Arguments.of("yyyy-MM", "2020-01", NEW_YORK_TIME_ZONE_ID, 
"2020-01-01T05:00:00"),
+            Arguments.of("yyyy", "2020", NEW_YORK_TIME_ZONE_ID, 
"2020-01-01T05:00:00"),
             Arguments.of("HH:mm:ss", "03:00:00", NEW_YORK_TIME_ZONE_ID, 
"1970-01-01T08:00:00"),
             Arguments.of("yyyy-MMM-dd", "2020-may-01", NEW_YORK_TIME_ZONE_ID, 
"2020-05-01T04:00:00"),
             /*GMT+*/
             Arguments.of("yyyy-MM-dd HH:mm:ss", "2020-01-01 02:00:00", 
KIEV_TIME_ZONE_ID, "2020-01-01T00:00:00"),
             Arguments.of("yyyy-MM-dd", "2020-01-01", KIEV_TIME_ZONE_ID, 
"2019-12-31T22:00:00"),
+            Arguments.of("yyyy-MM", "2020-01", KIEV_TIME_ZONE_ID, 
"2019-12-31T22:00:00"),
+            Arguments.of("yyyy", "2020", KIEV_TIME_ZONE_ID, 
"2019-12-31T22:00:00"),
             Arguments.of("HH:mm:ss", "03:00:00", KIEV_TIME_ZONE_ID, 
"1970-01-01T00:00:00"),
             Arguments.of("yyyy-MMM-dd", "2020-may-01", KIEV_TIME_ZONE_ID, 
"2020-04-30T21:00:00"),
             /*UTC*/
             Arguments.of("yyyy-MM-dd HH:mm:ss", "2020-01-01 02:00:00", 
ZoneOffset.UTC.getId(), "2020-01-01T02:00:00"),
             Arguments.of("yyyy-MM-dd", "2020-01-01", ZoneOffset.UTC.getId(), 
"2020-01-01T00:00:00"),
+            Arguments.of("yyyy-MM", "2020-01", ZoneOffset.UTC.getId(), 
"2020-01-01T00:00:00"),
+            Arguments.of("yyyy", "2020", ZoneOffset.UTC.getId(), 
"2020-01-01T00:00:00"),
             Arguments.of("HH:mm:ss", "03:00:00", ZoneOffset.UTC.getId(), 
"1970-01-01T03:00:00"),
             Arguments.of("yyyy-MMM-dd", "2020-may-01", ZoneOffset.UTC.getId(), 
"2020-05-01T00:00:00"));
     }

Reply via email to