[rules-users] Problem with temporal operations spanning daylight to standard time

lhorton LHorton at ABCLegal.com
Tue Mar 13 17:29:57 EDT 2012


Most of the USA went from Standard Time to Daylight Time this past weekend. 
We use some of the temporal operations in our rules, mainly to do
calculations of date differences by whole days, and some of the calculations
are wrong.  The ones in error involve comparing dates where one is standard
time and one is daylight savings time.  I wrote a small test case to prove
this.

I have a simple Person class:
public class Person  {

	private Date birthdate;
	private int status;

	public Date getBirthdate() {
		return birthdate;
	}

	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}
}

and a simple drl file:

package com.drools.resources;

import com.drools.person.Person;
import java.util.Date;
import java.util.Calendar;

global Long now

rule "Birthday Rule"
dialect "mvel"
when
	$person : Person(birthdate != null)
	Date($bDate : time before[3d] now) from $person.birthdate
then
	$person.setStatus(3);
end

rule "Debug Birthday Rule"
dialect "mvel"
when
	$person : Person(birthdate != null)
then
	System.out.println("now is " + new Date(now));
	System.out.println("birthdate is " + $person.getBirthdate());
end

and two tests:
	@Test
	public void testTemporalJanuary() throws Exception {
		KnowledgeBase base =
createKnowledgeBaseFromRulesFile("src/com/drools/resources/temporal.drl");
		StatelessKnowledgeSession kSession =
createStatelessKnowledgeSession(base);

		// test january - both dates are standard time
		Date now = new Date(2012-1900, 00, 13);
		kSession.getGlobals().set("now", now.getTime());
		Person p = new Person();
		p.setStatus(0);
		p.setBirthdate(new Date(2012-1900, 00, 10));
		kSession.execute(p);
		assertTrue(p.getStatus() == 3);

	}

	@Test
	public void testTemporalMarch() throws Exception {
		KnowledgeBase base =
createKnowledgeBaseFromRulesFile("src/com/drools/resources/temporal.drl");
		StatelessKnowledgeSession kSession =
createStatelessKnowledgeSession(base);

		// test March: one day standard, the other is daylight
		Date now = new Date(2012-1900, 02, 13);
		kSession.getGlobals().set("now", now.getTime());
		Person p = new Person();
		p.setStatus(0);
		p.setBirthdate(new Date(2012-1900, 02, 10));
		kSession.execute(p);
		assertTrue(p.getStatus() == 3);


	}


the first test (testTemporalJanuary) compares January 13 to January 10, and
it passes (matches the before[3d] condition).

the second test (testTemporalMarch, which is the same except for using March
instead of January) fails.

The output of the debug rule for these tests is:

   now is Fri Jan 13 00:00:00 PST 2012
   birthdate is Tue Jan 10 00:00:00 PST 2012

   now is Tue Mar 13 00:00:00 PDT 2012
   birthdate is Sat Mar 10 00:00:00 PST 2012




--
View this message in context: http://drools.46999.n3.nabble.com/Problem-with-temporal-operations-spanning-daylight-to-standard-time-tp3823618p3823618.html
Sent from the Drools: User forum mailing list archive at Nabble.com.



More information about the rules-users mailing list