Skip to content

Commit

Permalink
[built-in function]
Browse files Browse the repository at this point in the history
- [`$(date|diff|date1|date2|precision)`]: *NEW* function to derive the date-level difference between 2 dates (assume in standard format, `MM/dd/yyyy HH:mm:ss`). Use `precision` to control the return value.

Signed-off-by: automike <[email protected]>
  • Loading branch information
mikeliucc committed May 7, 2019
1 parent 399416f commit c93c1ac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/main/java/org/nexial/core/variable/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@

public class Date {

enum DatePrecision {
YEAR(1000L * 60 * 60 * 24 * 365),
MONTH(1000L * 60 * 60 * 24 * 30),
WEEK(1000 * 60 * 60 * 24 * 7),
DAY(1000 * 60 * 60 * 24),
HOUR(1000 * 60 * 60),
MINUTE(1000 * 60),
SECOND(1000),
MILLISECOND(1);

private double timestampDivider;

DatePrecision(double timestampDivider) { this.timestampDivider = timestampDivider; }

public String format(long timestamp) {
double diff = Math.abs(timestamp) / timestampDivider;
return diff == (long) diff ? ((long) diff) + "" : diff + "";
}
}

private abstract class DateTransform {
abstract Calendar transform(Calendar c);
}
Expand Down Expand Up @@ -104,6 +124,32 @@ public String addSecond(String date, String seconds) {

public String setSecond(String date, int seconds) { return setDate(date, SECOND, seconds); }

/**
* assumed that {@code date1} and {@code date2} are both in the format of <code>MM/dd/yyyy HH:mm:ss</code>.
* {@code precision} should match to one of the predefined value in {@link DatePrecision}
*/
public String diff(String date1, String date2, String precision) {
java.util.Date d1 = parseDate(date1, STD_DATE_FORMAT);
if (d1 == null) {
ConsoleUtils.error("Unable to parse date1 in the standard format " + STD_DATE_FORMAT + ": " + date1);
return null;
}

java.util.Date d2 = parseDate(date2, STD_DATE_FORMAT);
if (d2 == null) {
ConsoleUtils.error("Unable to parse date2 in the standard format " + STD_DATE_FORMAT + ": " + date2);
return null;
}

try {
DatePrecision datePrecision = DatePrecision.valueOf(precision);
return datePrecision.format(d1.getTime() - d2.getTime());
} catch (IllegalArgumentException e) {
ConsoleUtils.error("Unknown precision specified: " + precision);
return null;
}
}

protected java.util.Date parseDate(String date, String format) {
if (StringUtils.isBlank(date)) { return null; }
if (StringUtils.equals(format, EPOCH)) { return new java.util.Date(NumberUtils.toLong(date)); }
Expand Down
20 changes: 19 additions & 1 deletion src/test/java/org/nexial/core/model/ExecutionContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void replaceTokens_ignored() {
}

@Test
public void handleFunction() {
public void handleDateFunctions() {
MockExecutionContext subject = initMockContext();
subject.setData("firstDOW", "04/30/2017");

Expand All @@ -236,6 +236,24 @@ public void handleFunction() {
System.out.println(subject.handleFunction(
"$(date|format|$(date|addDay|$(sysdate|firstDOW|MM/dd/yyyy)|1)|MM/dd/yyyy|MM/dd/yy)"));

// diff
subject.setData("date1", "04/30/2017 00:00:15");
subject.setData("date2", "05/17/2017 21:49:22");
Assert.assertEquals("17.91",
subject.handleFunction("$(format|number|$(date|diff|${date1}|${date2}|DAY)|###.00)"));
Assert.assertEquals("17.91",
subject.handleFunction("$(format|number|$(date|diff|${date1}|${date2}|DAY)|###.##)"));
Assert.assertEquals("2.6", subject.handleFunction("$(format|number|$(date|diff|${date2}|${date1}|WEEK)|0.#)"));
Assert.assertEquals("1", subject.handleFunction("$(format|number|$(date|diff|${date2}|${date1}|MONTH)|#)"));
Assert.assertEquals("0", subject.handleFunction("$(format|number|$(date|diff|${date2}|${date1}|YEAR)|0)"));
Assert.assertEquals("0430",
subject.handleFunction("$(format|number|$(date|diff|${date1}|${date2}|HOUR)|0000)"));
Assert.assertEquals("25789.1167",
subject.handleFunction("$(format|number|$(date|diff|${date1}|${date2}|MINUTE)|#.####)"));
Assert.assertEquals("1547347", subject.handleFunction("$(date|diff|${date1}|${date2}|SECOND)"));
Assert.assertEquals("1547347000",
subject.handleFunction("$(format|number|$(date|diff|${date1}|${date2}|MILLISECOND)|0000)"));

subject.cleanProject();
}

Expand Down

0 comments on commit c93c1ac

Please sign in to comment.