package cnp.ew.converter;
import java.util.*;

/**
 * Used to display the AM/PM portion of a date in a prespecified
 * format. It is typically used as a component in converting a date
 * to a full string with a prespecified format, where the AM/PM output
 * is just one portion of the string.  Example usage:
 * <pre>
 *    CpAmpmToString converter = new CpAmpmToString("am/pm");
 *    convertedString = converter.convertDate(new Date("3/1/95 11:30"));
 * </pre>
 *
 * @see            cnp.ew.util.CpDateUnitToString
 * @see            cnp.ew.util.CpDateToStringConverter
 * @version        $Version$
 * @author         $Author: Ken $
 */
class CpAmpmToString extends CpDateUnitToString
{

	static final int AMPMLOWER=0;
	static final int AMPMUPPER=1;
	static final int APLOWER=2;
	static final int APUPPER=3;
	static String[] amStrings={ "am", "AM", "a", "A" };
	static String[] pmStrings={ "pm", "PM", "p", "P" };



    /**
     * Creates a new converter, with the given format for conversion.
     * @param formatString  Possible formats are:
     *      "AM/PM" - prints "AM" or "PM"
     *      "am/pm" - prints "am" or "pm"
     *      "A/P"   - prints "A" or "P"
     *      "a/p"   - prints "a" or "p"
     */
    public CpAmpmToString(String formatString)
    {
        super(formatString);

        if (formatString.equals("AM/PM")) {
            formatStyle = AMPMUPPER;
        } else {
            if (formatString.equals("am/pm")) {
                formatStyle = AMPMLOWER;
            } else {
                if (formatString.equals("A/P")) {
                    formatStyle = APUPPER;
                } else {
                    formatStyle = APLOWER;
                }
            }
        }
    }

    /**
     * Converts a given date to the appropriate string for the
     * predefined format.
     */
    public String convertDate(Date d)
    {
        int hours = d.getHours();

        if ((hours >= 12) && (hours < 24)) {
            return pmStrings[formatStyle];
        } else {
            return amStrings[formatStyle];
        }
    }
}

