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.runtime.PageContext;
008    import railo.runtime.engine.ThreadLocalPageContext;
009    import railo.runtime.exp.ExpressionException;
010    import railo.runtime.ext.function.Function;
011    import railo.runtime.op.Caster;
012    import railo.runtime.type.dt.DateTime;
013    
014    /**
015     * Implements the Cold Fusion Function dateformat
016     */
017    public final class DateTimeFormat implements Function {
018    
019            private static final long serialVersionUID = 134840879454373440L;
020    
021            /**
022             * @param pc
023             * @param object
024             * @return Formated Time Object as String
025             * @throws ExpressionException
026             */
027            public static String call(PageContext pc , Object object) throws ExpressionException {
028                    return _call(pc,object,"dd-mmm-yy hh:nn tt",ThreadLocalPageContext.getTimeZone(pc));
029            }
030            
031            /**
032             * @param pc
033             * @param object
034             * @param mask Characters that show how CFML displays a date:
035             * @return Formated Time Object as String
036             * @throws ExpressionException
037             */
038            public static String call(PageContext pc , Object object, String mask) throws ExpressionException {
039                    return _call(pc,object,mask,ThreadLocalPageContext.getTimeZone(pc));
040            }
041    
042            public static String call(PageContext pc , Object object, String mask,String strTimezone) throws ExpressionException {
043                    return _call(pc,object,mask, strTimezone==null?ThreadLocalPageContext.getTimeZone(pc):TimeZoneUtil.toTimeZone(strTimezone));
044            }
045            
046            private static String _call(PageContext pc , Object object, String mask,TimeZone tz) throws ExpressionException {
047                    Locale locale=Locale.US;//:pc.getConfig().getLocale();
048                    DateTime datetime = Caster.toDate(object,true,tz,null);
049                    if(datetime==null) {
050                        if(object.toString().trim().length()==0) return "";
051                        throw new ExpressionException("can't convert value "+object+" to a datetime value");
052                    }
053                    
054                    
055                    return new railo.runtime.format.DateTimeFormat(locale).format(datetime,mask,tz);
056                    //return new railo.runtime.text.TimeFormat(locale).format(datetime,mask);
057            }
058    }