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.runtime.PageContext;
026import lucee.runtime.engine.ThreadLocalPageContext;
027import lucee.runtime.exp.ExpressionException;
028import lucee.runtime.ext.function.Function;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.dt.DateTime;
031
032/**
033 * Implements the CFML Function dateformat
034 */
035public final class DateTimeFormatClassic implements Function {
036
037        private static final long serialVersionUID = 134840879454373440L;
038
039        /**
040         * @param pc
041         * @param object
042         * @return Formated Time Object as String
043         * @throws ExpressionException
044         */
045        public static String call(PageContext pc , Object object) throws ExpressionException {
046                return _call(pc,object,"dd-mmm-yy hh:nn tt",ThreadLocalPageContext.getTimeZone(pc));
047        }
048        
049        /**
050         * @param pc
051         * @param object
052         * @param mask Characters that show how CFML displays a date:
053         * @return Formated Time Object as String
054         * @throws ExpressionException
055         */
056        public static String call(PageContext pc , Object object, String mask) throws ExpressionException {
057                return _call(pc,object,mask,ThreadLocalPageContext.getTimeZone(pc));
058        }
059
060        public static String call(PageContext pc , Object object, String mask,String strTimezone) throws ExpressionException {
061                return _call(pc,object,mask, strTimezone==null?ThreadLocalPageContext.getTimeZone(pc):TimeZoneUtil.toTimeZone(strTimezone));
062        }
063        
064        private static String _call(PageContext pc , Object object, String mask,TimeZone tz) throws ExpressionException {
065                Locale locale=Locale.US;//:pc.getConfig().getLocale();
066                DateTime datetime = Caster.toDate(object,true,tz,null);
067                if(datetime==null) {
068                    if(object.toString().trim().length()==0) return "";
069                    throw new ExpressionException("can't convert value "+object+" to a datetime value");
070                }
071                
072                
073                return new lucee.runtime.format.DateTimeFormat(locale).format(datetime,mask,tz);
074                //return new lucee.runtime.text.TimeFormat(locale).format(datetime,mask);
075        }
076}