Skip to content
Permalink
Browse files

Allow utc properties to have a non-UTC value when relaxed parsing is …

…enabled
  • Loading branch information...
benfortuna committed Apr 25, 2019
1 parent 9b1794e commit bfb0770e1094a5017b72f62a3b838eb889017ded
@@ -31,7 +31,11 @@
*/
package net.fortuna.ical4j.model.property;

import net.fortuna.ical4j.model.*;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.ParameterList;
import net.fortuna.ical4j.model.PropertyFactory;
import net.fortuna.ical4j.model.TimeZone;
import net.fortuna.ical4j.util.CompatibilityHints;
import net.fortuna.ical4j.validate.ValidationException;

/**
@@ -78,21 +82,29 @@ public final DateTime getDateTime() {
* @param dateTime The dateTime to set.
*/
public void setDateTime(final DateTime dateTime) {
// time must be in UTC..
if (dateTime != null) {
final DateTime utcDateTime = new DateTime(dateTime);
utcDateTime.setUtc(true);
setDate(utcDateTime);
if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
super.setDate(dateTime);
} else {
setDate(null);
// time must be in UTC..
if (dateTime != null) {
final DateTime utcDateTime = new DateTime(dateTime);
utcDateTime.setUtc(true);
setDate(utcDateTime);
} else {
setDate(null);
}
}
}

/**
* {@inheritDoc}
*/
public void setTimeZone(TimeZone timezone) {
throw new UnsupportedOperationException("Cannot set timezone for UTC properties");
if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING)) {
super.setTimeZone(timezone);
} else {
throw new UnsupportedOperationException("Cannot set timezone for UTC properties");
}
}

/**
@@ -0,0 +1,36 @@
package net.fortuna.ical4j.model.property

import net.fortuna.ical4j.model.DefaultTimeZoneRegistryFactory
import net.fortuna.ical4j.util.CompatibilityHints
import spock.lang.Specification

class DtStampSpec extends Specification {

def 'test dtstamp creation in relaxed parsing mode'() {
given: 'relaxed parsing is enabled'
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, true)

when: 'creating a dtstamp with a timezone'
def dtstamp = new DtStamp.Factory().createProperty()
dtstamp.parameters.add(new net.fortuna.ical4j.model.parameter.TzId('Europe/Prague'))
dtstamp.timeZone = DefaultTimeZoneRegistryFactory.instance.createRegistry().getTimeZone('Europe/Prague')
dtstamp.value = '20190104T155229'

then: 'dtstamp is created successfully'
dtstamp as String == 'DTSTAMP;TZID=Europe/Prague:20190104T155229\r\n'
}

def 'test dtstamp creation in default mode'() {
given: 'relaxed parsing is disabled'
CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_PARSING, false)

when: 'creating a dtstamp with a timezone'
def dtstamp = new DtStamp.Factory().createProperty()
dtstamp.parameters.add(new net.fortuna.ical4j.model.parameter.TzId('Europe/Prague'))
dtstamp.timeZone = DefaultTimeZoneRegistryFactory.instance.createRegistry().getTimeZone('Europe/Prague')
dtstamp.value = '20190104T155229'

then: 'exception is thrown'
thrown(UnsupportedOperationException)
}
}

0 comments on commit bfb0770

Please sign in to comment.
You can’t perform that action at this time.