Skip to content

Commit

Permalink
Merge pull request ethlo#6 from ethlo/issue-4-leap-second-parsing
Browse files Browse the repository at this point in the history
Use exception for signaling leap second
  • Loading branch information
ethlo authored Aug 12, 2019
2 parents cb68490 + 72f2afe commit 6f2513e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 64 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/ethlo/time/FastInternetDateTimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
public class FastInternetDateTimeUtil extends AbstractRfc3339 implements W3cDateTimeUtil
{
public static final int LEAP_SECOND_SECONDS = 60;
private final StdJdkInternetDateTimeUtil delegate = new StdJdkInternetDateTimeUtil();

private static final char PLUS = '+';
private static final char MINUS = '-';
private static final char DATE_SEPARATOR = '-';
Expand All @@ -48,6 +46,7 @@ public class FastInternetDateTimeUtil extends AbstractRfc3339 implements W3cDate
private static final char ZULU_UPPER = 'Z';
private static final char ZULU_LOWER = 'z';
private static final int[] widths = new int[]{100_000_000, 10_000_000, 1_000_000, 100_000, 10_000, 1_000, 100, 10, 1};
private final StdJdkInternetDateTimeUtil delegate = new StdJdkInternetDateTimeUtil();

@Override
public OffsetDateTime parseDateTime(String s)
Expand Down Expand Up @@ -414,7 +413,7 @@ else if (remaining == 0)
&& utcMinute == 59)
{
// Consider it a leap second
return OffsetDateTime.of(year, month, day, hour, minute, 59, fractions, offset).plusSeconds(1);
throw new LeapSecondException(OffsetDateTime.of(year, month, day, hour, minute, 59, fractions, offset).plusSeconds(1), second);
}
}
return OffsetDateTime.of(year, month, day, hour, minute, second, fractions, offset);
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/ethlo/time/LeapSecondException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.ethlo.time;

/*-
* #%L
* Internet Time Utility
* %%
* Copyright (C) 2017 - 2019 Morten Haraldsen (ethlo)
* %%
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import java.time.OffsetDateTime;

public class LeapSecondException extends RuntimeException
{
private final int secondsInMinute;
private final OffsetDateTime nearestDateTime;

public LeapSecondException(OffsetDateTime nearestDateTime, int secondsInMinute)
{
this.nearestDateTime = nearestDateTime;
this.secondsInMinute = secondsInMinute;
}

public int getSecondsInMinute()
{
return secondsInMinute;
}

public OffsetDateTime getNearestDateTime()
{
return nearestDateTime;
}
}
Loading

0 comments on commit 6f2513e

Please sign in to comment.