diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d293a71..ca854a3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -8,12 +8,17 @@ + + + Fix parsing of Durations between 0 and -1 seconds. + + Fix Chronology text missing from jar. - Try to be more OSGi friemdly. + Try to be more OSGi friendly. Fixes #4. diff --git a/src/main/java/org/threeten/bp/Duration.java b/src/main/java/org/threeten/bp/Duration.java index 4ee6d25..f023a4c 100644 --- a/src/main/java/org/threeten/bp/Duration.java +++ b/src/main/java/org/threeten/bp/Duration.java @@ -367,8 +367,8 @@ public static Duration between(Temporal startInclusive, Temporal endExclusive) { * there must be at least one section after the "T". * The number part of each section must consist of one or more ASCII digits. * The number may be prefixed by the ASCII negative or positive symbol. - * The number of days, hours and minutes must parse to an {@code long}. - * The number of seconds must parse to an {@code long} with optional fraction. + * The number of days, hours and minutes must parse to a {@code long}. + * The number of seconds must parse to a {@code long} with optional fraction. * The decimal point may be either a dot or a comma. * The fractional part may have from zero to 9 digits. *

@@ -408,7 +408,8 @@ public static Duration parse(CharSequence text) { long hoursAsSecs = parseNumber(text, hourMatch, SECONDS_PER_HOUR, "hours"); long minsAsSecs = parseNumber(text, minuteMatch, SECONDS_PER_MINUTE, "minutes"); long seconds = parseNumber(text, secondMatch, 1, "seconds"); - int nanos = parseFraction(text, fractionMatch, seconds < 0 ? -1 : 1); + boolean negativeSecs = secondMatch != null && secondMatch.charAt(0) == '-'; + int nanos = parseFraction(text, fractionMatch, negativeSecs ? -1 : 1); try { return create(negate, daysAsSecs, hoursAsSecs, minsAsSecs, seconds, nanos); } catch (ArithmeticException ex) { diff --git a/src/test/java/org/threeten/bp/TestDuration.java b/src/test/java/org/threeten/bp/TestDuration.java index afcfe8c..e42d04c 100644 --- a/src/test/java/org/threeten/bp/TestDuration.java +++ b/src/test/java/org/threeten/bp/TestDuration.java @@ -454,6 +454,7 @@ public void factory_between_Instant_Instant_endNull() { {"PT-123456789S", -123456789, 0}, {"PT" + Long.MIN_VALUE + "S", Long.MIN_VALUE, 0}, + {"PT0.1S", 0, 100000000}, {"PT1.1S", 1, 100000000}, {"PT1.12S", 1, 120000000}, {"PT1.123S", 1, 123000000}, @@ -464,6 +465,7 @@ public void factory_between_Instant_Instant_endNull() { {"PT1.12345678S", 1, 123456780}, {"PT1.123456789S", 1, 123456789}, + {"PT-0.1S", -1, 1000000000 - 100000000}, {"PT-1.1S", -2, 1000000000 - 100000000}, {"PT-1.12S", -2, 1000000000 - 120000000}, {"PT-1.123S", -2, 1000000000 - 123000000}, @@ -478,8 +480,22 @@ public void factory_between_Instant_Instant_endNull() { {"PT" + Long.MIN_VALUE + ".000000000S", Long.MIN_VALUE, 0}, {"PT12M", 12 * 60, 0}, + {"PT12M0.35S", 12 * 60, 350000000}, + {"PT12M1.35S", 12 * 60 + 1, 350000000}, + {"PT12M-0.35S", 12 * 60 - 1, 1000000000 - 350000000}, + {"PT12M-1.35S", 12 * 60 - 2, 1000000000 - 350000000}, + {"PT12H", 12 * 3600, 0}, + {"PT12H0.35S", 12 * 3600, 350000000}, + {"PT12H1.35S", 12 * 3600 + 1, 350000000}, + {"PT12H-0.35S", 12 * 3600 - 1, 1000000000 - 350000000}, + {"PT12H-1.35S", 12 * 3600 - 2, 1000000000 - 350000000}, + {"P12D", 12 * 24 * 3600, 0}, + {"P12DT0.35S", 12 * 24 * 3600, 350000000}, + {"P12DT1.35S", 12 * 24 * 3600 + 1, 350000000}, + {"P12DT-0.35S", 12 * 24 * 3600 - 1, 1000000000 - 350000000}, + {"P12DT-1.35S", 12 * 24 * 3600 - 2, 1000000000 - 350000000}, }; }