Skip to content

Commit

Permalink
Use exception for signaling leap second
Browse files Browse the repository at this point in the history
  • Loading branch information
ethlo committed Aug 12, 2019
1 parent 999b6c3 commit 72f2afe
Showing 1 changed file with 76 additions and 59 deletions.
135 changes: 76 additions & 59 deletions src/test/java/com/ethlo/time/CorrectnessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -34,85 +34,102 @@
public abstract class CorrectnessTest extends AbstractTest<Rfc3339>
{
private final String[] validFormats =
{
"2017-02-21T15:27:39Z", "2017-02-21T15:27:39.123Z",
"2017-02-21T15:27:39.123456Z", "2017-02-21T15:27:39.123456789Z",
"2017-02-21T15:27:39+00:00", "2017-02-21T15:27:39.123+00:00",
"2017-02-21T15:27:39.123456+00:00", "2017-02-21T15:27:39.123456789+00:00",
"2017-02-21T15:27:39.1+00:00", "2017-02-21T15:27:39.12+00:00",
"2017-02-21T15:27:39.123+00:00", "2017-02-21T15:27:39.1234+00:00",
"2017-02-21T15:27:39.12345+00:00", "2017-02-21T15:27:39.123456+00:00",
"2017-02-21T15:27:39.1234567+00:00", "2017-02-21T15:27:39.12345678+00:00",
"2017-02-21T15:27:39.123456789+00:00"
};
{
"2017-02-21T15:27:39Z", "2017-02-21T15:27:39.123Z",
"2017-02-21T15:27:39.123456Z", "2017-02-21T15:27:39.123456789Z",
"2017-02-21T15:27:39+00:00", "2017-02-21T15:27:39.123+00:00",
"2017-02-21T15:27:39.123456+00:00", "2017-02-21T15:27:39.123456789+00:00",
"2017-02-21T15:27:39.1+00:00", "2017-02-21T15:27:39.12+00:00",
"2017-02-21T15:27:39.123+00:00", "2017-02-21T15:27:39.1234+00:00",
"2017-02-21T15:27:39.12345+00:00", "2017-02-21T15:27:39.123456+00:00",
"2017-02-21T15:27:39.1234567+00:00", "2017-02-21T15:27:39.12345678+00:00",
"2017-02-21T15:27:39.123456789+00:00"
};

private final String[] invalidFormats = {
"2017-02-21T15:27:39", "2017-02-21T15:27:39.123",
"2017-02-21T15:27:39", "2017-02-21T15:27:39.123",
"2017-02-21T15:27:39.123456", "2017-02-21T15:27:39.123456789",
"2017-02-21T15:27:39+0000", "2017-02-21T15:27:39.123+0000",
"2017-02-21T15:27:39+0000", "2017-02-21T15:27:39.123+0000",
"201702-21T15:27:39.123456+0000", "20170221T15:27:39.123456789+0000"};

@Test
public void testParseLeapSecondUTC()
{
final String leapSecondUTC = "1990-12-31T23:59:60Z";
final OffsetDateTime result = instance.parseDateTime(leapSecondUTC);
assertThat(instance.formatUtc(result)).isEqualTo("1991-01-01T00:00:00Z");
final LeapSecondException exc = getLeapSecondsException(leapSecondUTC);
assertThat(instance.formatUtc(exc.getNearestDateTime())).isEqualTo("1991-01-01T00:00:00Z");
assertThat(exc.getSecondsInMinute()).isEqualTo(60);
}

@Test
public void testParseLeapSecondPST()
{
final String leapSecondPST = "1990-12-31T15:59:60-08:00";
final OffsetDateTime result = instance.parseDateTime(leapSecondPST);
assertThat(instance.formatUtc(result)).isEqualTo("1991-01-01T00:00:00Z");
final LeapSecondException exc = getLeapSecondsException(leapSecondPST);
assertThat(instance.formatUtc(exc.getNearestDateTime())).isEqualTo("1991-01-01T00:00:00Z");
assertThat(exc.getSecondsInMinute()).isEqualTo(60);
}

@Test
public void testParseLeapSecondUTCJune()
{
final String leapSecondUTC = "1992-06-30T23:59:60Z";
final OffsetDateTime result = instance.parseDateTime(leapSecondUTC);
assertThat(instance.formatUtc(result)).isEqualTo("1992-07-01T00:00:00Z");
final LeapSecondException exc = getLeapSecondsException(leapSecondUTC);
assertThat(instance.formatUtc(exc.getNearestDateTime())).isEqualTo("1992-07-01T00:00:00Z");
assertThat(exc.getSecondsInMinute()).isEqualTo(60);
}

@Test
public void testParseLeapSecondPSTJune()
{
final String leapSecondPST = "1992-06-30T15:59:60-08:00";
final OffsetDateTime result = instance.parseDateTime(leapSecondPST);
assertThat(instance.formatUtc(result)).isEqualTo("1992-07-01T00:00:00Z");
final LeapSecondException exc = getLeapSecondsException(leapSecondPST);
assertThat(instance.formatUtc(exc.getNearestDateTime())).isEqualTo("1992-07-01T00:00:00Z");
assertThat(exc.getSecondsInMinute()).isEqualTo(60);
}

private LeapSecondException getLeapSecondsException(final String dateTime)
{
try
{
instance.parseDateTime(dateTime);
throw new IllegalArgumentException("Should have thrown LeapSecondException");
}
catch (LeapSecondException exc)
{
return exc;
}
}

@Test(expected=DateTimeException.class)
@Test(expected = DateTimeException.class)
public void testFormat1()
{
final String s = "2017-02-21T15:27:39.0000000";
instance.parseDateTime(s);
}
@Test(expected=DateTimeException.class)

@Test(expected = DateTimeException.class)
public void testFormat2()
{
final String s = "2017-02-21T15:27:39.000+30:00";
instance.parseDateTime(s);
}

@Test
public void testFormat3()
{
final String s = "2017-02-21T10:00:00.000+12:00";
final OffsetDateTime date = instance.parseDateTime(s);
assertThat(instance.formatUtcMilli(date)).isEqualTo("2017-02-20T22:00:00.000Z");
}
@Test(expected=DateTimeException.class)

@Test(expected = DateTimeException.class)
public void testInvalidNothingAfterFractionalSeconds()
{
final String s = "2017-02-21T10:00:00.12345";
instance.parseDateTime(s);
}

@Test
public void testFormat4()
{
Expand All @@ -122,21 +139,21 @@ public void testFormat4()
assertThat(instance.format(Date.from(date.atZoneSameInstant(ZoneOffset.UTC).toInstant()), "CET", 3)).isEqualTo("2017-02-21T16:00:00.123+01:00");
assertThat(instance.format(new Date(date.toInstant().toEpochMilli()), "EST", 3)).isEqualTo("2017-02-21T10:00:00.123-05:00");
}
@Test(expected=DateTimeException.class)

@Test(expected = DateTimeException.class)
public void testParseMoreThanNanoResolutionFails()
{
instance.parseDateTime("2017-02-21T15:00:00.1234567891Z");
}
@Test(expected=DateTimeException.class)

@Test(expected = DateTimeException.class)
public void testFormatMoreThanNanoResolutionFails()
{
final OffsetDateTime d = instance.parseDateTime("2017-02-21T15:00:00.123456789Z");
final int fractionDigits = 10;
instance.formatUtc(d, fractionDigits);
}

@Test
public void testFormatUtc()
{
Expand All @@ -154,23 +171,23 @@ public void testFormatUtcMilli()
final OffsetDateTime date = instance.parseDateTime(s);
assertThat(instance.formatUtcMilli(date)).isEqualTo("2017-02-21T15:00:00.123Z");
}

@Test
public void testFormatUtcMilliWithDate()
{
final String s = "2017-02-21T15:00:00.123456789Z";
final OffsetDateTime date = instance.parseDateTime(s);
assertThat(instance.formatUtcMilli(new Date(date.toInstant().toEpochMilli()))).isEqualTo("2017-02-21T15:00:00.123Z");
}

@Test
public void testFormatUtcMicro()
{
final String s = "2017-02-21T15:00:00.123456789Z";
final OffsetDateTime date = instance.parseDateTime(s);
assertThat(instance.formatUtcMicro(date)).isEqualTo("2017-02-21T15:00:00.123456Z");
}

@Test
public void testFormatUtcNano()
{
Expand All @@ -179,37 +196,37 @@ public void testFormatUtcNano()
assertThat(instance.formatUtcNano(date)).isEqualTo(s);
}

@Test(expected=DateTimeException.class)
@Test(expected = DateTimeException.class)
public void testFormat4TrailingNoise()
{
final String s = "2017-02-21T15:00:00.123ZGGG";
instance.parseDateTime(s);
}

@Test
public void testFormat5()
{
final String s = "2017-02-21T15:27:39.123+13:00";
final OffsetDateTime date = instance.parseDateTime(s);
assertThat(instance.formatUtcMilli(date)).isEqualTo("2017-02-21T02:27:39.123Z");
}

@Test
public void testParseEmptyString()
{
final String s = "";
final OffsetDateTime date = instance.parseDateTime(s);
assertThat(date).isNull();
}

@Test
public void testParseNull()
{
final String s = null;
final OffsetDateTime date = instance.parseDateTime(s);
assertThat(date).isNull();
}

@Test
public void testRfcExample()
{
Expand All @@ -221,42 +238,42 @@ public void testRfcExample()
final OffsetDateTime dB = instance.parseDateTime(b);
assertThat(instance.formatUtc(dA)).isEqualTo(instance.formatUtc(dB));
}
@Test(expected=DateTimeException.class)

@Test(expected = DateTimeException.class)
public void testBadSeparator()
{
final String a = "1994 11-05T08:15:30-05:00";
assertThat(instance.parseDateTime(a)).isNotNull();
}
@Test(expected=DateTimeException.class)

@Test(expected = DateTimeException.class)
public void testParseNonDigit()
{
final String a = "199g-11-05T08:15:30-05:00";
assertThat(instance.parseDateTime(a)).isNotNull();
}

@Test(expected=DateTimeException.class)
@Test(expected = DateTimeException.class)
public void testInvalidDateTimeSeparator()
{
final String a = "1994-11-05X08:15:30-05:00";
assertThat(instance.parseDateTime(a)).isNotNull();
}

@Test
public void testLowerCaseTseparator()
{
final String a = "1994-11-05t08:15:30z";
assertThat(instance.parseDateTime(a)).isNotNull();
}

@Test
public void testSpaceAsSeparator()
{
final String a = "1994-11-05 08:15:30z";
assertThat(instance.parseDateTime(a)).isNotNull();
}

@Test
public void testValid()
{
Expand All @@ -265,7 +282,7 @@ public void testValid()
assertThat(instance.isValid(f)).overridingErrorMessage("Expecting to be valid <%s>", f).isTrue();
}
}

@Test
public void testInvalid()
{
Expand All @@ -277,21 +294,21 @@ public void testInvalid()
}
}
}

@Test
public void testMilitaryOffset()
{
final String s = "2017-02-21T15:27:39+0000";
assertThat(instance.isValid(s)).isFalse();
}

@Test(expected=DateTimeException.class)
@Test(expected = DateTimeException.class)
public void testParseUnknownLocalOffsetConvention()
{
final String s = "2017-02-21T15:27:39-00:00";
instance.parseDateTime(s);
}

@Test
public void testParseLowercaseZ()
{
Expand All @@ -307,7 +324,7 @@ public void testFormatWithNamedTimeZoneDate()
final String formatted = instance.format(new Date(d.toInstant().toEpochMilli()), "EST");
assertThat(formatted).isEqualTo("2017-02-21T10:27:39.321-05:00");
}

@Test
public void testFormatUtcDate()
{
Expand All @@ -316,7 +333,7 @@ public void testFormatUtcDate()
final String formatted = instance.formatUtc(new Date(d.toInstant().toEpochMilli()));
assertThat(formatted).isEqualTo("2017-02-21T15:27:39.321Z");
}

@Test
public void testFormatWithNamedTimeZone()
{
Expand All @@ -325,7 +342,7 @@ public void testFormatWithNamedTimeZone()
final String formatted = instance.format(new Date(d.toInstant().toEpochMilli()), "EST", 3);
assertThat(formatted).isEqualTo("2017-02-21T10:27:39.321-05:00");
}

@Override
protected long getRuns()
{
Expand Down

0 comments on commit 72f2afe

Please sign in to comment.