001    /**
002     * Implements the CFML Function lsparsedatetime
003     */
004    package railo.runtime.functions.international;
005    
006    import java.text.ParseException;
007    import java.text.SimpleDateFormat;
008    import java.util.Date;
009    import java.util.Locale;
010    import java.util.TimeZone;
011    
012    import railo.commons.date.TimeZoneUtil;
013    import railo.commons.lang.StringUtil;
014    import railo.runtime.PageContext;
015    import railo.runtime.engine.ThreadLocalPageContext;
016    import railo.runtime.exp.PageException;
017    import railo.runtime.ext.function.Function;
018    import railo.runtime.i18n.LocaleFactory;
019    import railo.runtime.op.Caster;
020    import railo.runtime.op.date.DateCaster;
021    import railo.runtime.type.dt.DateTimeImpl;
022    
023    public final class LSParseDateTime implements Function {
024            
025            private static final long serialVersionUID = 7808039073301229473L;
026            
027            public static railo.runtime.type.dt.DateTime call(PageContext pc , Object oDate) throws PageException {
028                    return _call(pc, oDate, pc.getLocale(),pc.getTimeZone(),null);
029            }
030            
031            public static railo.runtime.type.dt.DateTime call(PageContext pc , Object oDate,String strLocale) throws PageException {
032                    return _call(pc, oDate, LocaleFactory.getLocale(strLocale),pc.getTimeZone(),null);
033            }
034            
035            public static railo.runtime.type.dt.DateTime call(PageContext pc , Object oDate,String strLocale,String strTimezoneOrFormat) throws PageException {
036                    if(strTimezoneOrFormat==null) {
037                            return _call(pc, oDate, LocaleFactory.getLocale(strLocale),pc.getTimeZone(),null);
038                    }
039                    Locale locale = strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale);
040                    TimeZone tz = TimeZoneUtil.toTimeZone(strTimezoneOrFormat,null);
041                    if(tz!=null)
042                            return _call(pc, oDate, locale,tz,null);
043                    return _call(pc, oDate, locale,pc.getTimeZone(),strTimezoneOrFormat);
044            }
045    
046            
047            public static railo.runtime.type.dt.DateTime call(PageContext pc , Object oDate,String strLocale,String strTimezone, String strFormat) throws PageException {
048                    return _call(pc, oDate, 
049                                    strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale),
050                                    strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone),strFormat);
051            }
052            
053            private static railo.runtime.type.dt.DateTime _call(PageContext pc , Object oDate,Locale locale,TimeZone tz, String format) throws PageException {
054                    if(oDate instanceof Date) return Caster.toDate(oDate, tz);
055    
056                    String strDate = Caster.toString(oDate);
057                    
058                    // regular parse date time
059                    if(StringUtil.isEmpty(format,true))
060                            return DateCaster.toDateTime(locale,strDate,tz,locale.equals(Locale.US));
061                    
062                    
063                    // with java based format
064                    tz=ThreadLocalPageContext.getTimeZone(tz);
065            if(locale==null)locale=pc.getLocale();
066                    SimpleDateFormat df = new SimpleDateFormat(format, locale);
067                    df.setTimeZone(tz);
068                    try {
069                            return new DateTimeImpl(df.parse(strDate));
070                    } catch (ParseException e) {
071                            throw Caster.toPageException(e);
072                    }
073            }
074    }