001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019/**
020 * Implements the CFML Function lsisdate
021 */
022package lucee.runtime.functions.international;
023
024import java.util.Date;
025import java.util.Locale;
026import java.util.TimeZone;
027
028import lucee.commons.date.TimeZoneUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.exp.ExpressionException;
031import lucee.runtime.ext.function.Function;
032import lucee.runtime.i18n.LocaleFactory;
033import lucee.runtime.op.Decision;
034
035public final class LSIsDate implements Function {
036
037        private static final long serialVersionUID = -8517171925554806088L;
038
039
040        public static boolean call(PageContext pc , Object object) {
041                return call(pc, object, pc.getLocale(),pc.getTimeZone());
042        }
043
044        public static boolean call(PageContext pc , Object object,String strLocale) throws ExpressionException {
045                return call(pc, object, LocaleFactory.getLocale(strLocale),pc.getTimeZone());
046        }
047        public static boolean call(PageContext pc , Object object,String strLocale,String strTimezone) throws ExpressionException {
048                return call(pc, object, 
049                                strLocale==null?pc.getLocale():LocaleFactory.getLocale(strLocale),
050                                strTimezone==null?pc.getTimeZone():TimeZoneUtil.toTimeZone(strTimezone));
051        }
052        
053        
054        private static boolean call(PageContext pc  , Object object,Locale locale,TimeZone tz) {
055                if(object instanceof Date) return true;
056                else if(object instanceof String) {
057                    String str=object.toString();
058                    if(str.length()<2) return false;
059                    //print.out(Caster.toDateTime(locale,str,pc.getTimeZone(),null));
060                        return Decision.isDate(str,locale,tz,locale.equals(Locale.US));
061                        //return Caster.toDateTime(locale,str,pc.getTimeZone(),null)!=null;
062                }
063                return false;
064        }
065        
066}