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

Commit

Permalink
Merge pull request #148 from enketo/fix/dates-138-II
Browse files Browse the repository at this point in the history
Fix/dates 138 ii
  • Loading branch information
eyelidlessness authored Jan 31, 2022
2 parents 78d4955 + 57bdb8a commit a49bc5d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
16 changes: 10 additions & 6 deletions src/openrosa-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,25 +528,29 @@ function asInteger(r) {

function asDate(r) {
let temp;
let timeComponent;
switch(r.t) {
case 'bool': return new Date(NaN);
case 'date': return r.v;
case 'num': temp = new Date(1970, 0, 1); temp.setDate(temp.getDate() + r.v); return temp;
case 'num': temp = new Date(0); temp.setTime(temp.getTime() + r.v * 24 * 60 * 60 * 1000); return temp;
case 'arr':
case 'str':
r = asString(r);
if(RAW_NUMBER.test(r)) {
// Create a date at 00:00:00 1st Jan 1970 _in the current timezone_
temp = new Date(1970, 0, 1);
temp.setDate(1 + parseInt(r, 10));
temp = new Date(0);
temp.setTime(temp.getTime() + parseInt(r, 10) * 24 * 60 * 60 * 1000);
return temp;
} else if(DATE_STRING.test(r)) {
temp = r.indexOf('T');
if(temp !== -1) r = r.substring(0, temp);
if(temp !== -1) {
timeComponent = r.substring(temp);
r = r.substring(0, temp);
}
temp = r.split('-');
if(isValidDate(temp[0], temp[1], temp[2])) {
timeComponent = timeComponent ? timeComponent : 'T00:00:00.000' + getTimezoneOffsetAsTime(new Date(r));
const time = `${_zeroPad(temp[0])}-${_zeroPad(temp[1])}-${_zeroPad(temp[2])}`+
'T00:00:00.000' + getTimezoneOffsetAsTime(new Date(r));
timeComponent;
return new Date(time);
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/integration/complex.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const _ = require('lodash');
const {initDoc, assert} = require('./helpers');

const SIMPLE_DATE_MATCH = /^\d{4}-[0-1]\d-[0-3]\d$/;
const SIMPLE_DATE_OR_DATE_TIME_MATCH = /^\d{4}-[0-1]\d-[0-3]\d(T[0-2]\d:[0-5]\d:[0-5]\d\.\d\d\d(Z|[+-][0-1]\d(:[0-5]\d)?))?$/;

describe('some complex examples', () => {
const doc = initDoc('');
Expand All @@ -19,7 +20,7 @@ describe('some complex examples', () => {
"if(selected('approx' ,'date'), 'first' ,'second')": /^second$/,
"if(selected(/model/instance[1]/pregnancy/group_lmp/lmp_method, 'date'), /model/instance[1]/pregnancy/group_lmp/lmp_date, 'testing')": /testing/,
"if(selected(/model/instance[1]/pregnancy/group_lmp/lmp_method, 'date'), /model/instance[1]/pregnancy/group_lmp/lmp_date, concat('testing', '1', '2', '3', '...'))": /testing/,
"if(selected(/model/instance[1]/pregnancy/group_lmp/lmp_method, 'date'), /model/instance[1]/pregnancy/group_lmp/lmp_date, date-time(0))": SIMPLE_DATE_MATCH,
"if(selected(/model/instance[1]/pregnancy/group_lmp/lmp_method, 'date'), /model/instance[1]/pregnancy/group_lmp/lmp_date, date-time(0))": SIMPLE_DATE_OR_DATE_TIME_MATCH,
"if(selected(/model/instance[1]/pregnancy/group_lmp/lmp_method, 'date'), /model/instance[1]/pregnancy/group_lmp/lmp_date, date-time(decimal-date-time(today() - 60)))": SIMPLE_DATE_MATCH,
"if(selected(/model/instance[1]/pregnancy/group_lmp/lmp_method ,'date'), /model/instance[1]/pregnancy/group_lmp/lmp_date ,date-time(decimal-date-time(today()- 60 )))": SIMPLE_DATE_MATCH,
'if(true(), today(), today())': SIMPLE_DATE_MATCH,
Expand Down
8 changes: 4 additions & 4 deletions test/integration/openrosa-xpath/date-time.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ describe('#date-time()', () => {
});

describe('valid date-time string', () => {
it('should be converted to date string', () => {
assertStringValue("date-time('1970-01-01T21:50:49Z')", '1970-01-01');
it('should be converted to date-time string in the local time zone', () => {
assertStringValue("date-time('1970-01-01T21:50:49Z')", '1970-01-01T14:50:49.000-07:00');
});
});

describe('positive number', () => {
it('should be converted', () => {
assertStringValue('date-time(0)', '1970-01-01');
assertStringValue('date-time(1)', '1970-01-02');
assertStringValue('date-time(0)', '1969-12-31T17:00:00.000-07:00');
assertStringValue('date-time(1)', '1970-01-01T17:00:00.000-07:00');
});
});

Expand Down
40 changes: 26 additions & 14 deletions test/integration/openrosa-xpath/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ describe('#date()', () => {
assertNumberRounded('"2018-01-01" + 1', 17533.29167, 100000);
});

it('example 2', () => {
assertNumberRounded('date("1970-01-01T00:00:00.000+00:00")', 0, 100000);
});

it('example 3', () => {
assertNumberRounded('date(0)', 0, 100000);
});

describe('with explicit number() call', () => {
it('example 1', () => {
assertNumberRounded('number("2018-01-01" + 1)', 17533.29167, 100000);
Expand All @@ -61,6 +69,10 @@ describe('#date()', () => {
it('example 5', () => {
assertStringValue('"2021-11-30" - "2021-11-29"', '1');
});
it('example 6', () => {
assertStringValue('date(decimal-date-time("2003-10-20T08:00:00.000-07:00"))', '2003-10-20T08:00:00.000-07:00');
});


[
'today()',
Expand Down Expand Up @@ -104,10 +116,10 @@ describe('#date()', () => {

describe('number', () => {
[
['date(0)', '1970-01-01'],
['date(1)', '1970-01-02'],
['date(1.5)', '1970-01-02'],
['date(-1)', '1969-12-31'],
['date(0)', '1969-12-31T17:00:00.000-07:00'],
['date(1)', '1970-01-01T17:00:00.000-07:00'],
['date(1.5)', '1970-01-02T05:00:00.000-07:00'],
['date(-1)', '1969-12-30T17:00:00.000-07:00'],
].forEach(([expr, expected]) => {
it(expr + ' should be converted to ' + expected, () => {
assertString(expr, expected);
Expand All @@ -131,16 +143,16 @@ describe('#date()', () => {
['date("2004-05-01") != date("2004-05-01")', false],
['"string" != date("1999-09-09")', true],
['"string" = date("1999-09-09")', false],
['date(0) = date("1970-01-01")', true],
['date(0) != date("1970-01-01")', false],
['date(1) = date("1970-01-02")', true],
['date(1) != date("1970-01-02")', false],
['date(-1) = date("1969-12-31")', true],
['date(-1) != date("1969-12-31")', false],
['date(14127) = date("2008-09-05")', true],
['date(14127) != date("2008-09-05")', false],
['date(-10252) = date("1941-12-07")', true],
['date(-10252) != date("1941-12-07")', false],
['date(0) = date("1970-01-01T00:00:00.000Z")', true],
['date(0) != date("1970-01-01T00:00:00.000Z")', false],
['date(1) = date("1970-01-02T00:00:00.000Z")', true],
['date(1) != date("1970-01-02T00:00:00.000Z")', false],
['date(-1) = date("1969-12-31T00:00:00.000Z")', true],
['date(-1) != date("1969-12-31T00:00:00.000Z")', false],
['date(14127) = date("2008-09-05T00:00:00.000Z")', true],
['date(14127) != date("2008-09-05T00:00:00.000Z")', false],
['date(-10252) = date("1941-12-07T00:00:00.000Z")', true],
['date(-10252) != date("1941-12-07T00:00:00.000Z")', false],
['date("2012-01-01") < today()', true],
['date("2012-01-01") > today()', false],
['date("2100-01-02") > today()', true],
Expand Down
8 changes: 4 additions & 4 deletions test/unit/openrosa-extensions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ describe('openrosa-extensions', () => {
});
});

it('should convert zero to 1 Jan 1970 in local timezone', () => {
it('should convert zero to 1 Jan 1970 UTC', () => {
// when
const res = date(wrapVal(0));

// then
assert.equal(res.v.toISOString(), '1970-01-01T07:00:00.000Z');
assert.equal(res.v.toISOString(), '1970-01-01T00:00:00.000Z');
});
});

Expand All @@ -111,12 +111,12 @@ describe('openrosa-extensions', () => {
});
});

it('should convert zero to 1 Jan 1970 in local timezone', () => {
it('should convert zero to 1 Jan 1970 UTC', () => {
// when
const res = dateTime(wrapVal(0));

// then
assert.equal(res.v.toISOString(), '1970-01-01T07:00:00.000Z');
assert.equal(res.v.toISOString(), '1970-01-01T00:00:00.000Z');
});
});

Expand Down

0 comments on commit a49bc5d

Please sign in to comment.