-
Notifications
You must be signed in to change notification settings - Fork 156
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
Add method for comparing date against cron pattern #153
Comments
@andeersg there was some (similar) discussion regards implementing validation interface for such cases in #145. The idea itself is great, but I'm not completely sure how reasonable it's to add this into core (maybe separate cron-data-validation library or something like that). I must digest this a little bit. |
Yes I'm not the one to say if this is a feature that belongs here or not. But of all the libraries I found this was the one I thought was the most fitting one, since all the others focus on running functions at cron not parsing the cron string. But for now I can use my function, so take your time to consider it, and let me know if you need input or something. |
A couple of thoughts:
PS - For 2, an alternative might be to pass in a date and a "resolution" prop (a string value of second|minute|hour|day|month|weekday). The concept is the same, it's just a different way to specifying the "test" date and the level of granularity. |
Here's what I ended up doing. Since you already have a dependency on
|
This is what I do export function isTimeMatches(cronExpression: string, date: Date): boolean {
const currentDate = date.valueOf()
const parsed = cronParser.parseExpression(cronExpression, {
// this is bug of cron-parser, if date is exact match current cronExpression,
// current date will be missing from parsed, neither prev() nor next() would have current date
// thus we need to push currentDate back a little
currentDate: new Date(date).setSeconds(-1),
tz: 'UTC',
})
return parsed.next().toDate().valueOf() === currentDate
} |
I was trying to find a library that allowed my to check the current time against a cron pattern to see if it matches. So far this library is the one closest to what I want, but I had some problems with milliseconds and rounding errors when I tried to convert it to seconds.
The closest I got was to create this function:
It allows me to do to:
Could it be possible to add a method like this to the library? (Not saying my checks are the best, but something to validate current date against pattern).
The text was updated successfully, but these errors were encountered: