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.CasterException;
011    import railo.runtime.exp.PageException;
012    import railo.runtime.ext.function.Function;
013    import railo.runtime.op.date.DateCaster;
014    import railo.runtime.type.dt.DateTime;
015    
016    /**
017     * Implements the CFML Function dateformat
018     */
019    public final class DateFormat implements Function {
020            
021            public static String call(PageContext pc , Object object) throws PageException {
022                    return _call(pc,object,"dd-mmm-yy",ThreadLocalPageContext.getTimeZone(pc));
023            }
024            public static String call(PageContext pc , Object object, String mask) throws PageException {
025                    return _call(pc, object, mask, ThreadLocalPageContext.getTimeZone(pc));
026            }
027            public static String call(PageContext pc , Object object, String mask,String strTimezone) throws PageException {
028                    return _call(pc, object, mask, strTimezone==null?ThreadLocalPageContext.getTimeZone(pc):TimeZoneUtil.toTimeZone(strTimezone));
029            }
030            private static String _call(PageContext pc , Object object, String mask,TimeZone tz) throws PageException {
031                Locale locale=Locale.US;
032                
033                    DateTime datetime = DateCaster.toDateAdvanced(object,tz,null);
034                            //Caster.toDate(object,true,tz,null);
035                    if(datetime==null) {
036                            if(StringUtil.isEmpty(object,true)) return "";
037                        throw new CasterException(object,"datetime");
038                        //if(!Decision.isSimpleValue(object))
039                        //    throw new ExpressionException("can't convert object of type "+Type.getName(object)+" to a datetime value");
040                        //throw new ExpressionException("can't convert value "+object+" to a datetime value");
041                    }
042                    return new railo.runtime.format.DateFormat(locale).format(datetime,mask,tz);
043            }
044    }