breejs / later

*Maintained fork of Later.* A javascript library for defining recurring schedules and calculating future (or past) occurrences for them. Includes support for using English phrases and Cron schedules. Works in Node and in the browser.
https://breejs.github.io/later/
MIT License
134 stars 13 forks source link

Can't get UTC output to work #16

Closed mountaindude closed 2 years ago

mountaindude commented 2 years ago

Having read the docs, my understanding is that I can call later.date.UTC() to make the occurrences I create to be in UTC. UTC should even be the default.

But no matter what I do I just get local time zone... The code snippet below prints current time as UTC to the left and the occurrences from later to the right. I am basically trying to get later output something without a timezone offset applied.

const later = require('@breejs/later');

const sched = later.parse.text('every 5 minutes');
later.date.UTC();

const s = later.schedule(sched);
const occurrences = s.next(5);

const utcDate = new Date(Date.now());

console.log('-------------------------------------------------------------------------');
for (let i = 0; i < 5; i++) {
    console.log(`${utcDate.toUTCString()} /// ${i + 1} /// ${occurrences[i]}`);
}

results in this:

➜ node index.js
-------------------------------------------------------------------------
Thu, 21 Oct 2021 18:56:00 GMT /// 1 /// Thu Oct 21 2021 21:00:00 GMT+0200 (Central European Summer Time)
Thu, 21 Oct 2021 18:56:00 GMT /// 2 /// Thu Oct 21 2021 21:05:00 GMT+0200 (Central European Summer Time)
Thu, 21 Oct 2021 18:56:00 GMT /// 3 /// Thu Oct 21 2021 21:10:00 GMT+0200 (Central European Summer Time)
Thu, 21 Oct 2021 18:56:00 GMT /// 4 /// Thu Oct 21 2021 21:15:00 GMT+0200 (Central European Summer Time)
Thu, 21 Oct 2021 18:56:00 GMT /// 5 /// Thu Oct 21 2021 21:20:00 GMT+0200 (Central European Summer Time)
➜
mountaindude commented 2 years ago

Meh - that was literally less than a minute from posting to realising that this works:

for (let i = 0; i < 5; i++) {
    console.log(`${utcDate.toUTCString()} /// ${i + 1} /// ${occurrences[i].toUTCString()}`);
}

Still, from reading the docs I still expect that later.date.UTC() would give me UTC straight from later...

Oh well - all good I guess :)

shadowgate15 commented 2 years ago

you need to call later.date.UTC() before any other later calls.

mountaindude commented 2 years ago

@shadowgate15

Ok, but this code gives the same result as above, and I can't really move later.date.UTC() any higher up..

const later = require('@breejs/later');
later.date.UTC();

const sched = later.parse.text('every 5 minutes');

const s = later.schedule(sched);
const occurrences = s.next(5);

const utcDate = new Date(Date.now());

console.log('-------------------------------------------------------------------------');
for (let i = 0; i < 5; i++) {
    console.log(`${utcDate.toUTCString()} /// ${i + 1} /// ${occurrences[i]}`);
}
shadowgate15 commented 2 years ago

I figured it out, two things later will will return a Date object which mean that when it is stringified by default it will be displayed with local time offset.

and later.date.UTC makes later parse all dates as UTC. I.e.

later.date.UTC();
// system currently set for CST
const sched = later.parse.text('every Monday at midnight');

const s = later.schedule(sched);
const next  =  s.next();
// `next` when stringified will be offset by 5 hours from midnight
// but `next` will actually be midnight UTC
mountaindude commented 2 years ago

Ahhh... nice. Gottit.

Many thanks!

On Thu, 21 Oct 2021 at 21:39, shadowgate15 @.***> wrote:

I figured it out, two things later will will return a Date object which mean that when it is stringified by default it will be displayed with local time offset.

and later.date.UTC makes later parse all dates as UTC. I.e.

later.date.UTC();// system currently set for CSTconst sched = later.parse.text('every Monday at midnight'); const s = later.schedule(sched);const next = s.next();// next when stringified will be offset by 5 hours from midnight// but next will actually be midnight UTC

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/breejs/later/issues/16#issuecomment-948944788, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH3JDRBM7P5NN6BFBSY4JTUIBT65ANCNFSM5GO3AAUQ .