Skip to content

Commit

Permalink
Support relaxed parsing of utc properties
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jun 14, 2020
1 parent 9094db5 commit 404d2c7
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/main/java/net/fortuna/ical4j/model/TemporalAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ public String toString(ZoneId zoneId) {
}

private String toString(T temporal, ZoneId zoneId) {
if (!ChronoUnit.SECONDS.isSupportedBy(temporal)) {
if (ZoneOffset.UTC.equals(zoneId)) {
return toInstantString(temporal);
} else if (!ChronoUnit.SECONDS.isSupportedBy(temporal)) {
return toString(CalendarDateFormat.DATE_FORMAT, temporal);
} else {
if (isFloating(getTemporal())) {
Expand All @@ -146,6 +148,10 @@ private String toString(T temporal, ZoneId zoneId) {
}
}

private String toInstantString(T temporal) {
return toString(CalendarDateFormat.UTC_DATE_TIME_FORMAT, temporal);
}

private String toString(CalendarDateFormat format, T temporal) {
return format.format(temporal);
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/net/fortuna/ical4j/model/property/DateProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.model.parameter.TzId;
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.util.CompatibilityHints;
import net.fortuna.ical4j.util.Strings;
import net.fortuna.ical4j.validate.ParameterValidator;
import net.fortuna.ical4j.validate.ValidationException;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.ZoneId;
Expand Down Expand Up @@ -157,8 +159,20 @@ public void setValue(final String value) throws DateTimeParseException {
// value can be either a date-time or a date..
if (value != null && !value.isEmpty()) {
Optional<TzId> tzId = getParameter(Parameter.TZID);
this.date = tzId.map(id -> (TemporalAdapter<T>) TemporalAdapter.parse(value, id, timeZoneRegistry))
.orElseGet(() -> TemporalAdapter.parse(value, parseFormat));
try {
this.date = tzId.map(id -> (TemporalAdapter<T>) TemporalAdapter.parse(value, id, timeZoneRegistry))
.orElseGet(() -> TemporalAdapter.parse(value, parseFormat));
} catch (DateTimeParseException dtpe) {
if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
LoggerFactory.getLogger(DateProperty.class).warn("Invalid DATE-TIME format", dtpe);

// parse with relaxed format..
this.date = tzId.map(id -> (TemporalAdapter<T>) TemporalAdapter.parse(value, id, timeZoneRegistry))
.orElseGet(() -> TemporalAdapter.parse(value, CalendarDateFormat.DEFAULT_PARSE_FORMAT));
} else {
throw dtpe;
}
}
} else {
this.date = null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/fortuna/ical4j/model/property/ExDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@ public class ExDate<T extends Temporal> extends DateListProperty<T> {
* Default constructor.
*/
public ExDate() {
super(EXDATE, new Factory());
super(EXDATE, new Factory<T>());
}

/**
* @param aList a list of parameters for this component
* @param aValue a value string for this component
*/
public ExDate(final List<Parameter> aList, final String aValue) {
super(EXDATE, aList, new Factory());
super(EXDATE, aList, new Factory<T>());
setValue(aValue);
}

/**
* @param dList a list of dates
*/
public ExDate(final DateList<T> dList) {
super(EXDATE, dList, new Factory());
super(EXDATE, dList, new Factory<T>());
}

/**
* @param aList a list of parameters for this component
* @param dList a list of dates
*/
public ExDate(final List<Parameter> aList, final DateList<T> dList) {
super(EXDATE, aList, dList, new Factory());
super(EXDATE, aList, dList, new Factory<T>());
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/fortuna/ical4j/model/property/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.validate.ParameterValidator;
import net.fortuna.ical4j.validate.ValidationException;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -272,6 +273,7 @@ public final void setValue(final String aValue) {
super.setValue(aValue);
duration = null;
} catch (DateTimeParseException pe) {
LoggerFactory.getLogger(Trigger.class).warn(String.format("Not a valid DATE-TIME value: %s", aValue));
duration = TemporalAmountAdapter.parse(aValue);
super.setDate(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
public class LastModifiedTest extends PropertyTest {

/**
* @param property
* @param expectedValue
*/
public LastModifiedTest(LastModified lastModified, String expectedValue) {
Expand All @@ -62,13 +61,12 @@ public LastModifiedTest(String testMethod, LastModified property) {
}

/**
* @return
* @throws ParseException
*/
public static TestSuite suite() throws ParseException {
TestSuite suite = new TestSuite();
LastModified modified = new LastModified("20081124T090000");
suite.addTest(new LastModifiedTest(modified, "20081124T090000"));
LastModified modified = new LastModified("20081124T090000Z");
suite.addTest(new LastModifiedTest(modified, "20081124T090000Z"));

modified = new LastModified("20081124T090000Z");
suite.addTest(new LastModifiedTest("testValidation", modified));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.text.ParseException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Date;

/**
Expand Down Expand Up @@ -80,7 +81,7 @@ public TriggerTest(String testMethod, Trigger property) {
* @throws ParseException
*/
public void testSetValue() throws ParseException {
trigger.setValue(TemporalAdapter.from(new DateTime(new Date(0).getTime())).toString());
trigger.setValue(TemporalAdapter.from(new DateTime(new Date(0).getTime())).toString(ZoneOffset.UTC));

log.info(TemporalAdapter.from(new DateTime(new Date(0).getTime())).toString());
log.info(trigger.toString());
Expand Down

0 comments on commit 404d2c7

Please sign in to comment.