Skip to content
This repository has been archived by the owner on Mar 21, 2023. It is now read-only.

Date arithmetic #136

Merged
merged 5 commits into from Nov 24, 2016
Merged

Date arithmetic #136

merged 5 commits into from Nov 24, 2016

Conversation

kroepke
Copy link
Member

@kroepke kroepke commented Nov 22, 2016

This change introduces simple arithmetics on DateTime and Period types.

Supported operations are:

  • subtracting two DateTime instances from each other, yielding the Duration between them
  • adding two dates makes no sense and is not allowed (statically checked)
  • adding/subtracting a Period and a DateTime instance (commutative operation)
  • adding/subtracting two Periods

To 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 periods

The date types can also be compared with all equality and comparison functions.

@kroepke kroepke added this to the 2.2.0 milestone Nov 22, 2016
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()
Copy link
Member

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)?

Copy link
Member Author

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() &&
Copy link
Member

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())

Copy link
Member Author

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
@bernd bernd merged commit 255a919 into master Nov 24, 2016
@bernd bernd deleted the issue-91 branch November 24, 2016 09:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants