Added future tolerance to Date10 and Date4 parsers; simplified the use of generics in all the FieldParseInfo subclasses. Added a test for the future tolerance of date parsers.
git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@85 db60a039-f72e-0410-b1d2-baa000846441
This commit is contained in:
@@ -32,13 +32,17 @@ import com.solab.iso8583.IsoValue;
|
||||
*/
|
||||
public class Date10ParseInfo extends FieldParseInfo {
|
||||
|
||||
|
||||
private static final long FUTURE_TOLERANCE;
|
||||
|
||||
static {
|
||||
FUTURE_TOLERANCE = Long.parseLong(System.getProperty("j8583.future.tolerance", "900000"));
|
||||
}
|
||||
public Date10ParseInfo() {
|
||||
super(IsoType.DATE10, 10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> IsoValue<?> parse(byte[] buf, int pos, CustomField<T> custom)
|
||||
public IsoValue<Date> parse(byte[] buf, int pos, CustomField<?> custom)
|
||||
throws ParseException {
|
||||
if (pos < 0) {
|
||||
throw new ParseException(String.format("Invalid position %d", pos), pos);
|
||||
@@ -55,15 +59,12 @@ public class Date10ParseInfo extends FieldParseInfo {
|
||||
cal.set(Calendar.HOUR_OF_DAY, ((buf[pos + 4] - 48) * 10) + buf[pos + 5] - 48);
|
||||
cal.set(Calendar.MINUTE, ((buf[pos + 6] - 48) * 10) + buf[pos + 7] - 48);
|
||||
cal.set(Calendar.SECOND, ((buf[pos + 8] - 48) * 10) + buf[pos + 9] - 48);
|
||||
if (cal.getTime().after(new Date())) {
|
||||
cal.add(Calendar.YEAR, -1);
|
||||
}
|
||||
adjustWithFutureTolerance(cal);
|
||||
return new IsoValue<Date>(type, cal.getTime(), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> IsoValue<?> parseBinary(byte[] buf, int pos,
|
||||
CustomField<T> custom) throws ParseException {
|
||||
public IsoValue<Date> parseBinary(byte[] buf, int pos, CustomField<?> custom) throws ParseException {
|
||||
int[] tens = new int[5];
|
||||
int start = 0;
|
||||
for (int i = pos; i < pos + tens.length; i++) {
|
||||
@@ -78,10 +79,17 @@ public class Date10ParseInfo extends FieldParseInfo {
|
||||
cal.set(Calendar.HOUR_OF_DAY, tens[2]);
|
||||
cal.set(Calendar.MINUTE, tens[3]);
|
||||
cal.set(Calendar.SECOND, tens[4]);
|
||||
if (cal.getTime().after(new Date())) {
|
||||
cal.add(Calendar.YEAR, -1);
|
||||
}
|
||||
adjustWithFutureTolerance(cal);
|
||||
return new IsoValue<Date>(type, cal.getTime(), null);
|
||||
}
|
||||
|
||||
public static void adjustWithFutureTolerance(Calendar cal) {
|
||||
//We need to handle a small tolerance into the future (a couple of minutes)
|
||||
long now = System.currentTimeMillis();
|
||||
long then = cal.getTimeInMillis();
|
||||
if (then > now && then-now > FUTURE_TOLERANCE) {
|
||||
cal.add(Calendar.YEAR, -1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user