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