@ethersphere/bee-js
Version:
Javascript client for Bee
51 lines • 1.29 kB
JavaScript
import { Dates } from 'cafe-utility';
export class Duration {
constructor(seconds) {
this.seconds = Math.ceil(seconds);
if (seconds <= 0) {
throw Error('Duration must be greater than 0');
}
}
static fromMilliseconds(milliseconds) {
return new Duration(milliseconds / 1000);
}
static fromSeconds(seconds) {
return new Duration(seconds);
}
static fromHours(hours) {
return new Duration(hours * 60 * 60);
}
static fromDays(days) {
return new Duration(days * 24 * 60 * 60);
}
static fromWeeks(weeks) {
return new Duration(weeks * 7 * 24 * 60 * 60);
}
static fromYears(years) {
return new Duration(years * 365 * 24 * 60 * 60);
}
static fromEndDate(endDate, startDate) {
return new Duration((endDate.getTime() - (startDate ?? new Date()).getTime()) / 1000);
}
toSeconds() {
return this.seconds;
}
toHours() {
return this.seconds / 60 / 60;
}
toDays() {
return this.seconds / 24 / 60 / 60;
}
toWeeks() {
return this.seconds / 7 / 24 / 60 / 60;
}
toYears() {
return this.seconds / 365 / 24 / 60 / 60;
}
toEndDate(startDate) {
return new Date((startDate ?? new Date()).getTime() + this.seconds * 1000);
}
represent() {
return Dates.secondsToHumanTime(this.seconds);
}
}