npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aquarela/time-to-cron

v1.0.1

Published

Convert time to cron expression

Downloads

67

Readme

@aquarela/time-to-cron

@aquarela/time-to-cron is a utility library designed to convert time intervals into cron expressions. This package is particularly useful for developers looking to schedule tasks in environments that require cron syntax, providing a simple interface to generate accurate cron schedules.

Table of Contents

Installation

Install the package using npm:

npm install @aquarela/time-to-cron

Usage

Use the timeToCron function to convert time values into cron expressions. Below are some examples of how to utilize this function effectively.

Importing the Function

First, import the timeToCron function:

import { timeToCron } from "@aquarela/time-to-cron";

Examples

Convert Seconds

Convert a number of seconds to a cron expression. The value can be less than 60 or a multiple of 60.

const cronExpression = timeToCron(30);
console.log(cronExpression); // "*/30 * * * * *"

Convert Minutes

Convert a number of minutes to a cron expression. The value can be less than 60 or a multiple of 60.

const cronExpression = timeToCron(60, "minutes");
console.log(cronExpression); // "0 0 */1 * * *"

Convert Hours

Convert a number of hours to a cron expression. The value must be less than 24 or a multiple of 24.

const cronExpression = timeToCron(2, "hours");
console.log(cronExpression); // "0 0 */2 * * *"

Convert Days

Convert a number of days to a cron expression. The value is the number of days between executions.

const cronExpression = timeToCron(2, "days");
console.log(cronExpression); // "0 0 0 */2 * *"

Disable Repeat on Same Day

Disable repeating the cron job on the same day when the interval is equal to or greater than one day. This is useful for scenarios where you want the task to run at a specific interval starting at midnight.

const cronExpression = timeToCron(60, "minutes", false);
console.log(cronExpression); // "0 0 0/1 * * *"

API

timeToCron(value: number, unit: TimeUnit = "seconds", repeatSameDay: boolean = true): string

Converts a time value to a cron expression.

  • value: The time value to convert. Must be a positive integer.
  • unit: The unit of the time value. Can be "seconds", "minutes", "hours", or "days". Default is "seconds".
  • repeatSameDay: (Optional) Whether to repeat the cron job on the same day when the interval is greater than or equal to one day. Default is true.

Returns the corresponding cron expression as a string.

Throws an error if the value is not a positive integer, does not meet the unit constraints (such as being a multiple of 60 for minutes), or if an invalid time unit is provided.

Validation Rules

  • Seconds: Must be less than 60 or a multiple of 60.
  • Minutes: Must be less than 60 or a multiple of 60.
  • Hours: Must be less than 24 or a multiple of 24.
  • Days: Any positive integer value is valid.

Testing

To run the tests, execute the following command:

npm test

Repository

For more information and to contribute, visit the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Author

Diego Peixoto - @diegopeixoto on GitHub