import { DAY, HOUR, MINUTE, SECOND } from './utils'; function toDouble(value: number): string { return ('0' + value).substr(-2); } export class TimeLeft { public static fromString(value: string): TimeLeft { if (!value || value.length === 0) { throw new Error('str is empty'); } const [s, m = 0, h = 0, d = 0] = value .split(':') .reverse() .map((i) => parseInt(i, 10)); return new TimeLeft(d * DAY + h * HOUR + m * MINUTE + s * SECOND); } constructor(private timestamp: number) {} public getSeconds(): number { return Math.floor((this.timestamp % MINUTE) / SECOND); } public getMinutes(): number { return Math.floor((this.timestamp % HOUR) / MINUTE); } public getHours(): number { return Math.floor((this.timestamp % DAY) / HOUR); } public getDays(): number { return Math.floor(this.timestamp / DAY); } public toTimestamp(): number { return this.timestamp; } public toString(): string { return [this.getDays(), this.getHours(), this.getMinutes(), this.getSeconds()].map(toDouble).join(':'); } }