diff --git a/spec/MongoTransform.spec.js b/spec/MongoTransform.spec.js index 73c179d79f..0c24f715b1 100644 --- a/spec/MongoTransform.spec.js +++ b/spec/MongoTransform.spec.js @@ -387,9 +387,9 @@ describe('relativeTimeToDate', () => { describe('In the future', () => { it('should parse valid natural time', () => { - const text = 'in 12 days 10 hours 24 minutes 30 seconds'; + const text = 'in 1 year 2 weeks 12 days 10 hours 24 minutes 30 seconds'; const { result, status, info } = transform.relativeTimeToDate(text, now); - expect(result.toISOString()).toBe('2017-10-08T23:52:46.617Z'); + expect(result.toISOString()).toBe('2018-10-22T23:52:46.617Z'); expect(status).toBe('success'); expect(info).toBe('future'); }); @@ -405,6 +405,16 @@ describe('relativeTimeToDate', () => { }); }); + describe('From now', () => { + it('should equal current time', () => { + const text = 'now'; + const { result, status, info } = transform.relativeTimeToDate(text, now); + expect(result.toISOString()).toBe('2017-09-26T13:28:16.617Z'); + expect(status).toBe('success'); + expect(info).toBe('present'); + }); + }); + describe('Error cases', () => { it('should error if string is completely gibberish', () => { expect(transform.relativeTimeToDate('gibberishasdnklasdnjklasndkl123j123')).toEqual({ diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index 15884a1cd1..20bc916599 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -3152,6 +3152,31 @@ describe('Parse.Query testing', () => { .then((results) => { expect(results.length).toBe(2); }) + .then(() => { + const q = new Parse.Query('MyCustomObject'); + q.greaterThan('ttl', { $relativeTime: 'now' }); + return q.find({ useMasterKey: true }); + }) + .then((results) => { + expect(results.length).toBe(1); + }) + .then(() => { + const q = new Parse.Query('MyCustomObject'); + q.greaterThan('ttl', { $relativeTime: 'now' }); + q.lessThan('ttl', { $relativeTime: 'in 1 day' }); + return q.find({ useMasterKey: true }); + }) + .then((results) => { + expect(results.length).toBe(0); + }) + .then(() => { + const q = new Parse.Query('MyCustomObject'); + q.greaterThan('ttl', { $relativeTime: '1 year 3 weeks ago' }); + return q.find({ useMasterKey: true }); + }) + .then((results) => { + expect(results.length).toBe(2); + }) .then(done, done.fail); }); diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index 9c5c525621..01c1aa1a8a 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -544,7 +544,7 @@ function relativeTimeToDate(text, now = new Date()) { const future = parts[0] === 'in'; const past = parts[parts.length - 1] === 'ago'; - if (!future && !past) { + if (!future && !past && text !== 'now') { return { status: 'error', info: "Time should either start with 'in' or end with 'ago'" }; } @@ -562,7 +562,7 @@ function relativeTimeToDate(text, now = new Date()) { parts = parts.slice(0, parts.length - 1); } - if (parts.length % 2 !== 0) { + if (parts.length % 2 !== 0 && text !== 'now') { return { status: 'error', info: 'Invalid time string. Dangling unit or number.', @@ -585,6 +585,21 @@ function relativeTimeToDate(text, now = new Date()) { } switch(interval) { + case 'yr': + case 'yrs': + case 'year': + case 'years': + seconds += val * 31536000; // 365 * 24 * 60 * 60 + break; + + case 'wk': + case 'wks': + case 'week': + case 'weeks': + seconds += val * 604800; // 7 * 24 * 60 * 60 + break; + + case 'd': case 'day': case 'days': seconds += val * 86400; // 24 * 60 * 60 @@ -626,13 +641,18 @@ function relativeTimeToDate(text, now = new Date()) { info: 'future', result: new Date(now.valueOf() + milliseconds) }; - } - if (past) { + } else if (past) { return { status: 'success', info: 'past', result: new Date(now.valueOf() - milliseconds) }; + } else { + return { + status: 'success', + info: 'present', + result: new Date(now.valueOf()) + } } } diff --git a/src/ParseServer.js b/src/ParseServer.js index ee2c837501..3b96080eac 100644 --- a/src/ParseServer.js +++ b/src/ParseServer.js @@ -256,6 +256,7 @@ class ParseServer { /* eslint-disable no-console */ console.warn(`\nWARNING, Unable to connect to '${Parse.serverURL}'.` + ` Cloud code and push notifications may be unavailable!\n`); + /* eslint-enable no-console */ if(callback) { callback(false); }