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

@push.rocks/smarttime

v4.0.8

Published

Provides utilities for advanced time handling including cron jobs, timestamps, intervals, and more.

Downloads

258

Readme

@push.rocks/smarttime

handle time in smart ways

Install

To install @push.rocks/smarttime, use the following npm command:

npm install @push.rocks/smarttime --save

This will add @push.rocks/smarttime to your project's dependencies.

Usage

@push.rocks/smarttime provides a comprehensive toolkit for handling various aspects of time manipulation, scheduling, and comparison in a TypeScript project. The following sections will guide you through the capabilities of this package, showcasing how to use its classes and functions effectively.

Handling Time Units and Calculations

Working with Units

import { units, getMilliSecondsFromUnits } from '@push.rocks/smarttime';

// Define a duration using a combination of time units
let durationInMilliseconds = getMilliSecondsFromUnits({
  years: 1,
  months: 2,
  weeks: 3,
  days: 4,
  hours: 5,
  minutes: 6,
  seconds: 7
});

console.log(`Duration in milliseconds: ${durationInMilliseconds}`);

In the example above, we specify a complex duration made up of various time units using the getMilliSecondsFromUnits function. This is quite useful for calculations where you need to define durations in a more human-readable format.

Scheduling with CronManager

@push.rocks/smarttime includes a powerful scheduling tool called CronManager, which allows you to schedule tasks using cron syntax.

Creating and Starting a CronManager

import { CronManager } from '@push.rocks/smarttime';

const cronManager = new CronManager();

// Adding a cron job that runs every minute
cronManager.addCronjob('* * * * *', async () => {
  console.log('This task runs every minute.');
});

// Starting the CronManager
cronManager.start();

The example demonstrates how to create a CronManager, add a cron job that runs every minute, and start the scheduling. You can add multiple cron jobs to a single manager, each with its own scheduling and task.

Working with Extended Date Class

The ExtendedDate class extends the native JavaScript Date object, providing additional functionality for handling dates in various formats and zones.

Creating ExtendedDate Instances

import { ExtendedDate } from '@push.rocks/smarttime';

// Creating a date from a European format string
const dateFromEuroString = ExtendedDate.fromEuropeanDate('31.12.2023');
console.log(dateFromEuroString.toString());

// Creating a date from a hyphed date string
const dateFromHyphedString = ExtendedDate.fromHyphedDate('2023-12-31');
console.log(dateFromHyphedString.toString());

Checking if a Date is Today

import { ExtendedDate } from '@push.rocks/smarttime';

const someDate = new ExtendedDate();
console.log(`Is someDate today? ${someDate.isToday()}`);

Using ExtendedDate, you can also easily check if a given date is today. This simplifies certain date comparisons that are common in web and application development.

High-Resolution Time Measurement

For performance testing and high-resolution time tracking, @push.rocks/smarttime provides the HrtMeasurement class.

import { HrtMeasurement } from '@push.rocks/smarttime';

const hrtMeasurement = new HrtMeasurement();
hrtMeasurement.start();
// Simulate some operations...
setTimeout(() => {
  hrtMeasurement.stop();
  console.log(`Operation took ${hrtMeasurement.milliSeconds} milliseconds.`);
}, 1000);

This class allows you to measure the duration of operations in your code with high precision, offering both milliseconds and nanoseconds resolutions.

Interval and Timer functionalities

@push.rocks/smarttime includes classes for managing intervals and timers with enhanced control, such as pause, resume, and reset capabilities.

Using the Timer class

import { Timer } from '@push.rocks/smarttime';

const timer = new Timer(5000); // A 5-second timer
timer.start();

timer.completed.then(() => {
  console.log('Timer completed!');
});

// Resetting the timer
timer.reset();
timer.start();

The Timer class allows for asynchronous waiting in a more object-oriented manner. In the example, a Timer is created for five seconds, started, and then reset for demonstration purposes.

Conclusion

@push.rocks/smarttime offers an extensive toolkit for dealing with time in JavaScript and TypeScript applications. Whether you need precise timing, scheduled tasks, or extended date functionalities, this package provides a suite of tools designed to handle time in smart and efficient ways. The examples provided herein demonstrate the core functionalities, aiming to help you integrate time-related features into your projects with ease.

License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at [email protected].

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.