This is an automated email from the ASF dual-hosted git repository.
SbloodyS pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new c7dd30f04f [Feature-16510][TaskPlugin] Support month and week boundary
placeholders with date offset (#18726)
c7dd30f04f is described below
commit c7dd30f04f640ce5e7e89786efb7142e5746be76
Author: Ethan <[email protected]>
AuthorDate: Fri Oct 23 15:31:44 2026 +0800
[Feature-16510][TaskPlugin] Support month and week boundary placeholders
with date offset (#18726)
---
docs/docs/en/guide/parameter/built-in.md | 2 +
docs/docs/zh/guide/parameter/built-in.md | 2 +
.../task/api/parser/TimePlaceholderUtils.java | 244 +++++++++++++++++----
.../task/api/parser/TimePlaceholderUtilsTest.java | 217 ++++++++++++++++++
4 files changed, 422 insertions(+), 43 deletions(-)
diff --git a/docs/docs/en/guide/parameter/built-in.md
b/docs/docs/en/guide/parameter/built-in.md
index a2c50dc0a8..f30ab6ea4e 100644
--- a/docs/docs/en/guide/parameter/built-in.md
+++ b/docs/docs/en/guide/parameter/built-in.md
@@ -49,4 +49,6 @@
- Before(-)/After(+) The last day of Month(The unit of N is month):
`$[month_last_day(yyyy-MM-dd,-N)]`,Such as when N=1, 2022-08-28 => 2022-07-31
- Before(-)/After(+) The first day of Week(The unit of N is week):
`$[week_first_day(yyyy-MM-dd,-N)]`,Such as when N=1, 2022-08-26 => 2022-08-15
- Before(-)/After(+) The last day of Week(The unit of N is week):
`$[week_last_day(yyyy-MM-dd,-N)]`, Such as when N=1, 2022-08-26 => 2022-08-21
+ - The first day of Month for date offset:
`$[month_first_day(yyyy-MM-dd-1)]`, Such as 2022-09-01 => 2022-08-01
+ - The date format supports timezone offsets and quoted literals: e.g.
`$[month_first_day(yyyy-MM-dd+08:00)]` => 2022-08-01+08:00,
`$[this_day(yyyy-MM-dd'T'HH:mm:ss+08:00' (CST)')]` => 2022-08-26T00:00:00+08:00
(CST)
diff --git a/docs/docs/zh/guide/parameter/built-in.md
b/docs/docs/zh/guide/parameter/built-in.md
index dd0c1afab7..b8ab2619f5 100644
--- a/docs/docs/zh/guide/parameter/built-in.md
+++ b/docs/docs/zh/guide/parameter/built-in.md
@@ -55,4 +55,6 @@
* 前(-)/后(+) N 月最后一天:$[month_last_day(yyyy-MM-dd,-N)],如:N=1时 2022-08-28 =>
2022-07-31
* 前(-)/后(+) N 周的周一:$[week_first_day(yyyy-MM-dd,-N)],如:N=1 2022-08-26 =>
2022-08-15
* 前(-)/后(+) N 周的周日:$[week_last_day(yyyy-MM-dd,-N)],如:N=1 2022-08-26 =>
2022-08-21
+ * 前一天所在月的第一天:$[month_first_day(yyyy-MM-dd-1)],如:2022-09-01 => 2022-08-01
+ * 日期格式支持时区偏移和引号字面量:如 $[month_first_day(yyyy-MM-dd+08:00)] =>
2022-08-01+08:00,$[this_day(yyyy-MM-dd'T'HH:mm:ss+08:00' (CST)')] =>
2022-08-26T00:00:00+08:00 (CST)
diff --git
a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java
b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java
index fc72120b48..af2e698f8e 100644
---
a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java
+++
b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java
@@ -62,6 +62,7 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Stack;
+import java.util.regex.Pattern;
import lombok.extern.slf4j.Slf4j;
@@ -306,16 +307,16 @@ public class TimePlaceholderUtils {
String targetDate = "";
try {
-
if (dataFormat.contains(COMMA)) {
String param1 = dataFormat.split(COMMA)[0];
String param2 = dataFormat.split(COMMA)[1];
dataFormat = param1;
- targetDate = transformYearWeek(date, dataFormat,
calculate(param2));
-
+ Map.Entry<Date, String> baseEntry =
calcFormatOffset(dataFormat, date);
+ targetDate = transformYearWeek(baseEntry.getKey(),
baseEntry.getValue(), calculate(param2));
} else {
- targetDate = transformYearWeek(date, dataFormat, 1);
+ Map.Entry<Date, String> baseEntry =
calcFormatOffset(dataFormat, date);
+ targetDate = transformYearWeek(baseEntry.getKey(),
baseEntry.getValue(), 1);
}
} catch (Exception e) {
throw new RuntimeException("expression not valid");
@@ -447,11 +448,15 @@ public class TimePlaceholderUtils {
if (params.length == 2) {
String dateFormat = params[0];
String dayExpr = params[1];
+ Map.Entry<Date, String> baseEntry = calcFormatOffset(dateFormat,
date);
+ Date baseDate = baseEntry.getKey();
+ String cleanFormat = baseEntry.getValue();
+
Integer day = calculate(dayExpr);
- Date targetDate = DateUtils.getFirstDayOfMonth(date);
+ Date targetDate = DateUtils.getFirstDayOfMonth(baseDate);
targetDate = addDays(targetDate, day);
- return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
+ return new AbstractMap.SimpleImmutableEntry<>(targetDate,
cleanFormat);
}
throw new RuntimeException("expression not valid");
@@ -471,11 +476,15 @@ public class TimePlaceholderUtils {
if (params.length == 2) {
String dateFormat = params[0];
String dayExpr = params[1];
+ Map.Entry<Date, String> baseEntry = calcFormatOffset(dateFormat,
date);
+ Date baseDate = baseEntry.getKey();
+ String cleanFormat = baseEntry.getValue();
+
Integer day = calculate(dayExpr);
- Date targetDate = DateUtils.getLastDayOfMonth(date);
+ Date targetDate = DateUtils.getLastDayOfMonth(baseDate);
targetDate = addDays(targetDate, day);
- return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
+ return new AbstractMap.SimpleImmutableEntry<>(targetDate,
cleanFormat);
}
throw new RuntimeException("expression not valid");
@@ -495,66 +504,89 @@ public class TimePlaceholderUtils {
switch (keyDate) {
case MONTH_FIRST_DAY:
dataFormat = expression.substring(MONTH_FIRST_DAY.length() +
1, expression.length() - 1);
-
+ String monthFirstOffset = null;
if (dataFormat.contains(COMMA)) {
- String param1 = dataFormat.split(COMMA)[0];
- String param2 = dataFormat.split(COMMA)[1];
- dataFormat = param1;
+ String[] parts = dataFormat.split(COMMA);
+ dataFormat = parts[0];
+ monthFirstOffset = parts[1];
+ }
+ Map.Entry<Date, String> monthFirstBaseEntry =
calcFormatOffset(dataFormat, date);
+ Date monthFirstBaseDate = monthFirstBaseEntry.getKey();
+ dataFormat = monthFirstBaseEntry.getValue();
- targetDate = addMonths(DateUtils.getFirstDayOfMonth(date),
calculate(param2));
+ if (monthFirstOffset != null) {
+ targetDate =
+
addMonths(DateUtils.getFirstDayOfMonth(monthFirstBaseDate),
calculate(monthFirstOffset));
} else {
- targetDate = DateUtils.getFirstDayOfMonth(date);
+ targetDate =
DateUtils.getFirstDayOfMonth(monthFirstBaseDate);
}
break;
case MONTH_LAST_DAY:
dataFormat = expression.substring(MONTH_LAST_DAY.length() + 1,
expression.length() - 1);
-
+ String monthLastOffset = null;
if (dataFormat.contains(COMMA)) {
- String param1 = dataFormat.split(COMMA)[0];
- String param2 = dataFormat.split(COMMA)[1];
- dataFormat = param1;
-
- Date lastMonthDay = addMonths(date, calculate(param2));
+ String[] parts = dataFormat.split(COMMA);
+ dataFormat = parts[0];
+ monthLastOffset = parts[1];
+ }
+ Map.Entry<Date, String> monthLastBaseEntry =
calcFormatOffset(dataFormat, date);
+ Date monthLastBaseDate = monthLastBaseEntry.getKey();
+ dataFormat = monthLastBaseEntry.getValue();
+ if (monthLastOffset != null) {
+ Date lastMonthDay = addMonths(monthLastBaseDate,
calculate(monthLastOffset));
targetDate = DateUtils.getLastDayOfMonth(lastMonthDay);
-
} else {
- targetDate = DateUtils.getLastDayOfMonth(date);
+ targetDate =
DateUtils.getLastDayOfMonth(monthLastBaseDate);
}
break;
case THIS_DAY:
dataFormat = expression.substring(THIS_DAY.length() + 1,
expression.length() - 1);
- targetDate = addDays(date, 0);
+ Map.Entry<Date, String> thisDayBaseEntry =
calcFormatOffset(dataFormat, date);
+ targetDate = addDays(thisDayBaseEntry.getKey(), 0);
+ dataFormat = thisDayBaseEntry.getValue();
break;
case LAST_DAY:
dataFormat = expression.substring(LAST_DAY.length() + 1,
expression.length() - 1);
- targetDate = addDays(date, -1);
+ Map.Entry<Date, String> lastDayBaseEntry =
calcFormatOffset(dataFormat, date);
+ targetDate = addDays(lastDayBaseEntry.getKey(), -1);
+ dataFormat = lastDayBaseEntry.getValue();
break;
case WEEK_FIRST_DAY:
dataFormat = expression.substring(WEEK_FIRST_DAY.length() + 1,
expression.length() - 1);
-
+ String weekFirstOffset = null;
if (dataFormat.contains(COMMA)) {
- String param1 = dataFormat.split(COMMA)[0];
- String param2 = dataFormat.split(COMMA)[1];
- dataFormat = param1;
+ String[] parts = dataFormat.split(COMMA);
+ dataFormat = parts[0];
+ weekFirstOffset = parts[1];
+ }
+ Map.Entry<Date, String> weekFirstBaseEntry =
calcFormatOffset(dataFormat, date);
+ Date weekFirstBaseDate = weekFirstBaseEntry.getKey();
+ dataFormat = weekFirstBaseEntry.getValue();
- targetDate = addWeeks(DateUtils.getMonday(date),
calculate(param2));
+ if (weekFirstOffset != null) {
+ targetDate =
addWeeks(DateUtils.getMonday(weekFirstBaseDate), calculate(weekFirstOffset));
} else {
- targetDate = addWeeks(DateUtils.getMonday(date), 0);
+ targetDate =
addWeeks(DateUtils.getMonday(weekFirstBaseDate), 0);
}
break;
case WEEK_LAST_DAY:
dataFormat = expression.substring(WEEK_LAST_DAY.length() + 1,
expression.length() - 1);
-
+ String weekLastOffset = null;
if (dataFormat.contains(COMMA)) {
- String param1 = dataFormat.split(COMMA)[0];
- String param2 = dataFormat.split(COMMA)[1];
- dataFormat = param1;
+ String[] parts = dataFormat.split(COMMA);
+ dataFormat = parts[0];
+ weekLastOffset = parts[1];
+ }
+ Map.Entry<Date, String> weekLastBaseEntry =
calcFormatOffset(dataFormat, date);
+ Date weekLastBaseDate = weekLastBaseEntry.getKey();
+ dataFormat = weekLastBaseEntry.getValue();
- targetDate = addWeeks(DateUtils.getSunday(date),
calculate(param2));
+ if (weekLastOffset != null) {
+ targetDate =
addWeeks(DateUtils.getSunday(weekLastBaseDate), calculate(weekLastOffset));
} else {
- targetDate = addWeeks(DateUtils.getSunday(date), 0);
+ targetDate =
addWeeks(DateUtils.getSunday(weekLastBaseDate), 0);
}
break;
default:
@@ -579,10 +611,14 @@ public class TimePlaceholderUtils {
if (params.length == 2) {
String dateFormat = params[0];
String dayExpr = params[1];
+ Map.Entry<Date, String> baseEntry = calcFormatOffset(dateFormat,
date);
+ Date baseDate = baseEntry.getKey();
+ String cleanFormat = baseEntry.getValue();
+
Integer day = calculate(dayExpr);
- Date targetDate = DateUtils.getMonday(date);
+ Date targetDate = DateUtils.getMonday(baseDate);
targetDate = addDays(targetDate, day);
- return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
+ return new AbstractMap.SimpleImmutableEntry<>(targetDate,
cleanFormat);
}
throw new RuntimeException("expression not valid");
@@ -602,11 +638,15 @@ public class TimePlaceholderUtils {
if (params.length == 2) {
String dateFormat = params[0];
String dayExpr = params[1];
+ Map.Entry<Date, String> baseEntry = calcFormatOffset(dateFormat,
date);
+ Date baseDate = baseEntry.getKey();
+ String cleanFormat = baseEntry.getValue();
+
Integer day = calculate(dayExpr);
- Date targetDate = DateUtils.getSunday(date);
+ Date targetDate = DateUtils.getSunday(baseDate);
targetDate = addDays(targetDate, day);
- return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
+ return new AbstractMap.SimpleImmutableEntry<>(targetDate,
cleanFormat);
}
throw new RuntimeException("Expression not valid");
@@ -626,17 +666,33 @@ public class TimePlaceholderUtils {
if (params.length == 2) {
String dateFormat = params[0];
String monthExpr = params[1];
+ Map.Entry<Date, String> baseEntry = calcFormatOffset(dateFormat,
date);
+ Date baseDate = baseEntry.getKey();
+ String cleanFormat = baseEntry.getValue();
+
Integer addMonth = calculate(monthExpr);
- Date targetDate = addMonths(date, addMonth);
+ Date targetDate = addMonths(baseDate, addMonth);
- return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
+ return new AbstractMap.SimpleImmutableEntry<>(targetDate,
cleanFormat);
}
throw new RuntimeException("expression not valid");
}
+ /**
+ * Matches a timezone offset literal appended to the date format argument
of a boundary function,
+ * such as 0800 in month_first_day(yyyy-MM-dd+0800) or 08:00 in
month_first_day(yyyy-MM-dd+08:00).
+ * Timezone offsets range from UTC-12:00 to UTC+14:00, so only hours up to
14 with minutes up to
+ * 59 are accepted. Standard placeholders are not affected by this
pattern: their operands always
+ * keep the legacy numeric day-offset semantics.
+ */
+ private static final Pattern TIMEZONE_PATTERN =
+
Pattern.compile("^((?:0\\d|1[0-4])[0-5]\\d|(?:0\\d|1[0-4]):[0-5]\\d)$");
+
/**
* calculate time expression
+ * <p> Keeps the legacy semantics of standard placeholders such as
$[yyyyMMdd+1400]: the operand
+ * after the last '+'/'-' sign is always evaluated as a numeric offset
expression.
*
* @param expression expresstion
* @param date date
@@ -664,7 +720,7 @@ public class TimePlaceholderUtils {
return new AbstractMap.SimpleImmutableEntry<>(targetDate,
dateFormat);
}
- // yyyy-MM-dd/HH:mm:ss
+ // yyyy-MM-dd
return new AbstractMap.SimpleImmutableEntry<>(date, expression);
}
@@ -672,6 +728,108 @@ public class TimePlaceholderUtils {
return new AbstractMap.SimpleImmutableEntry<>(date, expression);
}
+ /**
+ * Resolves the date format argument of a boundary function, e.g.
month_first_day(yyyy-MM-dd-1):
+ * when the format carries a trailing offset expression the base date is
shifted and the offset is
+ * stripped from the returned format. Tails that are not arithmetic
offsets - timezone literals
+ * such as +0800 or +08:00, or quoted literal suffixes such as +08:00'
(CST)' - stay in the format.
+ *
+ * @param format date format, possibly with a trailing offset
+ * @param date date
+ * @return base date after applying the offset, and the date format
without the offset
+ */
+ private static Map.Entry<Date, String> calcFormatOffset(String format,
Date date) {
+ int opIndex = findFormatOffsetIndex(format);
+ if (opIndex == -1) {
+ return new AbstractMap.SimpleImmutableEntry<>(date, format);
+ }
+
+ char op = format.charAt(opIndex);
+ String addMinuteExpr = format.substring(opIndex + 1);
+ String dateFormat = format.substring(0, opIndex);
+
+ // arithmetic failures are propagated, formatTimeExpression decides
how to handle them
+ int minutes = calcMinutes(addMinuteExpr);
+ Date targetDate = (op == '+') ? addMinutes(date, minutes) :
addMinutes(date, -minutes);
+ return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat);
+ }
+
+ /**
+ * Finds the index of the arithmetic operator ('+' or '-') that separates
the date format from a
+ * complete trailing offset expression, ignoring operators inside
single-quoted literals. The
+ * boundary is the first sign whose entire remainder is an arithmetic
expression: timezone
+ * literals such as +08:00 or +0800 fail the arithmetic check and stay
part of the format, while
+ * unary signs inside the offset (e.g. the '-' in yyyy-MM-dd-7*-1) remain
part of the operand.
+ *
+ * @param format date format, possibly with a trailing offset
+ * @return index of operator, or -1 when the format has no trailing offset
+ */
+ private static int findFormatOffsetIndex(String format) {
+ if (StringUtils.isEmpty(format)) {
+ return -1;
+ }
+
+ boolean inQuote = false;
+ for (int i = 0; i < format.length(); i++) {
+ char c = format.charAt(i);
+ if (c == '\'') {
+ if (i + 1 < format.length() && format.charAt(i + 1) == '\'') {
+ // escaped quote ('') inside a literal
+ i++;
+ } else {
+ inQuote = !inQuote;
+ }
+ } else if (!inQuote && (c == '+' || c == '-') && i + 1 <
format.length()
+ && Character.isDigit(format.charAt(i + 1))) {
+ String operand = format.substring(i + 1).trim();
+ // a tail that cannot be arithmetic (e.g. +08:00' (CST)'), a
timezone offset literal
+ // (e.g. +0800), or an arithmetic tail starting with a
timezone literal (e.g. 0800-1)
+ // belongs to the date format rather than to an offset
+ if (isArithmeticExpression(operand) &&
!isTimezoneOffsetLiteral(operand)
+ && !startsWithTimezoneOffsetLiteral(operand)) {
+ return i;
+ }
+ }
+ }
+
+ return -1;
+ }
+
+ /**
+ * @param operand candidate offset expression
+ * @return whether the operand consists only of arithmetic characters
(digits, operators,
+ * parentheses and whitespace)
+ */
+ private static boolean isArithmeticExpression(String operand) {
+ for (int i = 0; i < operand.length(); i++) {
+ char c = operand.charAt(i);
+ if (!Character.isDigit(c) && c != '+' && c != '-' && c != '*' && c
!= '/' && c != '(' && c != ')'
+ && !Character.isWhitespace(c)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static boolean isTimezoneOffsetLiteral(String operand) {
+ if (StringUtils.isEmpty(operand)) {
+ return false;
+ }
+ return TIMEZONE_PATTERN.matcher(operand).matches();
+ }
+
+ /**
+ * @param operand candidate offset expression
+ * @return whether the operand is a timezone offset literal immediately
followed by another sign,
+ * e.g. the 0800-1 in +0800-1, so the sign belongs to the date
format rather than to an
+ * arithmetic offset. Continuations of the same arithmetic
expression, such as the /
+ * in 1000/24/60 or the digits of 10000, are complete operands and
are not affected.
+ */
+ private static boolean startsWithTimezoneOffsetLiteral(String operand) {
+ return operand.length() > 4 &&
TIMEZONE_PATTERN.matcher(operand.substring(0, 4)).matches()
+ && (operand.charAt(4) == '+' || operand.charAt(4) == '-');
+ }
+
/**
* calculate need minutes
*
diff --git
a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java
b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java
index 86d69a03c7..291102666a 100644
---
a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java
+++
b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java
@@ -18,6 +18,7 @@
package org.apache.dolphinscheduler.plugin.task.api.parser;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.utils.DateUtils;
@@ -129,9 +130,225 @@ public class TimePlaceholderUtilsTest {
assertEquals(weekLastDayTime, "20220828");
}
+ @Test
+ public void timePlaceHolderForMonthFirstDayWithDateOffset() {
+ // Business date: 2022-08-26 (Friday)
+ // Yesterday is 2022-08-25, which is in August 2022. First day of
yesterday's month is 2022-08-01
+ String monthFirstDate = "$[month_first_day(yyyy-MM-dd-1)]";
+ String monthFirstDay = "$[month_first_day(yyyyMMdd-1)]";
+ assertEquals("2022-08-01",
ParameterUtils.convertParameterPlaceholders(monthFirstDate, timeParams));
+ assertEquals("20220801",
ParameterUtils.convertParameterPlaceholders(monthFirstDay, timeParams));
+
+ String monthFirstDateWithMonthOffset =
"$[month_first_day(yyyy-MM-dd-1,0)]";
+ assertEquals("2022-08-01",
+
ParameterUtils.convertParameterPlaceholders(monthFirstDateWithMonthOffset,
timeParams));
+
+ String monthFirstDateLastMonth = "$[month_first_day(yyyy-MM-dd-1,-1)]";
+ assertEquals("2022-07-01",
ParameterUtils.convertParameterPlaceholders(monthFirstDateLastMonth,
timeParams));
+
+ // Test across month boundary: on 2022-09-01, yesterday is 2022-08-31
+ Date septFirst = DateUtils.parse("2022-09-01 00:00:00", "yyyy-MM-dd
HH:mm:ss");
+ Map<String, String> septParams =
+ BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA,
septFirst, null);
+ assertEquals("2022-08-01",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd-1)]",
septParams));
+ assertEquals("2022-08-01",
+
ParameterUtils.convertParameterPlaceholders("$[month_begin(yyyy-MM-dd-1,0)]",
septParams));
+ assertEquals("2022-08-31",
+
ParameterUtils.convertParameterPlaceholders("$[month_last_day(yyyy-MM-dd-1)]",
septParams));
+ assertEquals("2022-08-31",
+
ParameterUtils.convertParameterPlaceholders("$[month_end(yyyy-MM-dd-1,0)]",
septParams));
+
+ // On 2022-09-02, yesterday is 2022-09-01
+ Date septSecond = DateUtils.parse("2022-09-02 00:00:00", "yyyy-MM-dd
HH:mm:ss");
+ Map<String, String> septSecondParams =
+ BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA,
septSecond, null);
+ assertEquals("2022-09-01",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd-1)]",
septSecondParams));
+ assertEquals("2022-09-01",
+
ParameterUtils.convertParameterPlaceholders("$[month_begin(yyyy-MM-dd-1,0)]",
septSecondParams));
+ assertEquals("2022-09-30",
+
ParameterUtils.convertParameterPlaceholders("$[month_last_day(yyyy-MM-dd-1)]",
septSecondParams));
+ assertEquals("2022-09-30",
+
ParameterUtils.convertParameterPlaceholders("$[month_end(yyyy-MM-dd-1,0)]",
septSecondParams));
+
+ // Week tests with date offset:
+ // On 2022-08-29 (Monday), yesterday is 2022-08-28 (Sunday of previous
week)
+ Date aug29 = DateUtils.parse("2022-08-29 00:00:00", "yyyy-MM-dd
HH:mm:ss");
+ Map<String, String> aug29Params =
+ BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA,
aug29, null);
+ assertEquals("2022-08-22",
+
ParameterUtils.convertParameterPlaceholders("$[week_first_day(yyyy-MM-dd-1)]",
aug29Params));
+ assertEquals("2022-08-28",
+
ParameterUtils.convertParameterPlaceholders("$[week_last_day(yyyy-MM-dd-1)]",
aug29Params));
+ assertEquals("2022-08-22",
+
ParameterUtils.convertParameterPlaceholders("$[week_begin(yyyy-MM-dd-1,0)]",
aug29Params));
+ assertEquals("2022-08-28",
+
ParameterUtils.convertParameterPlaceholders("$[week_end(yyyy-MM-dd-1,0)]",
aug29Params));
+
+ // add_months with date offset:
+ assertEquals("2022-09-25",
+
ParameterUtils.convertParameterPlaceholders("$[add_months(yyyy-MM-dd-1,1)]",
timeParams));
+
+ // Preserve date-format literals and quoted literals:
+ assertEquals("2022-08-01+0800",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd+0800)]",
timeParams));
+ assertEquals("2022-08-01+0800",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd'+0800')]",
timeParams));
+ assertEquals("2022-08-01+08:00",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd+08:00)]",
timeParams));
+ assertEquals("2022-08-01+08:00",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd'+08:00')]",
timeParams));
+ assertEquals("2022-08-01-0500",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd-0500)]",
timeParams));
+ assertEquals("2022-08-01-0500",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd'-0500')]",
timeParams));
+ assertEquals("2022-08-01T00:00:00",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd'T'HH:mm:ss)]",
timeParams));
+
+ assertEquals("2022-08-01+0800",
+
ParameterUtils.convertParameterPlaceholders("$[month_begin(yyyy-MM-dd'+0800',0)]",
timeParams));
+ assertEquals("2022-08-22+0800",
+
ParameterUtils.convertParameterPlaceholders("$[week_first_day(yyyy-MM-dd'+0800')]",
timeParams));
+ assertEquals("2022-09-26+0800",
+
ParameterUtils.convertParameterPlaceholders("$[add_months(yyyy-MM-dd'+0800',1)]",
timeParams));
+
+ // Standard placeholders keep the legacy numeric offset semantics:
timezone-shaped operands
+ // are evaluated as day offsets, and quoted literals that fail
arithmetic parsing or date
+ // formatting leave the placeholder unresolved
+ assertEquals("2024-11-03",
+
ParameterUtils.convertParameterPlaceholders("$[yyyy-MM-dd+0800]", timeParams));
+ assertEquals("$[yyyy-MM-dd'+0800']",
+
ParameterUtils.convertParameterPlaceholders("$[yyyy-MM-dd'+0800']",
timeParams));
+ assertEquals("2021-04-13",
+
ParameterUtils.convertParameterPlaceholders("$[yyyy-MM-dd-0500]", timeParams));
+ assertEquals("$[yyyy-MM-dd'-0500']",
+
ParameterUtils.convertParameterPlaceholders("$[yyyy-MM-dd'-0500']",
timeParams));
+ }
+
@Test
void formatTimeExpressionWithInvalidExpression() {
assertEquals("$[week_last_day(yyyy-MM-dd,0) - 1]",
TimePlaceholderUtils.formatTimeExpression("$[week_last_day(yyyy-MM-dd,0) - 1]",
new Date(), true));
}
+
+ @Test
+ public void timePlaceHolderWithFourDigitDayOffset() {
+ // Standard placeholders keep the legacy numeric day-offset semantics
for any operand,
+ // including timezone-shaped four-digit values such as +1400
+ assertEquals("20120828",
ParameterUtils.convertParameterPlaceholders("$[yyyyMMdd-3650]", timeParams));
+ assertEquals("20320823",
ParameterUtils.convertParameterPlaceholders("$[yyyyMMdd+3650]", timeParams));
+ assertEquals("20260626",
ParameterUtils.convertParameterPlaceholders("$[yyyyMMdd+1400]", timeParams));
+ }
+
+ @Test
+ public void timePlaceHolderWithUnaryMinusInOffsetExpression() {
+ // The boundary sign only starts the offset expression: unary signs
and parenthesized
+ // arithmetic inside the operand stay part of the arithmetic expression
+ assertEquals("20220819",
ParameterUtils.convertParameterPlaceholders("$[yyyyMMdd+7*-1]", timeParams));
+ assertEquals("20220819",
ParameterUtils.convertParameterPlaceholders("$[yyyyMMdd+7*(-1)]", timeParams));
+ }
+
+ @Test
+ public void timePlaceHolderWithArithmeticFailure() {
+ // ignore-invalid mode preserves the original placeholder
+ assertEquals("$[yyyyMMdd+1/0]",
+ ParameterUtils.convertParameterPlaceholders("$[yyyyMMdd+1/0]",
timeParams));
+ assertEquals("yyyyMMdd+1/0",
+ TimePlaceholderUtils.formatTimeExpression("yyyyMMdd+1/0",
date, true));
+ // strict mode rejects the expression
+ assertThrows(IllegalArgumentException.class,
+ () ->
TimePlaceholderUtils.formatTimeExpression("yyyyMMdd+1/0", date, false));
+ }
+
+ @Test
+ public void timePlaceHolderWithTrailingFormatLiteralSuffix() {
+ // Boundary-function format arguments keep tails that are not
arithmetic offsets, such as
+ // a timezone suffix followed by a quoted zone name
+ assertEquals("2022-08-26T00:00:00+08:00 (CST)",
+ ParameterUtils.convertParameterPlaceholders(
+ "$[this_day(yyyy-MM-dd'T'HH:mm:ss+08:00' (CST)')]",
timeParams));
+ assertEquals("2022-08-01T00:00:00+08:00 (CST)",
+ ParameterUtils.convertParameterPlaceholders(
+ "$[month_first_day(yyyy-MM-dd'T'HH:mm:ss+08:00'
(CST)')]", timeParams));
+ }
+
+ @Test
+ public void timePlaceHolderWithBoundaryOffsetAfterTimezoneLiteral() {
+ // The offset boundary is the complete trailing arithmetic operand: a
timezone literal
+ // followed by an offset keeps both
+ Date septFirst = DateUtils.parse("2022-09-01 00:00:00", "yyyy-MM-dd
HH:mm:ss");
+ Map<String, String> septFirstParams =
+ BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA,
septFirst, null);
+ // colon timezone + negative offset
+ assertEquals("2022-08-01T00:00:00+08:00",
+ ParameterUtils.convertParameterPlaceholders(
+ "$[month_first_day(yyyy-MM-dd'T'HH:mm:ss+08:00-1)]",
septFirstParams));
+ // colon timezone + positive offset
+ assertEquals("2022-09-01T00:00:00+08:00",
+ ParameterUtils.convertParameterPlaceholders(
+ "$[month_first_day(yyyy-MM-dd'T'HH:mm:ss+08:00+1)]",
septFirstParams));
+ // compact timezone + offset: the 0800 prefix is a timezone literal,
not the start of the
+ // arithmetic operand
+ assertEquals("20220801+0800",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyyMMdd+0800-1)]",
+ septFirstParams));
+ // quoted zone-name suffix + offset
+ assertEquals("2022-08-01+08:00 (CST)",
+ ParameterUtils.convertParameterPlaceholders(
+ "$[month_first_day(yyyy-MM-dd+08:00' (CST)'-1)]",
septFirstParams));
+ }
+
+ @Test
+ public void timePlaceHolderWithUnaryMinusInBoundaryOffset() {
+ // Unary signs and parenthesized arithmetic inside a boundary-function
offset stay part
+ // of the operand
+ Date septFirst = DateUtils.parse("2022-09-01 00:00:00", "yyyy-MM-dd
HH:mm:ss");
+ Map<String, String> septFirstParams =
+ BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA,
septFirst, null);
+ assertEquals("2022-09-01",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd-7*-1)]",
+ septFirstParams));
+ assertEquals("20220801",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyyMMdd+7*-1)]",
timeParams));
+ assertEquals("20220821",
+
ParameterUtils.convertParameterPlaceholders("$[week_last_day(yyyyMMdd+7*(-1))]",
timeParams));
+ }
+
+ @Test
+ public void timePlaceHolderWithBoundaryArithmeticFailure() {
+ // Arithmetic failures in boundary-function offsets propagate to
formatTimeExpression:
+ // ignore-invalid mode preserves the placeholder, strict mode rejects
the expression
+ assertEquals("$[month_first_day(yyyy-MM-dd+1/0)]",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyy-MM-dd+1/0)]",
timeParams));
+ assertThrows(IllegalArgumentException.class, () -> TimePlaceholderUtils
+ .formatTimeExpression("month_first_day(yyyy-MM-dd+1/0)", date,
false));
+ }
+
+ @Test
+ public void timePlaceHolderWithTzShapedArithmeticOperand() {
+ // Operands that merely start with a timezone-shaped number are
complete arithmetic
+ // expressions: division, multiplication and longer numbers are
evaluated as offsets,
+ // not treated as timezone literals followed by another offset
+ Date septFirst = DateUtils.parse("2022-09-01 00:00:00", "yyyy-MM-dd
HH:mm:ss");
+ Map<String, String> septFirstParams =
+ BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA,
septFirst, null);
+ // division operand: 1000/24/60 is 1000 minutes
+ assertEquals("20220801",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyyMMdd-1000/24/60)]",
+ septFirstParams));
+ // multiplication operand: 2*3 is 6 days
+ assertEquals("20220820",
+
ParameterUtils.convertParameterPlaceholders("$[this_day(yyyyMMdd-2*3)]",
timeParams));
+ // five-digit numeric operand
+ assertEquals("19950401",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyyMMdd-10000)]",
+ septFirstParams));
+ // arithmetic failures with a timezone-shaped prefix still reach
formatTimeExpression
+ assertEquals("$[month_first_day(yyyyMMdd+1000/0)]",
+
ParameterUtils.convertParameterPlaceholders("$[month_first_day(yyyyMMdd+1000/0)]",
+ septFirstParams));
+ assertThrows(IllegalArgumentException.class, () -> TimePlaceholderUtils
+ .formatTimeExpression("month_first_day(yyyyMMdd+1000/0)",
date, false));
+ }
}