.gitea/workflows | ||
.vscode | ||
test | ||
ts | ||
.gitignore | ||
changelog.md | ||
license | ||
npmextra.json | ||
package.json | ||
pnpm-lock.yaml | ||
readme.hints.md | ||
readme.md | ||
tsconfig.json |
@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 hello@task.vc.
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.