Permalink
Browse files

Better lenient parsing for fraction

See #65
  • Loading branch information...
1 parent 33479da commit 0491d49458dbb019245407aa4a95ca916f90e3f4 @jodastephen jodastephen committed Dec 2, 2012
View
13 src/main/java/javax/time/format/DateTimeFormatterBuilder.java
@@ -422,8 +422,8 @@ private DateTimeFormatterBuilder appendFixedWidth(int width, NumberPrinterParser
* No rounding occurs due to the maximum width - digits are simply dropped.
* <p>
* When parsing in strict mode, the number of parsed digits must be between
- * the minimum and maximum width. When parsing in lenient mode, the number
- * of parsed digits must be at least the minimum width, up to 9 digits.
+ * the minimum and maximum width. When parsing in lenient mode, the minimum
+ * width is considered to be zero and the maximum is nine.
* <p>
* If the value cannot be obtained then an exception will be thrown.
* If the value is negative an exception will be thrown.
@@ -2056,23 +2056,24 @@ public boolean print(DateTimePrintContext context, StringBuilder buf) {
@Override
public int parse(DateTimeParseContext context, CharSequence text, int position) {
+ int effectiveMin = (context.isStrict() ? minWidth : 0);
+ int effectiveMax = (context.isStrict() ? maxWidth : 9);
int length = text.length();
if (position == length) {
// valid if whole field is optional, invalid if minimum width
- return (minWidth > 0 ? ~position : position);
+ return (effectiveMin > 0 ? ~position : position);
}
if (decimalPoint) {
if (text.charAt(position) != context.getSymbols().getDecimalSeparator()) {
// valid if whole field is optional, invalid if minimum width
- return (minWidth > 0 ? ~position : position);
+ return (effectiveMin > 0 ? ~position : position);
}
position++;
}
- int minEndPos = position + minWidth;
+ int minEndPos = position + effectiveMin;
if (minEndPos > length) {
return ~position; // need at least min width digits
}
- int effectiveMax = (context.isStrict() ? maxWidth : 9);
int maxEndPos = Math.min(position + effectiveMax, length);
int total = 0; // can use int because we are only parsing up to 9 digits
int pos = position;
View
16 src/tck/java/javax/time/format/TCKDateTimeFormatterBuilder.java
@@ -251,44 +251,44 @@ public void test_appendValueReduced_subsequent_parse() throws Exception {
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
@Test(groups={"tck"})
- public void test_appendFraction_3arg() throws Exception {
+ public void test_appendFraction_4arg() throws Exception {
builder.appendFraction(MINUTE_OF_HOUR, 1, 9, false);
DateTimeFormatter f = builder.toFormatter();
assertEquals(f.toString(), "Fraction(MinuteOfHour,1,9)");
}
@Test(expectedExceptions=NullPointerException.class, groups={"tck"})
- public void test_appendFraction_3arg_nullRule() throws Exception {
+ public void test_appendFraction_4arg_nullRule() throws Exception {
builder.appendFraction(null, 1, 9, false);
}
@Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"})
- public void test_appendFraction_3arg_invalidRuleNotFixedSet() throws Exception {
+ public void test_appendFraction_4arg_invalidRuleNotFixedSet() throws Exception {
builder.appendFraction(DAY_OF_MONTH, 1, 9, false);
}
@Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"})
- public void test_appendFraction_3arg_minTooSmall() throws Exception {
+ public void test_appendFraction_4arg_minTooSmall() throws Exception {
builder.appendFraction(MINUTE_OF_HOUR, -1, 9, false);
}
@Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"})
- public void test_appendFraction_3arg_minTooBig() throws Exception {
+ public void test_appendFraction_4arg_minTooBig() throws Exception {
builder.appendFraction(MINUTE_OF_HOUR, 10, 9, false);
}
@Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"})
- public void test_appendFraction_3arg_maxTooSmall() throws Exception {
+ public void test_appendFraction_4arg_maxTooSmall() throws Exception {
builder.appendFraction(MINUTE_OF_HOUR, 0, -1, false);
}
@Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"})
- public void test_appendFraction_3arg_maxTooBig() throws Exception {
+ public void test_appendFraction_4arg_maxTooBig() throws Exception {
builder.appendFraction(MINUTE_OF_HOUR, 1, 10, false);
}
@Test(expectedExceptions=IllegalArgumentException.class, groups={"tck"})
- public void test_appendFraction_3arg_maxWidthMinWidth() throws Exception {
+ public void test_appendFraction_4arg_maxWidthMinWidth() throws Exception {
builder.appendFraction(MINUTE_OF_HOUR, 9, 3, false);
}

0 comments on commit 0491d49

Please sign in to comment.