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 **/
019/**
020 * Implements the CFML Function lsdateformat
021 */
022package lucee.runtime.functions.international;
023
024import java.util.Locale;
025import java.util.TimeZone;
026
027import lucee.commons.date.TimeZoneUtil;
028import lucee.commons.lang.StringUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.exp.PageException;
031import lucee.runtime.ext.function.Function;
032import lucee.runtime.functions.BIF;
033import lucee.runtime.i18n.LocaleFactory;
034import lucee.runtime.op.Caster;
035import lucee.runtime.op.date.DateCaster;
036import lucee.runtime.type.dt.DateTime;
037
038public final class LSDateFormat extends BIF implements Function {
039
040        private static final long serialVersionUID = 4720003854756942610L;
041        
042        public static String call(PageContext pc , Object object) throws PageException {
043                return _call(pc, object, "medium", pc.getLocale(),pc.getTimeZone());
044        }
045        public static synchronized String call(PageContext pc , Object object, String mask) throws PageException {
046                return _call(pc, object, mask, pc.getLocale(),pc.getTimeZone());
047        }
048        public static synchronized String call(PageContext pc , Object object, String mask,String strLocale) throws PageException {
049                return _call(pc, object, mask, LocaleFactory.getLocale(strLocale),pc.getTimeZone());
050        }
051        public static synchronized String call(PageContext pc , Object object, String mask,String strLocale,String strTimezone) throws PageException {
052                return _call(pc, object, mask, 
053                                strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale),
054                                strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone));
055        }
056        
057        
058        private static synchronized String _call(PageContext pc , Object object, String mask,Locale locale,TimeZone tz) throws PageException {
059                if(StringUtil.isEmpty(object)) return "";
060                
061                return new lucee.runtime.format.DateFormat(locale).
062                        format(toDateLS(pc ,locale,tz, object),mask,tz);
063        }
064
065        private static DateTime toDateLS(PageContext pc ,Locale locale, TimeZone timeZone, Object object) throws PageException {
066                if(object instanceof DateTime) return (DateTime) object;
067                else if(object instanceof CharSequence) {
068                        DateTime res = DateCaster.toDateTime(locale,Caster.toString(object),timeZone,null,locale.equals(Locale.US));
069                        if(res!=null)return res;
070                }
071                return DateCaster.toDateAdvanced(object,timeZone);
072        }
073        
074        @Override
075        public Object invoke(PageContext pc, Object[] args) throws PageException {
076                if (args.length == 1) {
077                        return call(pc, args[0]);
078                } else if (args.length == 2) {
079                return call(pc, args[0], Caster.toString(args[1]));
080        } else if (args.length == 3){
081                return call(pc, args[0], Caster.toString(args[1]), Caster.toString(args[2]));
082        } else {
083                return call(pc, args[0], Caster.toString(args[1]), Caster.toString(args[2]), Caster.toString(args[3]));         
084        }
085        }
086}