Skip to content

jaenyf/time-provider

Repository files navigation

NPM CI codecov

A tiny library to rapidly have time !

Description

It's a very simple typescript library to setup a source of time. A time provider works with a compatible adapter (even for native Date or Temporal), so you must both import the core library and the plugin of your choice (See usage).

Core library

Name NPM package
@time-provider/core NPM

Plugins

Currently supported plugins are :

Plugin Name Returned Type NPM package
Temporal @time-provider/plugin-temporal Instant NPM
Native @time-provider/plugin-native Date NPM
Luxon @time-provider/plugin-luxon DateTime NPM
Day.js @time-provider/plugin-dayjs Dayjs NPM
Moment.js @time-provider/plugin-moment Moment NPM

Usage

  • Each plugin (adapter) exports a TimeAdapter and a FixedTimeAdapter class
  • Select your desired plugin (native/dayjs/moment/luxon/temporal)
  • call createTimeProvider.for(/*your adapter here*/)

For your production code

import { createTimeProvider } from "@time-provider/core";
//Import the plugin of your choice (here the temporal plugin)
import { TimeAdapter } from "@time-provider/plugin-temporal";
import { Temporal } from "@js-temporal/polyfill";
const timeProvider = createTimeProvider.for(new TimeAdapter());

Or for your tests (fixed time)

import { createTimeProvider } from "@time-provider/core";
//Import the plugin of your choice (here the temporal plugin)
import { FixedTimeAdapter } from "@time-provider/plugin-temporal";
import { Temporal } from "@js-temporal/polyfill";
const fixedTimeProvider = createTimeProvider.for(
  new FixedTimeAdapter(Temporal.Instant.from("2026-01-01T00:00Z")),
);

API

interface ITimeProvider<TDate> {
  localNow(): TDate;
  utcNow(): TDate;
  parse(input: string | number | TDate): TDate;
}

About

A library to rapidly have time

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors