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

Fix/dates 141 #143

Merged
merged 6 commits into from
Jan 18, 2022
Merged

Fix/dates 141 #143

merged 6 commits into from
Jan 18, 2022

Conversation

MartijnR
Copy link
Member

@MartijnR MartijnR commented Nov 15, 2021

Fixes enketo/enketo#866 (likely caused by a date misunderstanding on my part when guiding the development of this new evaluator - hence the incorrect test responses).

  1. This fixes a behavior change with date arithmetic in the new evaluator and ensures that old forms will again continue to work as before.
  2. It also fixes an existing date comparison bug that was discovered while working on this issue where e.g. date("2100-01-02") > 2 returns false instead of true (ODK Collect does it correctly).
  3. It also fixes an existing bug with decimal-date-time when the argument is a number. The fix for 1 surfaced this issue with e.g. a complex expression like decimal-date-time(today()- 60 ) for which we had a test.

A remaining difference with ODK Collect is that Enketo adds the local timezone offset. However, since it has been doing that since forever, it was considered outside the scope of this issue to investigate. I added comments to these tests though.

@@ -154,7 +154,7 @@ const openrosa_xpath_extensions = function() {
'decimal-date-time': function(r) {
if(arguments.length > 1) throw TOO_MANY_ARGS;

const days = dateStringToDays(asString(r));
const days = r.t === 'num' ? asNumber(r) : dateStringToDays(asString(r));
Copy link
Member Author

@MartijnR MartijnR Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While fixing 141 a bug in decimal-date-time surfaced. It did not convert number arguments into dateTimes.


if (lhs.t === 'date') lhs = { t:'num', v:dateToDays(lhs.v) };
if (rhs.t === 'date') rhs = { t:'num', v:dateToDays(rhs.v) };

Copy link
Member Author

@MartijnR MartijnR Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When converting dates to numbers if used as math operands or for comparisons, I believe we no longer need this complexity. Yay! 🥳

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens for logical or/and operators?

Copy link
Member Author

@MartijnR MartijnR Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look and date strings '1969-12-30' or date('1969-12-30') are always true so I guess that's maybe bad.... 🤔 I cannot actually think of a use case where you would do date or date or date and date because it would be about an epoch check.

Things like this do work (and I'll add these test just in case):

      ["('2001-12-26' > '2001-12-25') or false()", true],
      ["false() or ('2001-12-26' > '2001-12-25')", true],
      ["('2001-12-26' < '2001-12-25') or false()", false],
      ["false() or ('2001-12-26' < '2001-12-25')", false],
      ["('2001-12-26' < '2001-12-25') and true()", false],
      ["true() and ('2001-12-26' < '2001-12-25')", false],
      ["('2001-12-26' > '2001-12-25') and true()", true],
      ["true() and ('2001-12-26' > '2001-12-25')", true],

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off the top of my head this makes sense - <date> or <date> would always be true, and the same with <date> and <date>... unless one or both were invalid dates (which there may not be support for).

I don't know if those examples add much, as they are really testing <date> greater-than <date> and <date>less-than <date>, and then <boolean> and <boolean>/<boolean> or <boolean>.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed. Thank you. I didn't feel good about those tests actually. Am going to revert them! :)

@MartijnR
Copy link
Member Author

MartijnR commented Nov 15, 2021

  • Martijn still to double-check that some of these changed tests return similar number results in Collect (minus the timezone)

@MartijnR
Copy link
Member Author

Cannot request via GitHub, but if you happen to have some time, a review would be very welcome @alxndrsn!

@alxndrsn
Copy link
Contributor

alxndrsn commented Nov 23, 2021

@MartijnR I think the date handling (edit: in OR) is a bit nuts, but within that context this all looks great 😁

@MartijnR MartijnR mentioned this pull request Dec 6, 2021
@MartijnR MartijnR merged commit 4ad7ad7 into master Jan 18, 2022
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.

date arithmetic returns date instead of number
3 participants