question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add method for comparing date against cron pattern

See original GitHub issue

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:

function timeMatches(expression, date) {
  var interval = parser.parseExpression(expression);
  var data = interval._fields;
  
  if (!data.second.includes(date.getSeconds())) {
    return false;
  }
  if (!data.minute.includes(date.getMinutes())) {
    return false;
  }
  if (!data.hour.includes(date.getHours())) {
    return false;
  }
  if (!data.dayOfMonth.includes(date.getDate())) {
    return false;
  }
  if (!data.month.includes(date.getMonth() + 1)) {
    return false;
  }
  if (!data.dayOfWeek.includes(date.getDay())) {
    return false;
  }
  return true;
}

It allows me to do to:

if (timeMatches('* 20 * * * *', new Date()) {
  // This is true when minute is 20.
}

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).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
Eric24commented, Feb 23, 2020

Here’s what I ended up doing. Since you already have a dependency on moment-timezone, it’s a pretty lightweight function:

function match(expression, date, scope = 'second') {
  scope = ['second', 'minute', 'hour', 'day', 'month', 'weekday'].indexOf(scope.toLowerCase());
  try {
    let data = cron.parseExpression(expression)._fields;

    if (scope <= 0 && !data.second.includes(date.second())) return false;
    if (scope <= 1 && !data.minute.includes(date.minute())) return false;
    if (scope <= 2 && !data.hour.includes(date.hour())) return false;
    if (scope <= 3 && !data.dayOfMonth.includes(date.date())) return false;
    if (scope <= 4 && !data.month.includes(date.month() + 1)) return false;
    if (scope <= 5 && !data.dayOfWeek.includes(date.day())) return false;

    return true;
  } catch (e) {
    return false;
  }
}
2reactions
andeersgcommented, Mar 29, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

compare Cron Expression with current time - Stack Overflow
I want to check whether cron expression time refer to the time in the future, Otherwise trigger won't be executed at all. Is...
Read more >
Construct cron expressions for a filter subscription
This page describes how to construct a cron expression. Cron expressions can be used when creating a subscription to a filter, as described...
Read more >
Cron job every month - Crontab.guru
The quick and simple editor for cron schedule expressions by Cronitor. “At 00:00 on day-of-month 1.” next at 2023-01-01 00:00:00.
Read more >
croniter · PyPI
This is much like the builtin range(start,stop,step) function, but for dates. The step argument is a cron expression. Added in (>=0.3.34).
Read more >
Date functions | BigQuery - Google Cloud
WEEK(<WEEKDAY>) : Returns the week number of the date in the range [0, 53]. Weeks begin on WEEKDAY . Dates prior to the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found