001    /**
002     * Implements the Cold Fusion Function datecompare
003     */
004    package railo.runtime.functions.dateTime;
005    
006    import java.util.Calendar;
007    import java.util.TimeZone;
008    
009    import railo.commons.date.JREDateTimeUtil;
010    import railo.runtime.PageContext;
011    import railo.runtime.engine.ThreadLocalPageContext;
012    import railo.runtime.exp.ExpressionException;
013    import railo.runtime.exp.FunctionException;
014    import railo.runtime.ext.function.Function;
015    import railo.runtime.type.dt.DateTime;
016    
017    public final class DateCompare implements Function {
018    
019            public static double call(PageContext pc , DateTime left, DateTime right) throws ExpressionException {
020                    return call(pc , left, right,"s");
021            }
022            
023            public static double call(PageContext pc , DateTime left, DateTime right, String datepart) throws ExpressionException {
024                    datepart=datepart.toLowerCase().trim();
025                    TimeZone tz=ThreadLocalPageContext.getTimeZone(pc);
026                    Calendar cLeft=JREDateTimeUtil.getCalendar();
027                    cLeft.setTimeZone(tz);
028                    cLeft.setTime(left);
029                    
030                    Calendar cRight=JREDateTimeUtil.newInstance();
031                    cRight.setTimeZone(tz);
032                    cRight.setTime(right);
033                    
034                    // TODO WEEEK
035                    
036                    int type=0;
037                    if(datepart.equals("s"))                type=Calendar.SECOND;           
038                    else if(datepart.equals("n"))   type=Calendar.MINUTE;           
039                    else if(datepart.equals("h"))   type=Calendar.HOUR;             
040                    else if(datepart.equals("d"))   type=Calendar.DATE;     
041                    else if(datepart.equals("m"))   type=Calendar.MONTH;
042                    else if(datepart.equals("y"))   type=Calendar.DATE;
043                    else if(datepart.equals("yyyy"))type=Calendar.YEAR;
044                    else {
045                            throw new FunctionException(pc,"dateCompare",3,"datePart","invalid value ["+datepart+"], valid values has to be [s,n,h,d,m,y,yyyy]");
046                    }
047                    
048                    // Year
049                    int value = cLeft.get(Calendar.YEAR)-cRight.get(Calendar.YEAR);
050                    if(value!=0) return value>0?1:-1;
051                    if(Calendar.YEAR==type) return 0;
052                    if(Calendar.YEAR==type) return 0;
053                    
054                    // Month
055                    value = cLeft.get(Calendar.MONTH)-cRight.get(Calendar.MONTH);
056                    if(value!=0) return value>0?1:-1;
057                    if(Calendar.MONTH==type) return 0;
058                    
059                    // Day
060                    value = cLeft.get(Calendar.DATE)-cRight.get(Calendar.DATE);
061                    if(value!=0) return value>0?1:-1;
062                    if(Calendar.DATE==type) return 0;
063                    
064                    // Hour
065                    //print.out(cLeft.get(Calendar.HOUR_OF_DAY)+"-"+cRight.get(Calendar.HOUR_OF_DAY));
066                    value = cLeft.get(Calendar.HOUR_OF_DAY)-cRight.get(Calendar.HOUR_OF_DAY);
067                    if(value!=0) return value>0?1:-1;
068                    if(Calendar.HOUR==type) return 0;
069                    
070                    // Minute
071                    value = cLeft.get(Calendar.MINUTE)-cRight.get(Calendar.MINUTE);
072                    if(value!=0) return value>0?1:-1;
073                    if(Calendar.MINUTE==type) return 0;
074                    
075                    // Second
076                    value = cLeft.get(Calendar.SECOND)-cRight.get(Calendar.SECOND);
077                    if(value!=0) return value>0?1:-1;
078                    return 0;
079                            
080            }
081    
082    }