Skip to content

Commit

Permalink
Merge pull request #368 from ical4j/feature/366-parse-empty-duration
Browse files Browse the repository at this point in the history
Support empty duration values when relaxed parsing is enabled
  • Loading branch information
benfortuna committed Nov 1, 2019
2 parents 9b82efe + 021b009 commit 1e717ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
@@ -1,5 +1,6 @@
package net.fortuna.ical4j.model;

import net.fortuna.ical4j.util.CompatibilityHints;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

Expand Down Expand Up @@ -115,7 +116,10 @@ private String durationToString(Duration duration) {

public static TemporalAmountAdapter parse(String value) {
TemporalAmount retVal = null;
if (value.matches("([+-])?P.*(W|D)")) {
if ("P".equals(value) && CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
retVal = Period.ZERO;
}
else if (value.matches("([+-])?P.*(W|D)")) {
retVal = java.time.Period.parse(value);
} else {
retVal = java.time.Duration.parse(value);
Expand Down
@@ -1,5 +1,6 @@
package net.fortuna.ical4j.model

import net.fortuna.ical4j.util.CompatibilityHints
import spock.lang.Specification
import spock.lang.Unroll

Expand All @@ -26,6 +27,18 @@ class TemporalAmountAdapterTest extends Specification {
Duration.ofDays(15).plusHours(5).plusSeconds(20) | 'P15DT5H0M20S'
}

def "verify relaxed string parsing"() {
setup: 'enable relaxed parsing'
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, true)

expect:
TemporalAmountAdapter.parse(stringValue).duration == expectedDuration

where:
stringValue | expectedDuration
"P" | java.time.Period.ZERO
}

def 'verify temporalamount creation'() {
expect:
TemporalAmountAdapter.from(duration).duration == expectedTemporalAmount
Expand Down

0 comments on commit 1e717ea

Please sign in to comment.