November 20, 2008, 09:39:50 am *
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Identifing compare Timestamp with Date  (Read 3376 times)
bwburch
Newbie
*
Posts: 4


« on: August 29, 2007, 07:52:23 pm »

I would like to create a query that can identify when a compare is done between a Timestamp and a Date.

A compare such as java.sql.Timestamp.after(java.sql.Date) will always assert true even when the
Timestamp = 2007-8-28 and Date = 2007-8-28.

But when the compare is reversed java.sql.Date.after(java.sql.Timestamp) will assert correctly depending on the
dates provides.

I was trying to create the query but I'm having difficulty.

Thanks,
Brett
Logged
Mathieu Verbaere
Administrator
Newbie
*****
Posts: 26


« Reply #1 on: August 30, 2007, 11:54:30 am »

Hi Brett,

I am not sure about the problem you are trying to solve - it seems to work fine at least with Java 6. In both cases, java.util.Date.after(..) should be called, and we store that fact. We do not store the static type of the receiver and arguments of calls yet. This is also something we plan to add soon. With just that additional information however, your query will likely miss cases where only the *dynamic* type of the receiver is Timestamp.

In the meantime, if you want to spot all possible date comparisons, you can try:

Code:
class DateCompareMethod extends Method {
  DateCompareMethod() {
    this.getDeclaringType().hasQualifiedName("java.util","Date") and
    (this.hasName("before") or this.hasName("after") or this.hasName("compareTo"))
  }
}

from Call c
where c.getCallee() instanceof DateCompareMethod
select c, "Is this comparison correct?"

We first create a special class of methods "DateCompareMethod" that consists of
java.util.Date.after(..), java.util.Date.before(..), java.util.Date.compareTo(..).

Then we search for calls where the callee is one of these methods.

Hope it helps.
Logged
bwburch
Newbie
*
Posts: 4


« Reply #2 on: August 30, 2007, 01:13:16 pm »

Thanks for the solution it works great.

The issue that I'm trying to solve occurs when you define a Timestamp in a database, say oracle and you have a user enter a date on a form (or another date field in a database) and try to compare the two.  Depending on the method you choose Timestamp.after(date) or date.after(Timestamp) you will get two different answers even if the "dates" are the same.

Example:
Timestamp: 2007-08-22 07:30:20.111111 Date: 2007-08-22
Is Timestamp After Date:true
Is Timestamp After Date:1
Timestamp: 2007-08-22 07:30:20.111111 Date: 2007-08-22
Is Date After Timestamp:false
Is Date After Timestamp:-1
Logged
Pages: [1]
  Print  
 
Jump to: