joda-time icon indicating copy to clipboard operation
joda-time copied to clipboard

ISO8601 strangeness in Interval.parse()

Open gsson opened this issue 12 years ago • 2 comments

See comment in test for details.

import static org.junit.Assert.assertTrue;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Interval;
import org.junit.Test;

public class TestInterval {
    /* I don't  have access to ISO 8601, but various sources on the
     * Internet (Wikipedia [1] and others) claim that when defining
     * intervals from two instants, unspecified fields in the second
     * instant of the interval should be copied from the first,
     * including timezones.
     *
     * As far as I can tell, unspecified timezones will always be
     * replaced with the local timezone.
     *
     * The strange timezone used in test was selected to minimize the
     * risk that it is the same as the local timezone.
     *
     * [1]: http://en.wikipedia.org/wiki/ISO_8601#Time_intervals
     */
    @Test
    public void testInterval() {
        String intervalString = "2007-03-10T00:00+12:34/2007-03-11";
        Interval actual = Interval.parse(intervalString);

        DateTimeZone offset = DateTimeZone.forOffsetHoursMinutes(12, 34);
        DateTime start = new DateTime(2007, 03, 10, 00, 00, offset);
        DateTime end = new DateTime(2007, 03, 11, 00, 00, offset);
        Interval expected = new Interval(start, end);

        assertTrue(expected.isEqual(actual));
    }
}

gsson avatar Jan 23 '14 22:01 gsson

That would be a reasonable format to parse, although I don't know if it is an ISO-8601 format or not.

jodastephen avatar Jan 27 '14 15:01 jodastephen

I have it on good authority that at least one version of ISO 8601 says this about the interval notation:

  • representations for time-zones and Coordinated Universal Time included with the component preceding the solidus shall be assumed to apply to the component following the solidus, unless a corresponding alternative is included

gsson avatar Jan 27 '14 18:01 gsson