Browse Source

[github-234] Ensure the hours position is correct. Thanks to Anthony Schott. This closes #234

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1888804 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_1_0
PJ Fanning 3 years ago
parent
commit
e7a2df7dda

+ 15
- 6
poi/src/main/java/org/apache/poi/ss/format/CellDateFormatter.java View File



private static /* final */ CellDateFormatter SIMPLE_DATE; private static /* final */ CellDateFormatter SIMPLE_DATE;


private class DatePartHandler implements CellFormatPart.PartHandler {
class DatePartHandler implements CellFormatPart.PartHandler {
private int mStart = -1; private int mStart = -1;
private int mLen; private int mLen;
private int hStart = -1; private int hStart = -1;
case 'y': case 'y':
case 'Y': case 'Y':
mStart = -1; mStart = -1;
if (part.length() == 3)
// See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369
if (part.length() == 1)
part = "yy";
else if (part.length() == 3)
part = "yyyy"; part = "yyyy";
return part.toLowerCase(Locale.ROOT); return part.toLowerCase(Locale.ROOT);


} }
} }


public void updatePositions(int pos, int offset) {
if (pos < hStart) {
hStart += offset;
}
if (pos < mStart) {
mStart += offset;
}
}

public void finish(StringBuffer toAppendTo) { public void finish(StringBuffer toAppendTo) {
if (hStart >= 0 && !showAmPm) { if (hStart >= 0 && !showAmPm) {
for (int i = 0; i < hLen; i++) { for (int i = 0; i < hLen; i++) {
StringBuffer descBuf = CellFormatPart.parseFormat(format, StringBuffer descBuf = CellFormatPart.parseFormat(format,
CellFormatType.DATE, partHandler); CellFormatType.DATE, partHandler);
partHandler.finish(descBuf); partHandler.finish(descBuf);
// tweak the format pattern to pass tests on JDK 1.7,
// See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369
String ptrn = descBuf.toString().replaceAll("((y)(?!y))(?<!yy)", "yy");
dateFmt = new SimpleDateFormat(ptrn, locale);
dateFmt = new SimpleDateFormat(descBuf.toString(), locale);
dateFmt.setTimeZone(LocaleUtil.getUserTimeZone()); dateFmt.setTimeZone(LocaleUtil.getUserTimeZone());
} }



+ 8
- 0
poi/src/main/java/org/apache/poi/ss/format/CellFormatPart.java View File

int pos = 0; int pos = 0;
while ((pos = fmt.indexOf("''", pos)) >= 0) { while ((pos = fmt.indexOf("''", pos)) >= 0) {
fmt.delete(pos, pos + 2); fmt.delete(pos, pos + 2);
if (partHandler instanceof CellDateFormatter.DatePartHandler) {
CellDateFormatter.DatePartHandler datePartHandler = (CellDateFormatter.DatePartHandler) partHandler;
datePartHandler.updatePositions(pos, -2);
}
} }


// Now the final pass for quoted chars: Replace any \u0000 with '' // Now the final pass for quoted chars: Replace any \u0000 with ''
pos = 0; pos = 0;
while ((pos = fmt.indexOf("\u0000", pos)) >= 0) { while ((pos = fmt.indexOf("\u0000", pos)) >= 0) {
fmt.replace(pos, pos + 1, "''"); fmt.replace(pos, pos + 1, "''");
if (partHandler instanceof CellDateFormatter.DatePartHandler) {
CellDateFormatter.DatePartHandler datePartHandler = (CellDateFormatter.DatePartHandler) partHandler;
datePartHandler.updatePositions(pos, 1);
}
} }
} }



BIN
poi/src/main/java9/module-info.class View File


BIN
poi/src/test/java9/module-info.class View File


BIN
test-data/spreadsheet/DateFormatTests.xlsx View File


Loading…
Cancel
Save