Skip to content

Relative Time now,weeks,months,years #4304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions spec/MongoTransform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 1 month 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-11-21T23:52:46.617Z');
expect(status).toBe('success');
expect(info).toBe('future');
});
Expand All @@ -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({
Expand Down
25 changes: 25 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 2 months 3 weeks ago' });
return q.find({ useMasterKey: true });
})
.then((results) => {
expect(results.length).toBe(2);
})
.then(done, done.fail);
});

Expand Down
35 changes: 31 additions & 4 deletions src/Adapters/Storage/Mongo/MongoTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'" };
}

Expand All @@ -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.',
Expand All @@ -585,6 +585,28 @@ 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 'mo':
case 'mos':
case 'month':
case 'months':
seconds += val * 2592000; // 30 * 24 * 60 * 60, approx. 30 days/month
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
Expand Down Expand Up @@ -626,13 +648,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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

return {
status: 'success',
info: 'present',
result: new Date(now.valueOf())
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

if(callback) {
callback(false);
}
Expand Down