Skip to main content

TimeSpan

The @daiso-tech/core/time-span component provides an easy way for defining, manipulating, and comparing durations. Furthermore, it is designed for easy integration with external time libraries like Luxon and Dayjs.

TimeSpan class

The TimeSpan class is used for representing time interval.

info

Note TimeSpan cannot be negative.

Creating a TimeSpan

Creating TimeSpan from milliseconds:

import { TimeSpan } from "@daiso-tech/core/time-span";

const timeSpan = TimeSpan.fromMilliseconds(100);

Creating TimeSpan from seconds:

const timeSpan = TimeSpan.fromSeconds(30);

Creating TimeSpan from minutes:

const timeSpan = TimeSpan.fromMinutes(15);

Creating TimeSpan from hours:

const timeSpan = TimeSpan.fromHours(1);

Creating TimeSpan from days:

const timeSpan = TimeSpan.fromDays(1);

Creating TimeSpan from date range:

const timeSpan = TimeSpan.fromDateRange({
start: new Date("2000-01-01"),
end: new Date("2010-01-01"),
});

Adding time to TimeSpan

You can add milliseconds to a TimeSpan:

timeSpan.addMilliseconds(200);

You can add seconds to a TimeSpan:`

timeSpan.addSeconds(30);

You can add minutes to a TimeSpan:

timeSpan.addMinutes(20);

You can add hours to a TimeSpan:

timeSpan.addHours(2);

You can add days to a TimeSpan:

timeSpan.addDays(14);

You can add 2 TimeSpan together:

timeSpan.addTimeSpan(TimeSpan.fromDays(14).addHours(20));

Subtracting time from TimeSpan

You can subtract milliseconds from a TimeSpan:

timeSpan.subtractMilliseconds(200);

You can subtract seconds from a TimeSpan:`

timeSpan.subtractSeconds(30);

You can subtract minutes from a TimeSpan:

timeSpan.subtractMinutes(20);

You can subtract hours from a TimeSpan:

timeSpan.subtractHours(2);

You can subtract days from a TimeSpan:

timeSpan.subtractDays(14);

You can subtract 2 TimeSpan together:

timeSpan.subtractTimeSpan(TimeSpan.fromDays(14).addHours(20));

Multiplying and dividing a TimeSpan

Dividing a timespan:

// Will be now 100 miliseconds
TimeSpan.fromMilliseconds(200).divide(2);

Multiplying a timespan:

// Will be now 400 miliseconds
TimeSpan.fromMilliseconds(200).multiply(2);

Comparing TimeSpan:s

Equals:

// Returns false
TimeSpan.fromSeconds(1).equal(TimeSpan.fromSeconds(2));

Greater than:

// Returns false
TimeSpan.fromSeconds(1).gt(TimeSpan.fromSeconds(2));

Greater than or equals:

// Returns false
TimeSpan.fromSeconds(1).gte(TimeSpan.fromSeconds(2));

Less than:

// Returns true
TimeSpan.fromSeconds(1).lt(TimeSpan.fromSeconds(2));

Less than or equals:

// Returns true
TimeSpan.fromSeconds(1).lte(TimeSpan.fromSeconds(2));

Converting a TimeSpan

You can get amount of milliseconds contained in the TimeSpan:

TimeSpan.fromSeconds(1).toMilliseconds();

You can get amount of seconds contained in the TimeSpan:

TimeSpan.fromMinutes(1).toSeconds();

You can get amount of minutes contained in the TimeSpan:

TimeSpan.fromHour(1).toMinutes();

You can get amount of hours contained in the TimeSpan:

TimeSpan.fromDays(1).toHours();

You can get amount of days contained in the TimeSpan:

TimeSpan.fromHour(48).toDays();

You can get end date relative to a start date:

// Will return date of "2002-01-01"
TimeSpan.fromDays(365).toEndDate(new Date("2001-01-01"));

You can get start date relative to a end date:

// Will return date of "2000-01-01"
TimeSpan.fromDays(365).toStartDate(new Date("2001-01-01"));

Serialization and deserialization of TimeSpan

The TimeSpan class supports serialization and deserialization, allowing you to easily convert instances to and from serialized formats. However, registration is required first:

import { Serde } from "@daiso-tech/core/serde";
import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter";
import { TimeSpan } from "@daiso-tech/core/time-span";

const serde = new Serde(new SuperJsonSerdeAdapter());

serde.registerClass(TimeSpan);

const timeSpan = TimeSpan.fromSeconds(12);
const serializedTimeSpan = serde.serialize(timeSpan);
const deserializedTimeSpan = serde.deserialize(serializedTimeSpan);

// logs false
console.log(serializedTimeSpan === deserializedTimeSpan);

ITimeSpan contract

The ITimeSpan contract provides a standardized way to express a duration as milliseconds.

Key components, including Cache and Lock, rely on this contract, ensuring they are not tightly coupled to a specific duration implementation.

This decoupling is crucial for interoperability, allowing seamless integration with external time libraries like Luxon or Dayjs. To integrate a new library, its duration objects must simply implement the ITimeSpan contract.

info

Note TimeSpan class implements ITimeSpan contract.

The ITimeSpan contract requires you to implement the TO_MILLISECONDS method on the duration object, which must return the duration in milliseconds.

import { ITimeSpan, TO_MILLISECONDS } from "@daiso-tech/core/time-span/contracts";

export class Duration implements ITimeSpan {
constructor(private readonly timeInMs: number) {}

[TO_MILLISECONDS](): number {
return this.timeInMs;
}
}

Further information

For further information refer to @daiso-tech/core/time-span API docs.