I have a function that returns a Date object modified by a delta like this:
export function getDate(delta: string = "", start?: Date): Date {
const date = start ? new Date(start.getTime()) : new Date();
const rel = delta.split(" ").join("");
const [, sign, years, months, days, hours, mins, secs] = toArray(/([+-])?(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?/.exec(rel) as ArrayLike<string>);
const plus = sign !== "-";
if (years) date.setFullYear(date.getFullYear() + (plus ? +years : -years));
if (months) date.setMonth(date.getMonth() + (plus ? +months : -months));
if (days) date.setDate(date.getDate() + (plus ? +days : -days));
if (hours) date.setHours(date.getHours() + (plus ? +hours: -hours));
if (mins) date.setMinutes(date.getMinutes() + (plus ? +mins: -mins));
if (secs) date.setSeconds(date.getSeconds() + (plus ? +secs : -secs));
return date;
}
It seem to work just fine in node and chrome and my tests pass locally. But when I push to npm the tests fail in Travis CI, like this: Travis CI
What I am sadly blind to is why the first test pass and the second fail with exactly 1 hour. Is there some CET / UCT magic i am missing? Am I missing out on some special thing with how the Date object works in various node versions?
You can see the test code in travis (link above) but I will add it here as well:
const now = new Date();
let pos = Util.getDate("+1Y2M3d4h5m6s", now);
expect(now.getTime() - pos.getTime()).toBe(-37166706000); // passes locally and in travis
let neg = Util.getDate("-1Y2M3d4h5m6s", now);
expect(now.getTime() - neg.getTime()).toBe(36903906000); // passes locally but fails in travis
Thankful for some wisdom.