For representing times, there are two distinct data types:
- absolute time, as a sequence of points on a timeline (the real "Time");
- durations, which are differences between points on a timeline.
Absolute time can be represented in a number of ways:
- as seconds (or milliseconds or nanoseconds), with 0 being some agreed-upon
point in Time,
- by Y,M,D,h,m,s according to some calendar;
- as Julian Days, in fractions.
It's obvious that calculations of any kind are best done with one of the
single-valued representations.
Durations can be expressed in any convenient unit, as long as there is a
well-defined conversion between the units with which you represent absolute
time and the units you desire to express durations.
There is no such well-defined conversion for (months and) years, and
therefore the idea to express a duration (precisely) in years is not used
commonly. (Astronomers settle for a year being equal to 365.25 Julian days;
so if this suits you, go ahead and base your calculations on that.)
For efficiency in rules, you might consider using Date.getTime() (or
Calendar.getTimeInMillis()) to obtain a point on the timeline, and to
express your notion of a year's duration (whatever it is) in the suitable
unit, and to use this for comparisons.
-W
On Thu, Feb 18, 2010 at 1:43 PM, Pavel Tavoda <pavel.tavoda(a)gmail.com>wrote:
Hello,
I would like to calculate AGE of object based on Date inside object, I
can use eval but I hope it's so basic functionality that Drools should
have it. It's also performance problem if I need to use EVAL in many
rules (let's say 100 in XLS). I'm playing with some FUSION features
like:
$now : Now() && $order : SecurityOrder(this before[400d, 0d] $now)
should work but I can't specify YEARS as constant. That's a problem
for long time because 30*365d isn't same as 30 years if you consider
leap years. Is it possible some easy way to calculate AGE of something
with correct time handling and compare to years? Something like:
SecurityOrder( age(createdDate) > 20y, age(createdDate) < 25y)
or by FUSION:
$now : Now() && $order : SecurityOrder(this before[25y, 20y] $now)
For now I'm using following function in eval() but it's becoming
performance problem:
function boolean between(Calendar currentDate, Date oldDate, int from, int
to) {
Calendar fromCal = Calendar.getInstance();
fromCal.setTime(oldDate);
fromCal.add(Calendar.YEAR, from);
Calendar toCal = Calendar.getInstance();
toCal.setTime(oldDate);
toCal.add(Calendar.YEAR, to);
return currentDate.after(fromCal) && currentDate.before(toCal);
}
Thank you
Pavel
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users