2023-11-08 03:32:57 +01:00
|
|
|
import { formatInTimeZone } from "date-fns-tz";
|
2023-11-13 17:14:58 +01:00
|
|
|
import { injectable } from "tsyringe";
|
2023-03-03 16:23:46 +01:00
|
|
|
|
|
|
|
/**
|
2023-11-08 03:32:57 +01:00
|
|
|
* Utility class to handle time related operations.
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
|
|
|
@injectable()
|
|
|
|
export class TimeUtil
|
|
|
|
{
|
2023-11-16 16:09:01 +01:00
|
|
|
public static readonly ONE_HOUR_AS_SECONDS = 3600; // Number of seconds in one hour.
|
2023-03-03 16:23:46 +01:00
|
|
|
|
2023-11-08 03:32:57 +01:00
|
|
|
/**
|
|
|
|
* Pads a number with a leading zero if it is less than 10.
|
|
|
|
*
|
|
|
|
* @param {number} number - The number to pad.
|
|
|
|
* @returns {string} The padded number as a string.
|
|
|
|
*/
|
2023-11-08 10:06:22 +01:00
|
|
|
protected pad(number: number): string
|
2023-11-08 03:32:57 +01:00
|
|
|
{
|
|
|
|
return String(number).padStart(2, "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats the time part of a date as a UTC string.
|
|
|
|
*
|
|
|
|
* @param {Date} date - The date to format in UTC.
|
|
|
|
* @returns {string} The formatted time as 'HH-MM-SS'.
|
|
|
|
*/
|
2023-03-03 16:23:46 +01:00
|
|
|
public formatTime(date: Date): string
|
|
|
|
{
|
2023-11-08 03:32:57 +01:00
|
|
|
const hours = this.pad(date.getUTCHours());
|
|
|
|
const minutes = this.pad(date.getUTCMinutes());
|
|
|
|
const seconds = this.pad(date.getUTCSeconds());
|
2023-03-03 16:23:46 +01:00
|
|
|
return `${hours}-${minutes}-${seconds}`;
|
|
|
|
}
|
|
|
|
|
2023-11-08 03:32:57 +01:00
|
|
|
/**
|
|
|
|
* Formats the date part of a date as a UTC string.
|
|
|
|
*
|
|
|
|
* @param {Date} date - The date to format in UTC.
|
|
|
|
* @returns {string} The formatted date as 'YYYY-MM-DD'.
|
|
|
|
*/
|
2023-03-03 16:23:46 +01:00
|
|
|
public formatDate(date: Date): string
|
|
|
|
{
|
2023-11-08 03:32:57 +01:00
|
|
|
const day = this.pad(date.getUTCDate());
|
|
|
|
const month = this.pad(date.getUTCMonth() + 1); // getUTCMonth returns 0-11
|
|
|
|
const year = date.getUTCFullYear();
|
|
|
|
return `${year}-${month}-${day}`;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
2023-11-08 03:32:57 +01:00
|
|
|
/**
|
|
|
|
* Gets the current date as a formatted UTC string.
|
|
|
|
*
|
|
|
|
* @returns {string} The current date as 'YYYY-MM-DD'.
|
|
|
|
*/
|
2023-03-03 16:23:46 +01:00
|
|
|
public getDate(): string
|
|
|
|
{
|
|
|
|
return this.formatDate(new Date());
|
|
|
|
}
|
|
|
|
|
2023-11-08 03:32:57 +01:00
|
|
|
/**
|
|
|
|
* Gets the current time as a formatted UTC string.
|
|
|
|
*
|
|
|
|
* @returns {string} The current time as 'HH-MM-SS'.
|
|
|
|
*/
|
2023-03-03 16:23:46 +01:00
|
|
|
public getTime(): string
|
|
|
|
{
|
|
|
|
return this.formatTime(new Date());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-08 03:32:57 +01:00
|
|
|
* Gets the current timestamp in seconds in UTC.
|
|
|
|
*
|
|
|
|
* @returns {number} The current timestamp in seconds since the Unix epoch in UTC.
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
|
|
|
public getTimestamp(): number
|
|
|
|
{
|
|
|
|
return Math.floor(new Date().getTime() / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-08 03:32:57 +01:00
|
|
|
* Gets the current time in UTC in a format suitable for mail in EFT.
|
|
|
|
*
|
|
|
|
* @returns {string} The current time as 'HH:MM' in UTC.
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
|
|
|
public getTimeMailFormat(): string
|
|
|
|
{
|
2023-11-08 03:32:57 +01:00
|
|
|
return formatInTimeZone(new Date(), "UTC", "HH:mm");
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-08 03:32:57 +01:00
|
|
|
* Gets the current date in UTC in a format suitable for emails in EFT.
|
|
|
|
*
|
|
|
|
* @returns {string} The current date as 'DD.MM.YYYY' in UTC.
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
|
|
|
public getDateMailFormat(): string
|
|
|
|
{
|
2023-11-08 03:32:57 +01:00
|
|
|
return formatInTimeZone(new Date(), "UTC", "dd.MM.yyyy");
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-08 03:32:57 +01:00
|
|
|
* Converts a number of hours into seconds.
|
|
|
|
*
|
|
|
|
* @param {number} hours - The number of hours to convert.
|
|
|
|
* @returns {number} The equivalent number of seconds.
|
2023-03-03 16:23:46 +01:00
|
|
|
*/
|
|
|
|
public getHoursAsSeconds(hours: number): number
|
|
|
|
{
|
2023-11-16 16:09:01 +01:00
|
|
|
return hours * TimeUtil.ONE_HOUR_AS_SECONDS;
|
2023-03-03 16:23:46 +01:00
|
|
|
}
|
|
|
|
}
|