001package org.andromda.utils;
002
003import org.apache.commons.lang.time.DateUtils;
004
005/**
006 * Provides additional methods supporting various date-related features
007 */
008public class DateUtilsHelper
009    extends DateUtils
010{
011    // order is important !
012    private static final FormatPattern[] JAVA2PERL_FORMAT_PATTERNS = {new FormatPattern("y{4,}", "%Y"),
013        new FormatPattern("y{1,3}", "%y"),
014        new FormatPattern("M{4,}", "%B"),
015        new FormatPattern("M{3}", "%b"),
016        new FormatPattern("M{1,2}", "%m"),
017        new FormatPattern("d{4,}", "%A"),
018        new FormatPattern("d{3,}", "%a"),
019        new FormatPattern("d{2}", "%d"),
020        new FormatPattern("d{1}", "%e"),
021        new FormatPattern("E{4,}", "%A"),
022        new FormatPattern("E{1,3}", "%a"),
023        new FormatPattern("H{2,}", "%H"),
024        new FormatPattern("H{1}", "%k"),
025        new FormatPattern("h{2,}", "%I"),
026        new FormatPattern("h{1}", "%l"),
027        new FormatPattern("D{3}", "%j"),
028        new FormatPattern("s{2,}", "%S"),
029        new FormatPattern("s{1}", "%s"),
030        new FormatPattern("m{2,}", "%M")};
031
032    /**
033     * Converts a Java SimpleDateFormat into an equivalent String suited for dates used on Perl/PHP platforms.
034     * @param javaFormat
035     * @return perl format
036     */
037    public static String formatJavaToPerl(String javaFormat)
038    {
039        String perlFormat = null;
040
041        if (javaFormat != null)
042        {
043            perlFormat = javaFormat;
044
045            // this implementation is quite rough, and not at all performant, but it works for now
046            // @todo (wouter): re-implement
047            for (int i = 0; i < JAVA2PERL_FORMAT_PATTERNS.length; i++)
048            {
049                final FormatPattern formatPattern = JAVA2PERL_FORMAT_PATTERNS[i];
050                perlFormat = perlFormat.replaceAll(formatPattern.getPattern(), "%%%" + i + "%%%");
051            }
052
053            for (int i = 0; i < JAVA2PERL_FORMAT_PATTERNS.length; i++)
054            {
055                final FormatPattern formatPattern = JAVA2PERL_FORMAT_PATTERNS[i];
056                perlFormat = perlFormat.replaceAll("%%%" + i + "%%%", formatPattern.getReplacement());
057            }
058        }
059
060        return perlFormat;
061    }
062
063    private static final String[] PERL_TIME_FORMATS = new String[]{"%H", "%I", "%k", "%l", "%p", "%P", "%s", "%S"};
064
065    /**
066     * Checks whether a perl formatted date contains information about displaying time.
067     * @param perlFormat
068     * @return true if timeformat
069     */
070    public static boolean containsTimeFormat(String perlFormat)
071    {
072        boolean containsTimeFormat = false;
073
074        for (int i = 0; i < PERL_TIME_FORMATS.length && !containsTimeFormat; i++)
075        {
076            String timeFormatPattern = PERL_TIME_FORMATS[i];
077            containsTimeFormat = perlFormat.contains(timeFormatPattern);
078        }
079
080        return containsTimeFormat;
081    }
082
083    private static final class FormatPattern
084    {
085        private final String pattern;
086        private final String replacement;
087
088        public FormatPattern(
089            final String formatPattern,
090            final String replacement)
091        {
092            this.pattern = formatPattern;
093            this.replacement = replacement;
094        }
095
096        public String getPattern()
097        {
098            return pattern;
099        }
100
101        public String getReplacement()
102        {
103            return replacement;
104        }
105    }
106}