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 datecompare
021 */
022package lucee.runtime.functions.dateTime;
023
024import java.util.Calendar;
025import java.util.Locale;
026import java.util.TimeZone;
027
028import lucee.commons.date.JREDateTimeUtil;
029import lucee.runtime.PageContext;
030import lucee.runtime.engine.ThreadLocalPageContext;
031import lucee.runtime.exp.ExpressionException;
032import lucee.runtime.exp.FunctionException;
033import lucee.runtime.exp.PageException;
034import lucee.runtime.functions.BIF;
035import lucee.runtime.op.Caster;
036import lucee.runtime.type.dt.DateTime;
037
038public final class DateCompare extends BIF {
039
040        public static double call(PageContext pc , DateTime left, DateTime right) throws ExpressionException {
041                return call(pc , left, right,"s");
042        }
043        
044        public static double call(PageContext pc , DateTime left, DateTime right, String datepart) throws ExpressionException {
045                datepart=datepart.toLowerCase().trim();
046                TimeZone tz=ThreadLocalPageContext.getTimeZone(pc);
047                Calendar cLeft=JREDateTimeUtil.getThreadCalendar(tz);
048                cLeft.setTime(left);
049                
050                Calendar cRight=JREDateTimeUtil.newInstance(tz,Locale.US);
051                cRight.setTime(right);
052                
053                // TODO WEEEK
054                
055                int type=0;
056                if(datepart.equals("s"))                type=Calendar.SECOND;           
057                else if(datepart.equals("n"))   type=Calendar.MINUTE;           
058                else if(datepart.equals("h"))   type=Calendar.HOUR;             
059                else if(datepart.equals("d"))   type=Calendar.DATE;     
060                else if(datepart.equals("m"))   type=Calendar.MONTH;
061                else if(datepart.equals("y"))   type=Calendar.DATE;
062                else if(datepart.equals("yyyy"))type=Calendar.YEAR;
063                else {
064                        throw new FunctionException(pc,"dateCompare",3,"datePart","invalid value ["+datepart+"], valid values has to be [s,n,h,d,m,y,yyyy]");
065                }
066                
067                // Year
068                int value = cLeft.get(Calendar.YEAR)-cRight.get(Calendar.YEAR);
069                if(value!=0) return value>0?1:-1;
070                if(Calendar.YEAR==type) return 0;
071                if(Calendar.YEAR==type) return 0;
072                
073                // Month
074                value = cLeft.get(Calendar.MONTH)-cRight.get(Calendar.MONTH);
075                if(value!=0) return value>0?1:-1;
076                if(Calendar.MONTH==type) return 0;
077                
078                // Day
079                value = cLeft.get(Calendar.DATE)-cRight.get(Calendar.DATE);
080                if(value!=0) return value>0?1:-1;
081                if(Calendar.DATE==type) return 0;
082                
083                // Hour
084                //print.out(cLeft.get(Calendar.HOUR_OF_DAY)+"-"+cRight.get(Calendar.HOUR_OF_DAY));
085                value = cLeft.get(Calendar.HOUR_OF_DAY)-cRight.get(Calendar.HOUR_OF_DAY);
086                if(value!=0) return value>0?1:-1;
087                if(Calendar.HOUR==type) return 0;
088                
089                // Minute
090                value = cLeft.get(Calendar.MINUTE)-cRight.get(Calendar.MINUTE);
091                if(value!=0) return value>0?1:-1;
092                if(Calendar.MINUTE==type) return 0;
093                
094                // Second
095                value = cLeft.get(Calendar.SECOND)-cRight.get(Calendar.SECOND);
096                if(value!=0) return value>0?1:-1;
097                return 0;
098                        
099        }
100        
101        @Override
102        public Object invoke(PageContext pc, Object[] args) throws PageException {
103                if(args.length==2)return call(pc,Caster.toDatetime(args[0],pc.getTimeZone()),Caster.toDatetime(args[1],pc.getTimeZone()));
104                return call(pc,Caster.toDatetime(args[0],pc.getTimeZone()),Caster.toDatetime(args[1],pc.getTimeZone()),Caster.toString(args[2]));
105        }
106
107}