diff --git a/engine/src/main/java/org/camunda/bpm/engine/impl/calendar/DurationHelper.java b/engine/src/main/java/org/camunda/bpm/engine/impl/calendar/DurationHelper.java index ac2aa23427c..dd93d2c3579 100644 --- a/engine/src/main/java/org/camunda/bpm/engine/impl/calendar/DurationHelper.java +++ b/engine/src/main/java/org/camunda/bpm/engine/impl/calendar/DurationHelper.java @@ -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; @@ -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"); } diff --git a/engine/src/test/java/org/camunda/bpm/engine/test/standalone/calendar/DurationHelperTest.java b/engine/src/test/java/org/camunda/bpm/engine/test/standalone/calendar/DurationHelperTest.java index 80a4bf653df..e33fabbf88b 100644 --- a/engine/src/test/java/org/camunda/bpm/engine/test/standalone/calendar/DurationHelperTest.java +++ b/engine/src/test/java/org/camunda/bpm/engine/test/standalone/calendar/DurationHelperTest.java @@ -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; @@ -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");