All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.solab.iso8583.parse.DateTimeParseInfo Maven / Gradle / Ivy

Go to download

Java implementation of the ISO 8583 protocol, focused on making the creation, edition and reading of ISO8583 messages as simple and flexible as possible.

There is a newer version: 1.22.5
Show newest version
/*
 * j8583 A Java implementation of the ISO8583 protocol
 * Copyright (C) 2007 Enrique Zamudio Lopez
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package com.solab.iso8583.parse;

import com.solab.iso8583.IsoType;
import com.solab.iso8583.IsoValue;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Abstract class for date/time parsers.
 *
 * @author Enrique Zamudio         Date: 18/12/13 18:21
 */
public abstract class DateTimeParseInfo extends FieldParseInfo {

    /**
     * The constant FUTURE_TOLERANCE.
     */
    protected static final long FUTURE_TOLERANCE;
    /**
     * The Tz.
     */
    protected TimeZone tz;
    private static TimeZone defaultTimezone;

   	static {
   		FUTURE_TOLERANCE = Long.parseLong(System.getProperty("j8583.future.tolerance", "900000"));
   	}

    /**
     * Sets default time zone.
     *
     * @param tz the tz
     */
    public static void setDefaultTimeZone(TimeZone tz) {
   	    defaultTimezone = tz;
    }

    /**
     * Gets default time zone.
     *
     * @return the default time zone
     */
    public static TimeZone getDefaultTimeZone() {
   	    return defaultTimezone;
    }

    /**
     * Instantiates a new Date time parse info.
     *
     * @param type   the type
     * @param length the length
     */
    protected DateTimeParseInfo(IsoType type, int length) {
        super(type, length);
    }

    /**
     * Sets time zone.
     *
     * @param value the value
     */
    public void setTimeZone(TimeZone value) {
        tz = value;
    }

    /**
     * Gets time zone.
     *
     * @return the time zone
     */
    public TimeZone getTimeZone() {
        return tz;
    }

    /**
     * Adjust with future tolerance.
     *
     * @param cal the cal
     */
    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);
   		}
   	}

    /**
     * Create value iso value.
     *
     * @param cal       the cal
     * @param adjusting the adjusting
     * @return the iso value
     */
    protected IsoValue createValue(Calendar cal, boolean adjusting) {
        if (tz != null) {
            cal.setTimeZone(tz);
        } else if (getDefaultTimeZone() != null) {
            cal.setTimeZone(getDefaultTimeZone());
        }
        if (adjusting) {
            adjustWithFutureTolerance(cal);
        }
        IsoValue v = new IsoValue<>(type, cal.getTime(), null);
        if (tz != null) {
            v.setTimeZone(tz);
        } else if (getDefaultTimeZone() != null) {
            v.setTimeZone(getDefaultTimeZone());
        }
		return v;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy