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.displayFormatting;
020
021import java.util.Locale;
022import java.util.TimeZone;
023
024import lucee.commons.date.TimeZoneUtil;
025import lucee.commons.lang.StringUtil;
026import lucee.runtime.PageContext;
027import lucee.runtime.engine.ThreadLocalPageContext;
028import lucee.runtime.exp.CasterException;
029import lucee.runtime.exp.PageException;
030import lucee.runtime.ext.function.Function;
031import lucee.runtime.functions.BIF;
032import lucee.runtime.op.Caster;
033import lucee.runtime.op.date.DateCaster;
034import lucee.runtime.type.dt.DateTime;
035
036/**
037 * Implements the CFML Function dateformat
038 */
039public final class DateFormat extends BIF implements Function {
040        
041        public static String call(PageContext pc , Object object) throws PageException {
042                return _call(pc,object,"dd-mmm-yy",ThreadLocalPageContext.getTimeZone(pc));
043        }
044        public static String call(PageContext pc , Object object, String mask) throws PageException {
045                return _call(pc, object, mask, ThreadLocalPageContext.getTimeZone(pc));
046        }
047        public static String call(PageContext pc , Object object, String mask,String strTimezone) throws PageException {
048                return _call(pc, object, mask, strTimezone==null?ThreadLocalPageContext.getTimeZone(pc):TimeZoneUtil.toTimeZone(strTimezone));
049        }
050
051    @Override
052    public String invoke(PageContext pc, Object[] args) throws PageException {
053        if (args.length == 1) {
054            return call(pc, args[0]);
055        } else if (args.length == 2) {
056            return call(pc, args[0], Caster.toString(args[1]));
057        } else {
058            return call(pc, args[0], Caster.toString(args[1]), Caster.toString(args[2]));
059        }
060    }
061
062        private static String _call(PageContext pc , Object object, String mask,TimeZone tz) throws PageException {
063            Locale locale=Locale.US;
064            
065                DateTime datetime = DateCaster.toDateAdvanced(object,tz,null);
066                        //Caster.toDate(object,true,tz,null);
067                if(datetime==null) {
068                        if(StringUtil.isEmpty(object,true)) return "";
069                    throw new CasterException(object,"datetime");
070                    //if(!Decision.isSimpleValue(object))
071                    //    throw new ExpressionException("can't convert object of type "+Type.getName(object)+" to a datetime value");
072                    //throw new ExpressionException("can't convert value "+object+" to a datetime value");
073                }
074                return new lucee.runtime.format.DateFormat(locale).format(datetime,mask,tz);
075        }
076}