Skip to content

Commit

Permalink
chore(engine): add support for "PnW" duration patterns on timer events
Browse files Browse the repository at this point in the history
Related to CAM-10321, PR #422
  • Loading branch information
mboskamp authored and koevskinikola committed Jul 29, 2019
1 parent 9125cca commit 5e83407
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
*/
public class DurationHelper {

private final static EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER;
public static final String PnW_PATTERN = "P\\d+W";
private static final int MS_PER_WEEK = 7 * 24 * 60 * 60 * 1000;
private static final EngineUtilLogger LOG = ProcessEngineLogger.UTIL_LOGGER;

Date start;

Expand Down Expand Up @@ -135,9 +137,18 @@ private Date add(Date date, Duration duration) {
}

private Duration parsePeriod(String period) {
if (period.matches(PnW_PATTERN)) {
return parsePnWDuration(period);
}
return datatypeFactory.newDuration(period);
}

private Duration parsePnWDuration(String period) {
String weeks = period.replaceAll("\\D", "");
int numberOfWeeks = Integer.parseInt(weeks);
return datatypeFactory.newDuration(numberOfWeeks * MS_PER_WEEK);
}

private boolean isDuration(String time) {
return time.startsWith("P");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.camunda.bpm.engine.test.standalone.calendar;

import static junit.framework.TestCase.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;

import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -97,7 +98,17 @@ public void shouldNotExceedNumberNegativeWithStartDate() throws Exception {

assertEquals(parse("19700101-00:00:40"), dh.getDateAfter(parse("19700101-00:00:35")));
}


@Test
public void shouldParseAllSupportedISO8601DurationPatterns() throws Exception {
// given
// when
DurationHelper PnYnMnDTnHnMnS = new DurationHelper("P1Y5M21DT19H47M55S", parse("19700101-00:00:00"));
DurationHelper PnW = new DurationHelper("P2W", parse("19700101-00:00:00"));
// then
assertThat(PnYnMnDTnHnMnS.getDateAfter()).isEqualTo(parse("19710622-19:47:55"));
assertThat(PnW.getDateAfter()).isEqualTo(parse("19700115-00:00:00"));
}

private Date parse(String str) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");
Expand Down

0 comments on commit 5e83407

Please sign in to comment.