001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.international;
020
021import java.text.DateFormat;
022import java.text.ParseException;
023import java.util.Locale;
024import java.util.TimeZone;
025
026import lucee.commons.date.TimeZoneUtil;
027import lucee.commons.i18n.FormatUtil;
028import lucee.commons.lang.StringUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.exp.PageException;
031import lucee.runtime.ext.function.Function;
032import lucee.runtime.i18n.LocaleFactory;
033import lucee.runtime.op.Caster;
034import lucee.runtime.op.date.DateCaster;
035import lucee.runtime.type.dt.DateTime;
036import lucee.runtime.type.dt.DateTimeImpl;
037
038/**
039 * Implements the CFML Function dateformat
040 */
041public final class LSTimeFormat implements Function {
042        
043        /**
044         * @param pc
045         * @param o
046         * @return
047         * @throws PageException
048         */
049        public static String call(PageContext pc , Object o) throws PageException {
050                return _call(pc, o, "short", pc.getLocale(),pc.getTimeZone());
051        }
052        
053        public static String call(PageContext pc , Object o, String mask) throws PageException {
054                return _call(pc, o, mask, pc.getLocale(),pc.getTimeZone());
055        }
056        public static String call(PageContext pc , Object o, String mask,String strLocale) throws PageException {
057                return _call(pc, o, mask, LocaleFactory.getLocale(strLocale),pc.getTimeZone());
058        }
059        public static String call(PageContext pc , Object o, String mask,String strLocale,String strTimezone) throws PageException {
060                return _call(pc, o, mask, 
061                                strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale),
062                                strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone));
063        }
064
065        private static String _call(PageContext pc, Object o, String mask,Locale locale,TimeZone tz) throws PageException {
066        if(o instanceof String && StringUtil.isEmpty((String)o,true)) return "";
067        return new lucee.runtime.format.TimeFormat(locale).format(toTimeLS(locale, tz, o),mask,tz);
068        }
069        
070        
071        private static DateTime toTimeLS(Locale locale, TimeZone timeZone, Object object) throws PageException {
072                if(object instanceof DateTime) return (DateTime) object;
073                if(object instanceof CharSequence) {
074                        String str=Caster.toString(object);
075                        
076                        DateFormat[] formats=FormatUtil.getTimeFormats(locale,timeZone,true);
077                        for(int i=0;i<formats.length;i++) {
078                                try {
079                                        return new DateTimeImpl(formats[i].parse(str).getTime(),false);
080                                } 
081                                catch (ParseException e) {}
082                        }
083                        
084                }
085                return DateCaster.toDateAdvanced(object,timeZone);
086        }
087        
088        
089}