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