001    /**
002     * Implements the CFML Function lsdateformat
003     */
004    package railo.runtime.functions.international;
005    
006    import java.util.Locale;
007    import java.util.TimeZone;
008    
009    import railo.commons.date.TimeZoneUtil;
010    import railo.commons.lang.StringUtil;
011    import railo.runtime.PageContext;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.ext.function.Function;
014    import railo.runtime.i18n.LocaleFactory;
015    import railo.runtime.op.Caster;
016    import railo.runtime.op.date.DateCaster;
017    import railo.runtime.type.dt.DateTime;
018    
019    public final class LSDateFormat implements Function {
020    
021            private static final long serialVersionUID = 4720003854756942610L;
022            
023            //private static Calendar calendar;
024            public static String call(PageContext pc , Object object) throws PageException {
025                    return _call(pc, object, "medium", pc.getLocale(),pc.getTimeZone());
026            }
027            public static synchronized String call(PageContext pc , Object object, String mask) throws PageException {
028                    return _call(pc, object, mask, pc.getLocale(),pc.getTimeZone());
029            }
030            public static synchronized String call(PageContext pc , Object object, String mask,String strLocale) throws PageException {
031                    return _call(pc, object, mask, LocaleFactory.getLocale(strLocale),pc.getTimeZone());
032            }
033            public static synchronized String call(PageContext pc , Object object, String mask,String strLocale,String strTimezone) throws PageException {
034                    return _call(pc, object, mask, 
035                                    strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale),
036                                    strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone));
037            }
038            
039            
040            private static synchronized String _call(PageContext pc , Object object, String mask,Locale locale,TimeZone tz) throws PageException {
041                    if(StringUtil.isEmpty(object)) return "";
042                    
043                    return new railo.runtime.format.DateFormat(locale).
044                            format(toDateLS(pc ,locale,tz, object),mask,tz);
045            }
046    
047            private static DateTime toDateLS(PageContext pc ,Locale locale, TimeZone timeZone, Object object) throws PageException {
048                    if(object instanceof DateTime) return (DateTime) object;
049                    else if(object instanceof CharSequence) {
050                            DateTime res = DateCaster.toDateTime(locale,Caster.toString(object),timeZone,null,locale.equals(Locale.US));
051                            if(res!=null)return res;
052                    }
053                    return DateCaster.toDateAdvanced(object,timeZone);
054            }
055    }