001    package railo.runtime.functions.displayFormatting;
002    
003    import java.util.Locale;
004    import java.util.TimeZone;
005    
006    import railo.commons.date.TimeZoneUtil;
007    import railo.commons.lang.StringUtil;
008    import railo.runtime.PageContext;
009    import railo.runtime.engine.ThreadLocalPageContext;
010    import railo.runtime.exp.ExpressionException;
011    import railo.runtime.ext.function.Function;
012    import railo.runtime.op.Caster;
013    import railo.runtime.type.dt.DateTime;
014    
015    /**
016     * Implements the CFML Function dateformat
017     */
018    public final class TimeFormat implements Function {
019            
020            /**
021             * @param pc
022             * @param object
023             * @return Formated Time Object as String
024             * @throws ExpressionException
025             */
026            public static String call(PageContext pc , Object object) throws ExpressionException {
027                    return _call(pc,object,"hh:mm tt",ThreadLocalPageContext.getTimeZone(pc));
028            }
029            
030            /**
031             * @param pc
032             * @param object
033             * @param mask Characters that show how CFML displays a date:
034             * @return Formated Time Object as String
035             * @throws ExpressionException
036             */
037            public static String call(PageContext pc , Object object, String mask) throws ExpressionException {
038                    return _call(pc,object,mask,ThreadLocalPageContext.getTimeZone(pc));
039            }
040    
041            public static String call(PageContext pc , Object object, String mask,String strTimezone) throws ExpressionException {
042                    return _call(pc,object,mask, strTimezone==null?ThreadLocalPageContext.getTimeZone(pc):TimeZoneUtil.toTimeZone(strTimezone));
043            }
044            
045            private static String _call(PageContext pc , Object object, String mask,TimeZone tz) throws ExpressionException {
046                    Locale locale=Locale.US;//:pc.getConfig().getLocale();
047                    DateTime datetime = Caster.toDate(object,true,tz,null);
048                    if(datetime==null) {
049                        if(StringUtil.isEmpty(object,true)) return "";
050                        throw new ExpressionException("can't convert value "+object+" to a datetime value");
051                    }
052                    
053                    
054                    return new railo.runtime.format.TimeFormat(locale).format(datetime,mask,tz);
055                    //return new railo.runtime.text.TimeFormat(locale).format(datetime,mask);
056            }
057    }