Date arithmetic #136
Date arithmetic #136
Conversation
attempting to add (not subtract) two dates raises an InvalidOperation parse error because there aren't any sane semantics for doing so while subtraction gives the duration between the dates, there's hardly any good reason to add them
now() - minutes(1) < now() && | ||
now() - seconds(1) < now() && | ||
now() - millis(1) < now() && | ||
now() - period("P1YT1M") < now() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if you do something like now() - years(3000)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then it's a long time ago. ;)
I've added a test
rule "date math" | ||
when | ||
now() + years(1) > now() && | ||
now() + months(1) > now() && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding some tests that check the actual result? Otherwise the tests do not catch the case when a wrong period is used in a function. (e.g. Period.years()
vs. Period.months()
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I'll put those into the functions test suite.
add test for subtracting 10000 years
This change introduces simple arithmetics on DateTime and Period types.
Supported operations are:
DateTime
instances from each other, yielding theDuration
between themPeriod
and aDateTime
instance (commutative operation)Period
sTo be able to have type safety for these operations, various
Period
creating functions are introduced:years()
months()
weeks()
days()
hours()
minutes()
seconds()
millis()
period()
taking an ISO8601 period string for arbitrary periodsThe date types can also be compared with all equality and comparison functions.