Comments (9)
Hi @shayonj!
Here's what happens:
-
When the input time is
2023-10-03 18:17:37 UTC
, andAmerica/New_York
is used as the timezone, a-240
(or 4 hours) offset is applied to the internal date, resulting in a time change to14:17:37
. -
For the cron expression
0 */1 * * *
, the closest hour match will be15
. After applying date formatting and converting the date to UTC (+4 hours or +240 minutes), it becomes19:00:00
. -
For
0 */2 * * *
, the closest hour match will be16
, and after date formatting and UTC conversion, it becomes20:00:00
. -
For
0 */3 * * *
, the closest hour match will be15
, and after date formatting and UTC conversion, it becomes19:00:00
. -
For
0 */4 * * *
, the closest hour match will be16
, and after date formatting and UTC conversion, it becomes20:00:00
.
In summary, date matching occurs against the localized date, not the original input date.
Let me know if I can provide any further help or missed something important.
from cron-parser.
oh i see! Is it possible to make it happen against the original input date and the TZ?
from cron-parser.
@shayonj one option is just to convert input date to UTC, and later, when you want to display it, just convert it back to the original TZ. But I'm not sure if it completely solves what you want to achieve.
from cron-parser.
hi @harrisiirak , so unfortunately that doesn't, if I understand you correctly because the end result I am expecting is for the result to relative to the timezone that I pass tz
. Is that basically a new feature request?
Because even if I don't convert it to UTC, the output (in the passed in tz
) still seems incorrect, but ofc that is working based on how you described above:
var parser = require("cron-parser");
var options = {
currentDate: new Date("2023-10-03 18:17:37 UTC"),
tz: "America/New_York"
};
var interval = parser.parseExpression("0 */1 * * *", options);
console.log("0 */1 * * * =>", interval.next().toDate());
// 0 */1 * * * => Tue Oct 03 2023 15:00:00 GMT-0400 (Eastern Daylight Time)
var interval0 = parser.parseExpression("0 */2 * * *", options);
console.log("0 */2 * * * =>", interval0.next().toDate());
// 0 */2 * * * => Tue Oct 03 2023 16:00:00 GMT-0400 (Eastern Daylight Time)
var interval1 = parser.parseExpression("0 */3 * * *", options);
console.log("0 */3 * * * =>", interval1.next().toDate());
// 0 */3 * * * => Tue Oct 03 2023 15:00:00 GMT-0400 (Eastern Daylight Time)
var interval2 = parser.parseExpression("0 */4 * * *", options);
console.log("0 */4 * * * =>", interval2.next().toDate());
// 0 */4 * * * => Tue Oct 03 2023 16:00:00 GMT-0400 (Eastern Daylight Time)
from cron-parser.
I was thinking if the input date is already in UTC, you can set tz = UTC
also, and later apply to America/New_York
tz conversion where needed.
Another option is that you already convert given currentDate
to correct timezone (to America/New_York
).
if I understand you correctly because the end result I am expecting is for the result to relative to the timezone that I pass tz.
But it does operate relative to the timezone defined in the options, as described in my first comment. And later when toUTCString
is being called on the date object, UTC value is returned.
I would have expected it to be
Tue, 03 Oct 2023 21:00:00 GMT
, instead ofTue, 03 Oct 2023 20:00:00 GMT
for0 */4 * * *
.
Even if you set tz = UTC
, this would never will be true. Only following hours will match */4
: 0, 4, 8, 12, 16, 20
. Of course, how it is formatted later based on tz, is a separate topic.
from cron-parser.
ah! i see. My bad in explaining, so the currentDate
is only used as an example here to show a reproducible case. When I am using this live, the currentDate
is nil and the issue still happens for the affect numerals. All I am doing is setting the tz
to America/New_York
.
from cron-parser.
I was thinking if the input date is already in UTC, you can set tz = UTC also, and later apply to America/New_York tz conversion where needed.
Even if I keep the timezones in UTC, I still see the same issue 🤔
var parser = require("cron-parser");
var options = {
currentDate: new Date("2023-10-03 18:17:37 UTC"),
utc: true
};
var interval = parser.parseExpression("0 */1 * * *", options);
console.log("0 */1 * * * =>", interval.next().toDate().toUTCString());
// 0 */1 * * * => Tue, 03 Oct 2023 19:00:00 GMT
var interval0 = parser.parseExpression("0 */2 * * *", options);
console.log("0 */2 * * * =>", interval0.next().toDate().toUTCString());
// 0 */2 * * * => Tue, 03 Oct 2023 20:00:00 GMT
var interval1 = parser.parseExpression("0 */3 * * *", options);
console.log("0 */3 * * * =>", interval1.next().toDate().toUTCString());
// 0 */3 * * * => Tue, 03 Oct 2023 21:00:00 GMT
var interval2 = parser.parseExpression("0 */4 * * *", options);
console.log("0 */4 * * * =>", interval2.next().toDate().toUTCString());
// 0 */4 * * * => Tue, 03 Oct 2023 20:00:00 GMT
I would have expected 0 */4 * * *
to be Tue, 03 Oct 2023 22:00:00 GMT
, right?
from cron-parser.
I would have expected 0 */4 * * * to be Tue, 03 Oct 2023 22:00:00 GMT , right?
Actually no. As I've explained in my previous comment:
Even if you set tz = UTC, this would never will be true. Only following hours will match */4: 0, 4, 8, 12, 16, 20. Of course, how it is formatted later based on tz, is a separate topic.
22:00:00 can't be never matched under these conditions.
from cron-parser.
ah! Ok i get it now, timezones oof. I wasn't thinking of 20:00 in UTC. Makes all the more sense, thanks so much for the examples @harrisiirak
from cron-parser.
Related Issues (20)
- Pull Request? HOT 4
- suggestion for iterator example
- Yes, I've also seen it quite frequently in the past. More fun (and actually practical) reasoning behind naming conventions: https://stackoverflow.com/a/41967120/143788 HOT 1
- cron expression does not match result HOT 2
- Add nthDayOfMonth examples
- Unexpected behavior with hours range HOT 1
- ”0 0 22 * * ? * “ parser cron expression error HOT 2
- Unexpected behavior when the step size plus the starting value is higher than the maximal expected value HOT 1
- :bug: the cron time is error HOT 1
- The crontab expression '0 0 0 12W 6' is causing a parsing error HOT 1
- [Docs]: API for Composing Cron Strings HOT 1
- Incomplete reset() when prev() was called before makes next() skip a day HOT 3
- First Monday of each month triggers every day HOT 1
- how to express 'every tow weeks on monday' HOT 1
- "0 0 1L */12 *" isn't parsed
- fieldsToExpression bug when using seconds HOT 2
- Browser
- Possible bug or a misunderstanding HOT 2
- Repeatable day of month not working when providing day of week
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cron-parser.