Skip to content
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

Improve date handling #118

Merged
merged 13 commits into from
Jul 6, 2021
55 changes: 31 additions & 24 deletions src/dateUtils.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,51 @@
const { DateTime, Duration } = require('luxon');

const toDate = val => {
let t;
if (DateTime.isDateTime(val)){
eljhkrr marked this conversation as resolved.
Show resolved Hide resolved
return val;
} else if (typeof val === 'number'){
return DateTime.fromMillis(val);
} else if (typeof val === 'string'){
if (val.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/)){
return DateTime.fromISO(val);
} else if (val.match(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/)){
return DateTime.fromFormat(val, 'dd/MM/yyyy');
}
return DateTime.fromRFC2822(val);
t = val;
} else if (typeof val === 'object'){
eljhkrr marked this conversation as resolved.
Show resolved Hide resolved
if (typeof val.getMonth === 'function'){
return DateTime.fromJSDate(val);
if (val instanceof Date && typeof val.getTime() === 'number'){
t = DateTime.fromJSDate(val);
} else {
t = DateTime.fromObject(val);
}
return DateTime.fromObject(val);
}
if (typeof val === 'number'){
t = DateTime.fromMillis(val);
}
if (typeof val === 'string'){
const parsedDate = new Date(val);
if (!isNaN(parsedDate.getTime())){
t = DateTime.fromJSDate(parsedDate);
}
}
if (t instanceof DateTime && t.isValid){
eljhkrr marked this conversation as resolved.
Show resolved Hide resolved
return t.toUTC();
}
throw 'Unsupported date value';
};

const toDuration = val => {
let d;
if (Duration.isDuration(val)){
return val;
d = val;
} else if (typeof val === 'object'){
return Duration.fromObject(val);
} else if (typeof val === 'number'){
return Duration.fromObject({days: val});
} else {
return Duration.fromISO(val);
d = Duration.fromObject(val);
}
};

const addDate = (start, period) => {
return toDate(start).plus(toDuration(period));
if (typeof val === 'number'){
d = Duration.fromObject({days: val});
}
if (typeof val === 'string'){
d = Duration.fromISO(val);
}
if (d instanceof Duration && d.isValid){
return d;
}
throw 'Unsupported duration value';
};

module.exports = {
addDate,
toDate,
toDuration
};
4 changes: 2 additions & 2 deletions src/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const uuid = require('uuid/v4');
const devMode = require('./dev-mode');
const coreAdapter = require('./core-adapter');
const ChtCoreFactory = require('./cht-core-factory');
const { toDate, addDate } = require('./dateUtils');
const { toDate, toDuration } = require('./dateUtils');

const pathToHost = path.join(__dirname, 'form-host/form-host.html');
if (!fs.existsSync(pathToHost)) {
Expand Down Expand Up @@ -269,7 +269,7 @@ class Harness {
*/
async flush(amount) {
let now = this._now || Date.now();
now = addDate(now, amount);
now = toDate(now).plus(toDuration(amount));
return this.setNow(now);
}

Expand Down
5 changes: 2 additions & 3 deletions test/harness.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ describe('Harness tests', () => {
await harness.setNow('1985-08-06');
await harness.flush({ years: 1, days: 1, hours: 2 });
const now = await harness.getNow();
// TODO: This is timezone sensitive...
expect(new Date(now).toString()).to.include('Thu Aug 07 1986 02:00:00');
expect(new Date(now).toUTCString()).to.include('Thu, 07 Aug 1986 02:00:00');
});

it('flush shorthands as days', async () => {
await harness.setNow('1985-08-06');
await harness.flush(5);
const now = await harness.getNow();
expect(new Date(now).toString()).to.include('Sun Aug 11 1985 00:00:00');
expect(new Date(now).toUTCString()).to.include('Sun, 11 Aug 1985 00:00:00');
});

it('control now', async () => {
Expand Down