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